开发者

Expandable list child selection problem in Android

开发者 https://www.devze.com 2023-02-09 22:10 出处:网络
I have an expandable list with groups and childrens. Each child is of TableRow class holding image, text and another image. Its width is less than group width which leads to offset before and after ch

I have an expandable list with groups and childrens. Each child is of TableRow class holding image, text and another image. Its width is less than group width which leads to offset before and after child row. The problem is when touching the child, offset area turns to se开发者_开发技巧lected and actual child area stays untouched. I need an opposite effect when only child area being selected.


Ok. Found a solution.

  1. Disabled isChildSelectable:

    public boolean isChildSelectable(int groupPosition, int childPosition) {            
        return false;        
    }
    
  2. Implemented my onTouchListener in getChildView:

    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild,                
            View convertView, ViewGroup parent) 
    {   
        parent.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
        View childView = getMyView();
    
        childView.setOnTouchListener(new OnTouchListener(){
            @Override
            public boolean onTouch(View view, MotionEvent event) {
    
                if (event.getAction() == MotionEvent.ACTION_DOWN)
                {                       
                    view.setBackgroundColor(Color.BLUE);                        
                    return true;
                }
                if (event.getAction() == MotionEvent.ACTION_UP)
                {
                    view.setBackgroundColor(Color.WHITE);
                    return true;
                }
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE)
                {
                    view.setBackgroundColor(Color.WHITE);
                    return true;
                }
                if (event.getAction() == MotionEvent.ACTION_CANCEL)
                {
                    view.setBackgroundColor(Color.WHITE);
                    return true;
                }
                return false;
            }               
        });
        LinearLayout newView = new LinearLayout(context);
        newView.setPadding(15, 0, 15, 0);
        newView.addView(childView);
        return newView;
    }
    
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号