Search code examples
javaandroidnavigation-drawerexpandablelistview

ExpandableListView is behaving abnormally for the last item in the list


I am using ExpandableListsViews in the Android Nav Drawer instead of menu's. And OnExpandlistner i am changing the background color. The first two items are behaving normally and their bachground color is changed. But the last item is doing the opposite.i have tried everything, i have debugged the code. On the last item the set background color code is runnig but it isn't changed. the next time i click the item, it change the color. And after if i click the other lists item. last item color also changes.

        simpleExpandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
        @Override
        public void onGroupCollapse(int i) {
            if(i == 0){
                View view = getGroupView(simpleExpandableListView,0);
                view.setBackgroundColor(Color.parseColor("#282828"));
            }
            if(i == 1){
                View view = getGroupView(simpleExpandableListView,1);
                view.setBackgroundColor(Color.parseColor("#282828"));
            }
            if(i == 2){
                View view = getGroupView(simpleExpandableListView,2);
                view.setBackgroundColor(Color.parseColor("#282828"));
            }
        }
    });

    simpleExpandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int i) {
            if(i == 0){
                View view = getGroupView(simpleExpandableListView,0);
                view.setBackgroundColor(Color.parseColor("#616262"));
            }
            if(i == 1){
                View view = getGroupView(simpleExpandableListView,1);
                view.setBackgroundColor(Color.parseColor("#616262"));
            }
            if(i == 2){
                View view = getGroupView(simpleExpandableListView,2);
                view.setBackgroundColor(Color.parseColor("#616262"));
            }
        }
    });

I am Even Getting the respected Views for each item.But Still. Here another Logic i have tried.

   simpleExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            //get the group header
            GroupInfo headerInfo = deptList.get(groupPosition);
            Log.d("GroupPosition", "onGroupClick: " + groupPosition);
            if(parent.isGroupExpanded(groupPosition)){
                v.setBackgroundColor(getResources().getColor(R.color.colorMenuBackground));
            }
            if(!parent.isGroupExpanded(groupPosition)){
                v.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            }
            //display it or do something with it
            /*Toast.makeText(getBaseContext(), " Header is :: " + headerInfo.getName(),
                    Toast.LENGTH_LONG).show();*/

            return false;
        }
    });

But Still Getting the Same Result. If Someone can point me in the right direction, and tell me what am i doing wrong, It will be much appreciated. Thanks you


Solution

  • Problem Solved! This can all be solved by using expandableListView xml attribute. "android:listSelector="any Color you want " "