【问题标题】:Android ExpandableListView getChildView not triggeringAndroid ExpandableListView getChildView 未触发
【发布时间】:2016-04-08 10:05:51
【问题描述】:

我根据许多教程和一些来自 SO 的错误问题制作了 ExpandableListView。 父列表由两个 TextView 和一个具有标准布局的 ImageButton 组成。子列表由单个 TextView 组成。 数据源来自SQLite数据库,从activity生成到ArrayList。

问题:

即使按钮正常工作,父组列表也能正常工作,但是当我尝试展开时,它不会展开。所以我尝试放一些 Toast 消息,但是在打开活动之后,它看起来像 getChildView() 方法根本没有触发,因为 Toast 消息根本没有显示。

ExpandableListAdapter 类是这样的

public class InventoryListExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private ArrayList<Inventory> data;
    private HashMap<String, ArrayList<String>> list_data;

    public InventoryListExpandableListAdapter(Context context, ArrayList<Inventory> data, HashMap<String, ArrayList<String>> list_data) {
        this.context = context;
        this.data = data;
        this.list_data = list_data;
    }

    @Override
    public int getGroupCount() {return data.size();}

    @Override
    public int getChildrenCount(int groupPosition) {return list_data.get(data.get(groupPosition)).size();}

    @Override
    public Inventory getGroup(int groupPosition) {return data.get(groupPosition);}

    @Override
    public Object getChild(int groupPosition, int childPosition) {return list_data.get(data.get(groupPosition).getCode()).get(childPosition);}

    @Override
    public long getGroupId(int groupPosition) {return groupPosition;}

    @Override
    public long getChildId(int groupPosition, int childPosition) {return childPosition;}

    @Override
    public boolean hasStableIds() {return true;}

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            final Inventory inventory = getGroup(groupPosition);

            convertView = View.inflate(context, R.layout.listview_layout_two_row_delete, null);
            ImageButton btnDelete = (ImageButton)convertView.findViewById(R.id.btnDelete);
            TextView text1 = (TextView)convertView.findViewById(R.id.text1);
            TextView text2 = (TextView)convertView.findViewById(R.id.text2);

            text1.setText(inventory.getCode());
            text2.setText(inventory.getItem());

            btnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context, "DELETED", Toast.LENGTH_SHORT).show();
                }
            });
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final String childText = (String)getChild(groupPosition, childPosition);
        LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            convertView = infalInflater.inflate(R.layout.listview_layout_single_text, null);
        }
        Toast.makeText(context, "Make Child number "+String.valueOf(childPosition), Toast.LENGTH_SHORT).show();

        TextView text1 = (TextView)convertView.findViewById(R.id.text1);
        text1.setText(childText);
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}

感谢您提供的任何帮助。谢谢。

【问题讨论】:

  • 如果没有调用它可能是因为getChildrenCount正在返回0
  • 我查过了,size不是0

标签: java android expandablelistview


【解决方案1】:

我花了好几个小时试图找到同样的问题,结果发现这对我来说很简单:

  1. GroupView 中有一个 ImageButton,它拦截触摸以展开视图。使用它来修复它:

android:focusable="假" android:focusableInTouchMode="false"

  1. 由于组视图未展开,因此没有理由浪费 CPU 周期来获取不会显示的视图。
  2. 适配器中与孩子相关的所有方法都没有被调用。

尝试调用expandableListView.expandGroup(0...n)手动展开,看看是否调用了适配器中的子相关方法

【讨论】:

  • 这个解决方案对我有用。非常感谢。这让我发疯了!
【解决方案2】:

我找到了一种不同的方法来调用 getChildView(),当它没有在 ExpandedListAdapter 类中自动调用时。这是一个似乎可以解决问题的快捷方式。我有一个扩展列表视图,其中父组项目是复选框列表。单击复选框可显示子项。这是我编写 getGroupView() 函数的方法。

@Override
public View getGroupView(final int groupPosition, final boolean isExpanded, View convertView, final ViewGroup viewGroup)
{
    if (convertView == null)
    {
        LayoutInflater inflater = (LayoutInflater) m_context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.expandable_list_milk_type,null);
    }
    final CheckBox cbMilkType = (CheckBox) convertView.findViewById(R.id.cb_milk_type);
    cbMilkType.setText(m_listMilktype.get(groupPosition));

    cbMilkType.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ExpandableListView expandableListView = (ExpandableListView)viewGroup;
            cbMilkType.toggle();
            if (!isExpanded) {
                expandableListView.expandGroup(groupPosition);
            }
            else {
                expandableListView.collapseGroup(groupPosition);
            }
        }
    });
    return convertView;
}

【讨论】:

    【解决方案3】:

    更改 hasStableIds 方法

      @Override
        public boolean hasStableIds() {return false;}
    

    **请更改您的 getGroupView 方法 :: **

     @Override
     public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            if (convertView == null) {
                final Inventory inventory = getGroup(groupPosition);
    
                convertView = View.inflate(context, R.layout.listview_layout_two_row_delete, null);
                }
                ImageButton btnDelete = (ImageButton)convertView.findViewById(R.id.btnDelete);
                TextView text1 = (TextView)convertView.findViewById(R.id.text1);
                TextView text2 = (TextView)convertView.findViewById(R.id.text2);
    
                text1.setText(inventory.getCode());
                text2.setText(inventory.getItem());
    
                btnDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(context, "DELETED", Toast.LENGTH_SHORT).show();
                    }
                });
    
            return convertView;
        }
    

    【讨论】:

      猜你喜欢
      • 2014-04-30
      • 1970-01-01
      • 1970-01-01
      • 2015-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多