Artem
Artem

Reputation: 4639

ExpandableListView doesn't work

I googled answer for my problem but I didn't find nothing for me.

I want use ExpandableListView in my project. I get code from example, but it doesn't work (doesn't expand item view).

This is my xml:

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

And this code:

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);

And this my 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;
    }
}

I can see groups in ExpandableListView but when I click on the item it doesn't expanded.

Upvotes: 2

Views: 344

Answers (2)

Srikanth
Srikanth

Reputation: 1575

I think your group header layout 'activity_repair_tune_detail_item' may contain any focusable items like Checkbox or Button or Edittext. if it is true try by add android:focusable="false" to them. code uploaded by you is working properly to me.

Upvotes: 1

Emdja
Emdja

Reputation: 64

You have to use

"implements OnChildClickListener, ExpandableListView.OnGroupClickListener"

at the class, where you will use your ExpandableListView.

After initializing your ListView you set the listener, where you implement your expandable listview.

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

and override the methods

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

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

I guess that will fix your issue.

Upvotes: 1

Related Questions