Bar*_*ica 31 android listview button adapter onitemclicklistener
我有ListView
自定义Adapter
,View
以ListView
这种方式供应:
public View getView(int position, View convertView, ViewGroup parent)
{
RelativeLayout.LayoutParams lineParams;
RelativeLayout line=new RelativeLayout(context);
TextView tv=new TextView(context);
tv.setText("Text in postion="+i);
lineParams=new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lineParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
line.addView(tv, lineParams);
lineParams.addRule(RelativeLayout.CENTER_IN_PARENT);
//checkbox
CheckBox checkBox=new CheckBox(context);
lineParams=new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lineParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lineParams.addRule(RelativeLayout.CENTER_IN_PARENT);
line.addView(checkBox, lineParams);
return line;
}
Run Code Online (Sandbox Code Playgroud)
在ListView
里面的某个地方setOnItemClickListener()
,它应该拦截项目点击事件.我的问题是,每当我尝试向项目添加复选框时 - 我都没有得到任何回复ListView
.如果我跳过CheckBox
或其他任何Button
工作.
我真的遇到了这个问题,我尝试了各种布局,对齐,包装等等 - 没用.看起来像CheckBox
干扰ListView
项目点击事件.
任何想法如何克服?
Pra*_*tik 52
只需将此行添加到项目视图中,而不是listView本身
android:focusable="false"
Run Code Online (Sandbox Code Playgroud)
从Android自定义ListView查看更多详细信息无法点击项目
dan*_*v91 17
如果列表项中包含ImageButtons,则需要添加:
android:descendantFocusability="blocksDescendants"
Run Code Online (Sandbox Code Playgroud)
到根列表项元素[例如根布局].
然后在列表项的每个ImageButton中,您需要添加:
android:focusableInTouchMode="true"
Run Code Online (Sandbox Code Playgroud)
这对我有用 - 但我使用的是ImageButtons,而不是标准按钮.
Jai*_*oni 13
我也面临同样的问题,我试图设置android:focusable="false"
为listview,但它不起作用然后我将其添加到listview项目...就像在我的listview项目我已经使用切换按钮创建问题,我添加android:focusable="false"
到切换按钮和listview on item click listener再次开始工作