android - Android中的可扩展列表子选择问题

标签 android expandablelistadapter

我有一个包含组和子项的可扩展列表。每个 child 都属于 TableRow 类,包含图像、文本和另一幅图​​像。它的宽度小于组宽度,导致子行前后偏移。 问题是当触摸 child 时,偏移区域变为选中状态,而实际的 child 区域保持不变。当只选择子区域时,我需要相反的效果。

最佳答案

好的。找到了解决方案。

  1. 禁用 isChildSelectable:

    public boolean isChildSelectable(int groupPosition, int childPosition) {            
        return false;        
    }
    
  2. 在 getChildView 中实现我的 onTouchListener:

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

关于android - Android中的可扩展列表子选择问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4956124/

相关文章:

android - 调用 Android native 方法失败

java - 如何将 map 聚焦到用户位置?

android - 用动画展开 ListView 项

android - 为 ExpandableListView 创建布局

android - 似乎无法弄清楚 ExpandableListAdapter

java - 错误: No resource found that matches the given name (at 'theme' with value '@style/Theme.Material.Light.DarkActionBar' )

安卓 android.database.StaleDataException

android - 无法将 crashlytics 与 ADT 集成到 Eclipse 中

android - 从 ExpandableListView 检索 ChildItem?

android - 自定义ListView找不到按钮