Skip to content Skip to sidebar Skip to footer

Onclick Event Repeats With Listview Items

I am having a listview in my app. Each listview row expands on tap and a hidden view becomes visible but problem is that hidden view also becomes visible for many others items in t

Solution 1:

ListView items are reusable. When you scroll the list, some items becomes "invisible" and the view item will be reused for the one that will become visible.

You do not keep the data of which item is expanded and which do not, so you cannot set the visibility to GONE or VISIBLE for "expanded" part depending on its previous "expanded" state in getView method. Simple workaround is to make a Map<Integer,Boolean> and keep the state here, when expanding put(position,true) and on hide put(position,false). In getView

boolean state = expandedStateMap.get(position);
finalHolder.linearLayout.setVisibility((state) ? View.VISIBLE : View.GONE);

Step zero (adding map for expanded state)

publicclassScheduleTaskAdapterextendsBaseAdapter{
    Context context;
    LayoutInflater layoutInflater;
    // List<InterestAndLanguageBean> interestAndLanguageBeans=new ArrayList<>();List<BirdsDataBean> imageList = new ArrayList<>();
    Map<Integer,Boolean> expandedStateMap = new HashMap<>();

Step one:

//determine if we already expanded the itemBoolean state = expandedStateMap.get(position);
    if (state == null) {
      state = false;
    }
    finalHolder.linearLayout.setVisibility((state) ? View.VISIBLE :      View.GONE);

    holder.frameLayout.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {
                //save expanded state 
                expandedStateMap.put(position,true);
                finalHolder.linearLayout.setVisibility(View.VISIBLE);

Step two:

 holder.imageView.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {
                 expandedStateMap.put(position,false);
                finalHolder.linearLayout.setVisibility(View.GONE);
            }
        });

Post a Comment for "Onclick Event Repeats With Listview Items"