Android Change image on click, custom expandable list adapter
Asked Answered
A

3

1

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

}
Anyhow answered 7/4, 2014 at 11:24 Comment(0)
A
11

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);
}
Albany answered 7/4, 2014 at 12:18 Comment(1)
that's perfect solution which i was finding,happy coding :)Riess
K
5

The best way to change images of parent in case of changing expansion of a group (Collapsing or Expanding) is to use getGroupView() method and the property with the method isExpanded bolean.

public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
  ...

    if (isExpanded) {
        groupHolder.img.setImageResource(R.drawable.group_down);
    } else {
        groupHolder.img.setImageResource(R.drawable.group_up);
    }
  ...

}
Kalmick answered 7/4, 2014 at 12:17 Comment(1)
#22831415Anyhow
C
1
 @BindView(R.id.expandableListView)
ExpandableListView expandableListView;



List<ExpandableGroupModel> expandableListTitle;
HashMap<String, List<String>> expandableListDetail;
ExpandableListAdapter expandableListAdapter;
private int lastExpandedPosition = -1;

add following code in onCreate or where u are getting data

    expandableListDetail = new HashMap<String, List<String>>();

    List<String> Attractions = new ArrayList<String>();
    Attractions.add("Coachin");
    Attractions.add("Munnar");
    Attractions.add("Thekkadi");
    Attractions.add("Alleppy");

    List<String> football = new ArrayList<String>();
    football.add("Brazil");
    football.add("Spain");
    football.add("Germany");
    football.add("Netherlands");
    football.add("Italy");

    List<String> basketball = new ArrayList<String>();
    basketball.add("United States");
    basketball.add("Spain");
    basketball.add("Argentina");
    basketball.add("France");
    basketball.add("Russia");

    expandableListDetail.put("visit for", Attractions);
    expandableListDetail.put("Seasonality", football);
    expandableListDetail.put("Major Attractions", basketball);
    expandableListDetail.put("Famous Shopping", football);
    expandableListDetail.put("Cuisines", basketball);
    expandableListDetail.put("Famous Festivals", basketball);
    expandableListDetail.put("Hotels", basketball);
    expandableListDetail.put("Price starts from", basketball);


    // expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
    expandableListTitle = new ArrayList<>();

    ExpandableGroupModel expandableGroupModel = new ExpandableGroupModel();
    expandableGroupModel.setName("visit for");
    expandableGroupModel.setImage(R.drawable.visit_for);
    expandableListTitle.add(expandableGroupModel);

    expandableGroupModel = new ExpandableGroupModel();
    expandableGroupModel.setName("Seasonality");
    expandableGroupModel.setImage(R.drawable.seasonality);
    expandableListTitle.add(expandableGroupModel);

    expandableGroupModel = new ExpandableGroupModel();
    expandableGroupModel.setName("Major Attractions");
    expandableGroupModel.setImage(R.drawable.major_attractions);
    expandableListTitle.add(expandableGroupModel);

    expandableGroupModel = new ExpandableGroupModel();
    expandableGroupModel.setName("Famous Shopping");
    expandableGroupModel.setImage(R.drawable.famous_shopping);
    expandableListTitle.add(expandableGroupModel);

    expandableGroupModel = new ExpandableGroupModel();
    expandableGroupModel.setName("Cuisines");
    expandableGroupModel.setImage(R.drawable.cuisines);
    expandableListTitle.add(expandableGroupModel);


    expandableGroupModel = new ExpandableGroupModel();
    expandableGroupModel.setName("Famous Festivals");
    expandableGroupModel.setImage(R.drawable.famous_festivals);
    expandableListTitle.add(expandableGroupModel);


    expandableGroupModel = new ExpandableGroupModel();
    expandableGroupModel.setName("Hotels");
    expandableGroupModel.setImage(R.drawable.hotels);
    expandableListTitle.add(expandableGroupModel);


    expandableGroupModel = new ExpandableGroupModel();
    expandableGroupModel.setName("Price starts from");
    expandableGroupModel.setImage(R.drawable.price_start);
    expandableListTitle.add(expandableGroupModel);


    //Set adapter
    expandableListAdapter = new CustomExpandableListAdapter(getActivity(), expandableListTitle, expandableListDetail, "Dest_detail");
    expandableListView.setAdapter(expandableListAdapter);
    expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

        @Override
        public void onGroupExpand(int groupPosition) {
            Toast.makeText(getActivity(),
                    expandableListTitle.get(groupPosition).getName() + " List Expanded.",
                    Toast.LENGTH_SHORT).show();


            if (lastExpandedPosition != -1
                    && groupPosition != lastExpandedPosition) {
                expandableListView.collapseGroup(lastExpandedPosition);
            }
            lastExpandedPosition = groupPosition;


        }
    });

    expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

        @Override
        public void onGroupCollapse(int groupPosition) {
            Toast.makeText(getActivity(),
                    expandableListTitle.get(groupPosition).getName() + " List Collapsed.",
                    Toast.LENGTH_SHORT).show();

        }
    });

    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {
            Toast.makeText(
                    getActivity(),
                    expandableListTitle.get(groupPosition).getName()
                            + " -> "
                            + expandableListDetail.get(
                            expandableListTitle.get(groupPosition).getName()).get(
                            childPosition), Toast.LENGTH_SHORT
            ).show();
            return false;
        }
    });

    expandableListView.setOnTouchListener(new View.OnTouchListener() {
        // Setting on Touch Listener for handling the touch inside ScrollView
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // Disallow the touch request for parent scroll on touch of child view
            v.getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }
    });

Adapter is as follows:

CustomExpandableListAdapter.java

public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<ExpandableGroupModel> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;
Typeface roboto;
String from;

public CustomExpandableListAdapter(Context context, List<ExpandableGroupModel> expandableListTitle,
                                   HashMap<String, List<String>> expandableListDetail, String from) {
    this.context = context;
    this.expandableListTitle = expandableListTitle;
    this.expandableListDetail = expandableListDetail;
    this.from = from;
}

@Override
public Object getChild(int listPosition, int expandedListPosition) {
    return this.expandableListDetail.get(this.expandableListTitle.get(listPosition).getName())
            .get(expandedListPosition);
}

@Override
public long getChildId(int listPosition, int expandedListPosition) {
    return expandedListPosition;
}

@Override
public View getChildView(int listPosition, final int expandedListPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {
    final String expandedListText = (String) getChild(listPosition, expandedListPosition);
    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_item, null);
    }
    TextView expandedListTextView = (TextView) convertView
            .findViewById(R.id.expandedListItem);
    expandedListTextView.setText(" - " + expandedListText);


    roboto = Typeface.createFromAsset(context.getAssets(), "fonts/roboto_regular.ttf");
    expandedListTextView.setTypeface(roboto);

    if (from.equals("Dest_detail")) {
        expandedListTextView.setBackgroundResource(R.color.white);

    } else {
        expandedListTextView.setBackgroundResource(R.color.screen_bg_color);

    }
    return convertView;
}

@Override
public int getChildrenCount(int listPosition) {
    return this.expandableListDetail.get(this.expandableListTitle.get(listPosition).getName())
            .size();
}

@Override
public Object getGroup(int listPosition) {
    return this.expandableListTitle.get(listPosition);
}

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

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

@Override
public View getGroupView(int listPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    ExpandableGroupModel listObj = (ExpandableGroupModel) getGroup(listPosition);
    String listTitle = listObj.getName();
    int listimage = listObj.getImage();
    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_group, null);
    }
    TextView listTitleTextView = (TextView) convertView
            .findViewById(R.id.listTitle);
    ImageView img_group_icon = (ImageView) convertView
            .findViewById(R.id.img_group_icon);
    ImageView img_group_arrow = (ImageView) convertView
            .findViewById(R.id.img_group_arrow);
    listTitleTextView.setText(listTitle);
    img_group_icon.setImageResource(listimage);


    roboto = Typeface.createFromAsset(context.getAssets(), "fonts/roboto_regular.ttf");
    listTitleTextView.setTypeface(roboto, Typeface.BOLD);

    if (isExpanded) {
        img_group_arrow.setImageDrawable(context.getResources().getDrawable(R.drawable.up));
        listTitleTextView.setTextColor(context.getResources().getColor(R.color.colorPrimary));
    } else {
        img_group_arrow.setImageDrawable(context.getResources().getDrawable(R.drawable.down));
        listTitleTextView.setTextColor(context.getResources().getColor(R.color.textcolor_package));

    }


    return convertView;
}

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

@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
    return true;
}
}

ExpandableGroupModel model is as follows:

public class ExpandableGroupModel {

String name;
int image;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getImage() {
    return image;
}

public void setImage(int image) {
    this.image = image;
}
}
Claudetta answered 3/11, 2018 at 6:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.