【发布时间】:2012-02-09 02:35:51
【问题描述】:
我正在尝试重现 Google 对 Gmail 应用中的 ListView 所做的操作。特别是,我希望每个列表项都包含一个 CheckBox 和两个 TextView(一个在另一个之上)。当 CheckBox 被选中(或单击)以及单击列表项上的其他任何位置时,我需要侦听器。最后,我希望 ActionBar 反映已选择的项目并提供全选、全选等选项(请参阅this screenshot)。
到目前为止,这是我想出的布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<CheckBox android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal" />
<LinearLayout android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dp"
android:focusable="true"
android:clickable="true" >
<TextView android:id="@+id/titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp" />
<TextView android:id="@+id/dateTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
这会正确显示所有内容,但我需要有关如何为两个视图(@+id/checkBox 和 @+id/linearLayout1)设置侦听器的指针.我查看了List16 API demo,但他们使用的是 simple_list_item_activated_1 布局,我不确定 XML 的样子。正如他们的代码所示,我创建了一个实现 ListView.MultiChoiceModeListener 的 ModeCallback 类,并将 ListView 的选择模式设置为 CHOICE_MODE_MULTIPLE_MODAL,但我不知道如何获取 CheckBox在我的布局中使用它。
是否有人成功复制了 Gmail 应用的 ListView 行为?我进行了相当多的搜索,但无法提出任何建议(尽管其他几个人提出了类似的问题,like this one - 大多数答案都指向同一个 API 演示)。
另外,就上下文而言,我正在将 SQLite 数据库中的数据加载到列表中,并且我创建了自己的光标适配器(工作正常)。我感觉我需要在 newView() 和 bindView() 方法中在这个类中设置监听器,但是我尝试过的所有方法都没有奏效。
有什么想法吗?
【问题讨论】:
-
我需要更好地解释我的问题,还是我问的太多了? ://
标签: android listview checkbox android-actionbar