Misha
Misha

Reputation: 5380

Expandable list child selection problem in Android

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 selected and actual child area stays untouched. I need an opposite effect when only child area being selected.

Upvotes: 1

Views: 2191

Answers (1)

Misha
Misha

Reputation: 5380

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;
    }
    

Upvotes: 1

Related Questions