gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:45:58 25 4
gpt4 key购买 nike

我有一个包含组和子项的可扩展列表。每个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com