我想要完成的是在每一行中都有一个复选框,能够单独选中该框(用于批量删除),并能够选择整行以查看与列表项关联的数据。
我已经完成了一个带有textview的复选框,但只允许选中该框,我无法点击列表项来查看该项目数据。我也使用了checkedTextView
,但是检查了你点击调用onListItemClick
的行的那个框,这不是我想要的。是否有一些我可以单独检查框中的单击列表视图项目?
在选择要删除和查看的邮件
时,几乎尝试执行gmail
应用程序所执行的操作
这是我的行布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"/>
<TextView
android:id="@+id/nameCheckTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/checkBox1"
android:layout_toRightOf="@+id/checkBox1"
android:paddingTop="5dp"
android:paddingLeft="15dp"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
创建listview
@Override
public void onActivityCreated(Bundle state){
super.onActivityCreated(state);
lv = getListView();
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setItemsCanFocus(false);
setEmptyText("No Bowlers");
registerForContextMenu(getListView());
populateList();
}
修改
我的填充方法
public void populateList(){
String[] fields = new String[] {BowlersDB.NAME};
//mAdapter = new CheckAdapter(getActivity(),R.layout.check_listview,null,fields,new int[] {R.id.nameCheckTV});
mAdapter = new SimpleCursorAdapter(getActivity(),R.layout.check_listview,null,fields,
new int[] {R.id.nameCheckTV});
setListAdapter(mAdapter);
getLoaderManager().initLoader(0,null,this);
}
答案 0 :(得分:13)
问题是Android不允许您选择包含可关注元素的列表项。尝试修改列表项上的复选框:
android:focusable="false"
答案 1 :(得分:0)
我对这个问题有一个奇怪的解决方法。这是我的解决方案。
将此用于ListView。在这种情况下,产品只是保存数据的模型对象:
public class CatalogItemAdapter extends ArrayAdapter<Product> //
{
private ArrayList<Product> products;
private Activity activity;
public CatalogItemAdapter(Context context, int textViewResourceId,
ArrayList<Product> items, Activity activity) //
{
super(context, textViewResourceId, items);
this.products = items;
this.activity = activity;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) //
{
Product product = products.get(position);
if (convertView == null) //
{
LayoutInflater vi = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.catalog_item_stub, null, false);
}
}
}
在onResume()
的某个地方,请点击:
listView = (ListView) activity.findViewById(R.id.CatalogProducts);
m_adapter = new CatalogItemAdapter(activity,
R.layout.catalog_item_stub, products, activity);
if (products == null)
products = new ArrayList<Product>();
listView.setAdapter(m_adapter);
R.layout.catalog_item_stub
是一个用XML创建的布局存根(就像复选框一样,在其中放入适当的项目):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/catalog_item_stub"
android:layout_width="match_parent" android:layout_height="90dp"
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="5dp">
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent" android:weightSum="5"
android:gravity="center_vertical|right">
<TextView android:layout_width="0dp" android:layout_height="match_parent"
android:text="product_title" android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/ProductTitle" android:padding="5dp"
android:layout_weight="2.5" android:textColor="#000000" />
<CheckBox android:padding="5dp" android:layout_height="match_parent"
android:text="select" android:layout_width="0dp" android:id="@+id/chkSelect"
android:layout_weight="1.5" android:textColor="#000000"
android:gravity="right" />
</LinearLayout>
希望这有帮助!如果您需要任何澄清,请使用Holler。
答案 2 :(得分:0)
gmail应用程序使用自己的名为CanvasConversationHeaderView的视图来管理其子视图。这种方法可能比你想要的更重。
更简单的方法是使复选框不是“可聚焦”(如Alex Lockwood建议的那样),然后在XML中附加onClick。
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:focusable="false"
android:onClick="onCheckboxClicked"/>
然后在您的活动代码中添加
public void onCheckboxClicked(View view) {
RelativeLayout rl = (RelativeLayout)view.getParent();
Log.d(TAG, "Checkbox clicked! getTag returned: " + rl.getTag());
}
编辑:如何从SimpleCursorAdapter.bindView
添加标签private class MyCursorAdapter extends SimpleCursorAdapter {
public MyCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
//Log.d(TAG, "Cursor pos: " + cursor.getPosition());
String name = cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
view.setTag(name);
super.bindView(view, context, cursor);
}
}
注意:我将标签设置为bindView调用中的View,它是xml根目录下的RelativeLayout。查看onCheckboxClicked方法以查看我是如何获得标记的。
答案 3 :(得分:-3)
您需要为ListView设置一个onItemClickListener,它将启动另一个活动,其中包含单击行时所选行的信息(当然,在CheckBox之外)。我建议让你的Activity实现AdapterView.OnItemClickListener
,这需要方法
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {}
在此方法中,您可以启动一个活动,其中包含与所选行中的数据相对应的详细信息。 希望我能正确理解你的问题。