Use custom selector for parent item and children items in ExpandableListView

扶醉桌前 提交于 2019-12-06 07:47:14

Try doing the following,

  • Add a hashmap of children states(Selected or not selected)

    private HashMap> listDataChildStates;

  • In your adapter constructor, add the following code:

    // Initially, set all child items as not selected
    for(int i = 0;i<listDataHeader.size();i++)
    {
        ArrayList<String> childItems = listChildData.get(listDataHeader.get(i));
        ArrayList<Boolean> childStates;
        for(int j = 0;j<childItems.size();j++)
        {
                childStates = listDataChildStates.get(childItems.get(j));
                if(childStates == null)
                    childStates = new ArrayList<Boolean>();
                childStates.add(false);
        }
    }
    
  • Then, in your onChildClickListener()

    expListView.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                    boolean childState = listDataChildStates.get(listDataHeader.get(groupPosition)).get(childPosition);
      listDataChildStates.get(listDataHeader.get(groupPosition)).remove(childPosition);
      listDataChildStates.get(listDataHeader.get(groupPosition)).add(childPosition,new Boolean(!childState));
    
                 notifyDataSetChanged();
                 return false;
            }
        });
    
  • Finally, in your getChildView() method, add the following:

     boolean childState = listDataChildStates.get(listDataHeader.get(groupPosition)).get(childPosition);
     if(childState)
     {
             // Set your child item as selected
     }else
     {
            // Set your child item as not selected
     }
    

have you tried like this ?

getChildView(int groupPosition, final int childPosition,
    boolean isLastChild, View convertView, ViewGroup parent) {

final String childText = (String) getChild(groupPosition, childPosition);

if (convertView == null) {
    LayoutInflater infalInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = infalInflater.inflate(R.layout.list_item, null);
}

TextView txtListChild = (TextView) convertView
        .findViewById(R.id.lblListItem);

txtListChild.setText(childText);

*convertView.setBackgroundDrawable(some drawable for view from drawable resourse)
return convertView;

}

I think this whould be direct way.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!