Way*_*yne 14 android android-widget android-layout
我有一个带有ArrayList适配器的ListView.该行是不是很复杂(在左边,里面TextViews一个的LinearLayout,以及右侧的复选框的图像...布局被复制在下面).我们的目标是让在用户点击了一个QuickAction酒吧上来在图像或文本上,如果用户单击CheckBox,则具有CheckBox更改状态.我让每个部分独立工作,但不是当它们在布局中在一起时 - 不知何故,我正在失去onItemClick事件.
该"QuickAction"栏由OnItemClickListener()被激活,并能正常工作 - 除非我有布局的复选框,在这种情况下复选框正常工作(使用的onClick()),但onItemClickListener永远不会在用户点击了在发射行但在CheckBox之外.布局(减去一些风格的东西)是:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/vw1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView android:id="@+id/img"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox android:id="@+id/ckb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:checked="false" />
<!-- stack text in middle section of the row -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content""/>
<TextView android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
它背后的代码并不复杂:
public class ListGroupsActivity extends BaseActivityForList {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
// add QuickAction bar
ListView lv = getListView();
lv.setOnItemClickListener(new QAListener(this));
}
}
//inner classes of the ListGroupsActivity
class CbOnClickListener implements OnClickListener {
// ...
// test the checkbox isChecked() and keep state in 'selectedItems' array
class GroupListAdapter extends ArrayAdapter<Group> {
super(/*...*/)
CbOnClickListener cblistener = null; // common listener for all the CheckBoxes
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// here do normal testing for convertView being null etc
// heres the view inflation from the layout into the ViewHolder
holder.tv1 = (TextView) convertView.findViewById(R.id.text1);
holder.tv2 = (TextView) convertView.findViewById(R.id.text2);
holder.img = (ImageView) convertView.findViewById(R.id.img);
holder.ckb = (CheckBox) convertView.findViewById(R.id.ckb);
Group g = groups.get(position); // this is the data obj for the row
holder.tv1.setText(g.getName());
holder.tv2.setText("child groups");
Bitmap bm = BitmapFactory.decodeFile(g.getIconUri());
holder.img.setImageBitmap(bm);
Integer key = (Integer) g.getId();
holder.ckb.setChecked(selectedItems.contains(key));
holder.ckb.setOnClickListener(cblistener); // hook onClick to cblistener
return convertView;
}
Run Code Online (Sandbox Code Playgroud)
QAListener是我所有列表活动的超类:
public class BaseActivityForList extends
OrmLiteBaseListActivity<DatabaseHelper> {
// QAListener inner class, constructs new quick action bar when item clicked
class QAListener implements OnItemClickListener {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// has three ActionItem instances, 'copyAction', 'delAction', 'editAction'
ActionItem copyAction = new ActionItem();
copyAction.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// ... does some stuff
mQuickAction.dismiss();
}
}); // end setOnClickListener
} // end onItemClick
} // end QAListener
} end BaseActivityForList
Run Code Online (Sandbox Code Playgroud)
那么,我如何安排Checkbox拿起onClick,以及列表项的其余部分来获取onItemClick()?
Ale*_*man 39
经过几个小时的调试和研究.我通过在复选框上设置以下属性来实现它:
android:focusable="false"
android:focusableInTouchMode="false"
Run Code Online (Sandbox Code Playgroud)
此外,ImageView可能需要按照http://blog.sachin.name/?p=62中的描述进行配置(感谢该人)
在我这边,我发现项目视图不能是可点击的,文本视图也不应该是可点击的.我花了一段时间才意识到我在代码中某处的项目视图中调用了setClickable(true).
Ewo*_*oks 12
只是为了补充这个被接受的答案:如果ImageButton不是CheckBox在xml focusable和focusableInTouchModefalse中设置是不够的,但在运行时可以将focusable设置为false.这意味着要确保onItemClick检测到必须做的事情如下:
button.setFocusable(false);
Run Code Online (Sandbox Code Playgroud)
所以事情完全有效.. 这里的作者原创作品.希望它对某人有所帮助.
干杯;)
编辑:有更优雅的解决方案尝试添加android:descendantFocusability="blocksDescendants"列表元素的根布局.这将使点击onListItem成为可能,并且你可以单独处理Button或ImageButton点击
| 归档时间: |
|
| 查看次数: |
11112 次 |
| 最近记录: |