Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Expandable list selector behaviour

I am facing a major problem with my selectors in Android Expandable list view.

Here is my code Expandable list view.

public class UserMenuAdapter extends BaseExpandableListAdapter {

    private ArrayList<String> groups;
    private ArrayList<ArrayList<ChildItems>> childs;
    private Context context;
    public LayoutInflater inflater;

    ImageView img_availabiliy;

    private static final int[] EMPTY_STATE_SET = {};
    private static final int[] GROUP_EXPANDED_STATE_SET =
            {android.R.attr.state_expanded};
    private static final int[][] GROUP_STATE_SETS = {
         EMPTY_STATE_SET, // 0
         GROUP_EXPANDED_STATE_SET // 1
    };

    public UserMenuAdapter(Context context, ArrayList<String> groups,
            ArrayList<ArrayList<ChildItems>> childs) {
        this.context = context;
        this.groups = groups;
        this.childs = childs;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childs.get(groupPosition).get(childPosition);
    }

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

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        View v = null;
        if (convertView != null)
            v = convertView;
        else
            v = inflater.inflate(R.layout.child_layout, parent, false);
        ChildItems ci = (ChildItems) getChild(groupPosition, childPosition);
        TextView tv = (TextView) v.findViewById(R.id.tvChild);
        tv.setText(ci.getName());
        TextView tv2 = (TextView) v.findViewById(R.id.tvChild2);
        tv2.setText(ci.getDailyStatus());
        img_availabiliy = (ImageView)v.findViewById(R.id.img_childlayout_AVAILABILITY);
        ImageView friendPics = (ImageView)v.findViewById(R.id.ivFriendPics);

        /**For Group child*/
        if(groupPosition == 3 && childPosition!= 0){
            if(ci.getPicture()!= null){
                Bitmap bitmap = BitmapFactory.decodeByteArray(ci.getPicture(), 0, ci.getPicture().length);
                bitmap = Bitmap.createScaledBitmap(bitmap, 48, 48, true);
                friendPics.setImageBitmap(bitmap);
            }else{
                friendPics.setImageResource(R.drawable.ic_launcher);
            }
            img_availabiliy.setVisibility(View.INVISIBLE);
        }else{
            if(ci.getStatusState() == 1){
                img_availabiliy.setImageResource(R.drawable.online);
            }
            else if(ci.getStatusState()==0){
                img_availabiliy.setImageResource(R.drawable.offline);           
            }           
            else if (ci.getStatusState()==2) {
                img_availabiliy.setImageResource(R.drawable.away);
            }       
            else if(ci.getStatusState()==3){
                img_availabiliy.setImageResource(R.drawable.busy);
            }
            else{
                img_availabiliy.setImageDrawable(null);
            }       
            if((groupPosition == 2 && childPosition == 0)){
                friendPics.setImageResource(R.drawable.inviteto_ccm);
                img_availabiliy.setVisibility(View.INVISIBLE);
            }
            else if(groupPosition == 3 && childPosition == 0){
                friendPics.setImageResource(R.drawable.new_ccmgroup);
                img_availabiliy.setVisibility(View.INVISIBLE);          
            }else{
                if(ci.getPicture()!= null){
                    Bitmap bitmap = BitmapFactory.decodeByteArray(ci.getPicture(), 0, ci.getPicture().length);
                    bitmap = Bitmap.createScaledBitmap(bitmap, 48, 48, true);
                    friendPics.setImageBitmap(bitmap);
                }else{
                    friendPics.setImageResource(R.drawable.ic_launcher);
                }

                img_availabiliy.setVisibility(View.VISIBLE);
            }
        }       
        return v;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childs.get(groupPosition).size();
    }

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        View v = null;
        if (convertView != null)
            v = convertView;
        else
            v = inflater.inflate(R.layout.group_layout, parent, false);
        String gt = (String) getGroup(groupPosition);
        TextView tv2 = (TextView) v.findViewById(R.id.tvGroup);
        if (gt != null)
            tv2.setText(gt);
        /**Set Image on group layout, Max/min*/
        View ind = v.findViewById( R.id.explist_indicator);
        View groupInd = v.findViewById( R.id.llgroup);

        if( ind != null ) {
            ImageView indicator = (ImageView)ind;           
            if( getChildrenCount( groupPosition ) == 0 ) {
                indicator.setVisibility( View.INVISIBLE );
            } else {
                indicator.setVisibility( View.VISIBLE );
                int stateSetIndex = ( isExpanded ? 1 : 0) ;
                Drawable drawable = indicator.getDrawable();
                drawable.setState(GROUP_STATE_SETS[stateSetIndex]);
            }
        }
        if( groupInd != null ) {
            RelativeLayout indicator2 = (RelativeLayout)groupInd;
            if( getChildrenCount( groupPosition ) == 0 ) {
                indicator2.setVisibility( View.INVISIBLE );             
            } else {
                indicator2.setVisibility( View.VISIBLE );
                int stateSetIndex = ( isExpanded ? 1 : 0) ;
                Drawable drawable2 = indicator2.getBackground();                
                drawable2.setState(GROUP_STATE_SETS[stateSetIndex]);
            }
        }
        return v;
    }

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

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

    public void onGroupCollapsed(int groupPosition) {
    }

    public void onGroupExpanded(int groupPosition) {
    }
    public void updateUserMenu(ArrayList<ArrayList<ChildItems>> childBack){

        this.childs = childBack;

    }

}

When I expand the groups I change the selector bar color, and vice versa.

The problem I am facing is that when I return back from any activity or scroll up & down rigorously my expandable list view then the selector color automatically changes even if the group is selected/unselected

If anybody presses on the top of the screen (of device) then also selector changes

I am totally confused about the behaviour it is showing to me and not able to find out what is happening. Please help if anybody understands

like image 549
Gaurav Arora Avatar asked Aug 30 '26 08:08

Gaurav Arora


1 Answers

You are using a wrong set of array values, simply use this -

if( getChildrenCount( groupPosition ) == 0 ) {
        indicator.setVisibility( View.INVISIBLE );
    } else {                        
        indicator.setVisibility( View.VISIBLE );
        if(isExpanded){
            indicator.setImageResource(R.drawable.expander_min);
            indicator2.setBackgroundResource(R.drawable.list_selected);
            mbtnMenu.setVisibility(View.VISIBLE);
        }else{
            indicator.setImageResource(R.drawable.expander_max);
            indicator2.setBackgroundResource(R.drawable.list_unselected);
            mbtnMenu.setVisibility(View.INVISIBLE);
        }
    }

Thanks.

like image 176
Akhilesh Mani Avatar answered Aug 31 '26 22:08

Akhilesh Mani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!