我有一个包含组和子项的可扩展列表。每个 child 都属于 TableRow 类,包含图像、文本和另一幅图像。它的宽度小于组宽度,导致子行前后偏移。 问题是当触摸 child 时,偏移区域变为选中状态,而实际的 child 区域保持不变。当只选择子区域时,我需要相反的效果。
最佳答案
好的。找到了解决方案。
禁用 isChildSelectable:
public boolean isChildSelectable(int groupPosition, int childPosition) { return false; }
在 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/