Android Extensible List Selector Behavior

I am having a serious problem with my selectors in the Expandable List view in Android.

Here is my code for Expandable List.

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 color of the selector bar and vice versa.

The problem I am running into is that when I go back from any activity or strictly view the expanded view of the list, the color of the selector automatically changes even if the group is selected / not selected

If someone clicks on the top of the screen (device), then they also select the selector

I am completely confused about what he is showing me and cannot find out what is going on. Please help if anyone understands

+3


source to share


3 answers


You are using the wrong set of array values, just 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);
        }
    }

      



Thank.

+1


source


The problem is in the getGroupView () method where you are trying to set the background for indicator and indicator2. The Drawable setState () method does not explicitly set the background - it only sets the set of resources that will be used in the UI when responding to state-changing events. You should also call invalidateSelf () after setState () to force Drawable to redraw new resources, which may or may not fix this issue. I recommend just using setBackground () or setBackgroundResource () for the Views themselves. Something like that:



 @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 );
                indicator.setBackgroundResource(isExpanded ? android.R.attr.state_expanded : 0);
            }
        }
        if( groupInd != null ) {
            RelativeLayout indicator2 = (RelativeLayout)groupInd;
            if( getChildrenCount( groupPosition ) == 0 ) {
                indicator2.setVisibility( View.INVISIBLE );             
            } else {
                indicator2.setVisibility( View.VISIBLE );
                indicator2.setBackgroundResource(isExpanded ? android.R.attr.state_expanded : 0);
            }
        }
        return v;
    }

      

0


source


Dear I am facing a similar problem in an extensible list view. i my problem i click on any parent element always goes through the whole list. my help in solving.

Just check the xml file when you use expandable list.

<ExpandableListView
        android:id="@+id/expandableListView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:transcriptMode="alwaysScroll" 
        android:layout_toRightOf="@+id/buttonBuyAmazon" >
    </ExpandableListView>

      

Android: transcriptMode = "alwaysScroll" change this with normal other disease ok

Best luck

0


source







All Articles