Highlight for selected item in expandable list

后端 未结 11 2313
攒了一身酷
攒了一身酷 2021-01-31 21:25

I have a layout where I have an expandable list in a fragment on the left and a details fragment on the right. This all works fine.

Now I would like to indicate what

11条回答
  •  南笙
    南笙 (楼主)
    2021-01-31 21:48

    The next solution help you make different list selector for group and child.

    First of all, you need disable list selector for ExpendableListView:

    
    ...
       
    ...
    
    

    After, you need copy an empty listSelector. You can do you like this

    
    ...
    private Drawable empty;
    ...
    
    // binding view here. Name of ExpandableListView will be elv
    empty = elv.getSelector();
    
    
    

    Now, we need know when we will enable list selector or disable. You should add follow changes to your adapter for that:

    
    my adapter here {
    ...
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup parent)
        {
           ...
           // after, we've got view of goup
           view.setOnTouchListener(new View.OnTouchListener()
           {
               @Override
               public boolean onTouch(View v, MotionEvent event)
               {
                   elv.setSelector(empty);
                   return false;
               }
           });
           ...
        }
    ...
        @Override
        public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, final ViewGroup parent)
        {
           ...
           // after, we've got view of child
           view.setOnTouchListener(new View.OnTouchListener()
           {
               @Override
               public boolean onTouch(View v, MotionEvent event)
               {
                   elv.setSelector(R.drawable.my_custom_list_selector);
                   return false;
               }
           });
           ...
        }
    ...
    }
    
    

    That's all. I hope this solution keeps your time.

提交回复
热议问题