Search code examples
androidexpandablelistviewexpandablelistadapter

Android Change image on click, custom expandable list adapter


I am going to implement an expandable listview with custom expandable list adapter.
And also image change on group expand and group collapse.

My Expandable list view works well but when I set image change on those events there is some problem. i.e when I expand one, automatically another one's image also changes. I think I can not hold the proper address of the group item.

Here is my code.

public class HomeFrame extends Fragment {

    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    ImageView img_selection;
    HashMap<String, List<String>> listDataChild = new HashMap<String, List<String>>();
     public static int[] GalImages = new int[] {
                R.drawable.banner_one,
                R.drawable.banner,
                R.drawable.banner_three,
                R.drawable.banner_two,};
    public HomeFrame() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.home_frame, container, false);

        ViewPager viewPager = (ViewPager) rootView.findViewById(R.id.banner);
        ImageAdapter adapter = new ImageAdapter(getActivity());
        viewPager.setAdapter(adapter);

        //img_selection=(ImageView)rootView.findViewById(R.id.img_selection);

        expListView = (ExpandableListView) rootView.findViewById(R.id.lvExp);

        // preparing list data
        prepareListData();

        listAdapter = new ExpListAdapter(getActivity(), listDataHeader,
                listDataChild);

        // setting list adapter
        expListView.setAdapter(listAdapter);

        // Listview Group click listener
        expListView.setOnGroupClickListener(new OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                    int groupPosition, long id) {
                Toast.makeText(getActivity(),
                 "Group Clicked " + listDataHeader.get(groupPosition),
                 Toast.LENGTH_SHORT).show();
                return false;

            }
        });

        // Listview Group expanded listener
        expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {

                  Toast.makeText(getActivity(),
                  groupPosition + " Expanded",
                  Toast.LENGTH_SHORT).show();

             img_selection=(ImageView)expListView.getChildAt(groupPosition).findViewById(R.id.img_selection);
                img_selection.setImageResource(R.drawable.arrow_se);
            }
        });

        // Listview Group collasped listener
        expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {

                 Toast.makeText(getActivity(),
                 groupPosition + " Collapsed",
                 Toast.LENGTH_SHORT).show();

                 img_selection=(ImageView) expListView.getChildAt(groupPosition).findViewById(R.id.img_selection);
                //ImageView img_selection=(ImageView) get.findViewById(R.id.img_selection);

                img_selection.setImageResource(R.drawable.arrow);
            }
        });

        // Listview on child click listener
        expListView.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                // TODO Auto-generated method stub
                /*
                 * Toast.makeText( getActivity(),
                 * listDataHeader.get(groupPosition) + " : " +
                 * listDataChild.get( listDataHeader.get(groupPosition)).get(
                 * childPosition), Toast.LENGTH_SHORT) .show();
                 */
                return false;
            }
        });

        return rootView;
    }

And Adapter class is..

public class ExpListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;

    public ExpListAdapter(Context context, List<String> listDataHeader,
            HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;

    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

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

    @Override
    public View 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) this._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);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.item_home, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.heading);
        //ImageView img_selection=(ImageView)convertView.findViewById(R.id.img_selection);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);
        //img_selection.setImageResource(R.drawable.arrow);

        return convertView;
    }

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

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

}

Solution

  • In your custom adapter in the getGroupView() do this:

    @Override
     public View getGroupView(int groupPosition, boolean isExpanded, View view,
                    ViewGroup parent) {
    
        ImageView img_selection=(ImageView) view.findViewById(R.id.img_selection);
        int imageResourceId = isExpanded ? R.drawable.group_closed
                            : R.drawable.group_open;
         img_selection.setImageResource(imageResourceId);
    }