ExpandableListView不起作用

时间:2015-12-11 09:08:38

标签: android expandablelistview

我用Google搜索了我的问题,但我没有找到任何帮助。

我想在我的项目中使用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中看到群组,但是当我点击该项目时,它不会展开。

2 个答案:

答案 0 :(得分:1)

你必须使用

“实现OnChildClickListener,ExpandableListView.OnGroupClickListener”

在课程中,您将使用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).

我想这会解决你的问题。

答案 1 :(得分:1)

我认为您的群组标题布局&#39; activity_repair_tune_detail_item&#39;可能包含任何可聚焦的项目,如Checkbox或Button或Edittext。如果是真的尝试添加android:focusable="false"给他们。你上传的代码对我有用。