public ImageButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setFocusable(true);
    }

 

  在listview中(或者ExpandableListview),item的view会被进行特殊的处理,通过convertview可以减少解析xml文件,提高效率。但是如果你自己解析一次,然后用变量保存,那么只有最后一个view才能正常显示,说明了每一个item的view都是不同的实例,这样就注定了每一个view事实上是可以添加按钮之类的单独响应事件的。

 

  有这么几种方法避免Button或者ImageButton抢夺整个item的焦点

  

  1. Button设置focusable=false,ImageButton要通过代码设置ib.setFocusable(false),这是因为源码的bug
    public ImageButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setFocusable(true);
    }

 

  2. 在item的layout的根viewgroup中设置android:descendantFocusability="blocksDescendants",这个属性也会使其他的view能响应焦点

 

  但是在实际开发过程中,我发现如果是ExpandableLisview的BaseExpandableListAdapter中的isChildSelectable方法覆写之后得返回true,否则也不能响应ItemClick

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-16
  • 2022-12-23
  • 2021-05-08
  • 2022-12-23
  • 2021-04-18
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-14
  • 2021-04-18
  • 2021-08-30
  • 2022-12-23
  • 2022-12-23
  • 2021-11-11
相关资源
相似解决方案