ExpandableListView 不起作用

ExpandableListView doesn't work

我用谷歌搜索了我的问题的答案,但没有找到任何适合我的东西。

我想在我的项目中使用 ExpandableListView。我从示例中获取代码,但它不起作用(不展开项目视图)。

这是我的 xml:

 <ExpandableListView
    android:id="@+id/exListView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:indicatorLeft="250dp"
    android:indicatorRight="300dp" />

还有这段代码:

mExpandableListView = (ExpandableListView) parent.findViewById(R.id.exListView);

    ArrayList<ArrayList<String>> groups = new ArrayList<ArrayList<String>>();
    ArrayList<String> children1 = new ArrayList<String>();
    ArrayList<String> children2 = new ArrayList<String>();
    children1.add("Child_1");
    children1.add("Child_2");
    groups.add(children1);
    children2.add("Child_1");
    children2.add("Child_2");
    children2.add("Child_3");
    groups.add(children2);

    ExpListAdapter adapter = new ExpListAdapter(getActivity(), groups);
    mExpandableListView.setAdapter(adapter);

这是我的适配器:

private class ExpListAdapter extends BaseExpandableListAdapter {

    private ArrayList<ArrayList<String>> mGroups;
    private Context mContext;

    public ExpListAdapter (Context context,ArrayList<ArrayList<String>> groups){
        mContext = context;
        mGroups = groups;
    }

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

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

    @Override
    public Object getGroup(int groupPosition) {
        return mGroups.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mGroups.get(groupPosition).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(int groupPosition, boolean isExpanded, View convertView,
                             ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.activity_repair_tune_detail_item, null);

        return convertView;

    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                             View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.order_part_open_layout, null);
        return convertView;
    }

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

我可以在 ExpandableListView 中看到群组,但是当我点击该项目时它没有展开。

你必须使用

"implements OnChildClickListener, ExpandableListView.OnGroupClickListener"

在 class,您将在其中使用 ExpandableListView。

初始化 ListView 后设置侦听器,在其中实现可扩展列表视图。

customListView.setOnGroupClickListener(this);
customListView.setOnChildClickListener(this);

并覆盖方法

onGroupClick(ExpandableListView parent, View v, int groupPosition, long id)

onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id).

我想这会解决您的问题。

我认为您的组页眉布局 'activity_repair_tune_detail_item' 可能包含任何可聚焦的项目,例如 Checkbox 或 Button 或 Edittext。如果是,请尝试向它们添加 android:focusable="false"。你上传的代码对我来说工作正常。