我已閱讀關於此事的一些帖子,但仍然無法讓它工作。我想有一個複選框的列表視圖。這裏是我的activity_choose_songs.xml
:android,列表視圖與複選框
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/songs_to_choose"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/sd_cards_playlist"
android:textStyle="bold"
android:gravity="center"
android:textSize="30sp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
/>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:listSelector="@drawable/list_selector"
android:divider="#242424"
android:dividerHeight="1dp"
android:layout_below="@+id/songs_to_choose">
</ListView>
</RelativeLayout>
而這裏的特定項目的佈局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="5dp"
android:background="@drawable/list_selector">
<CheckedTextView
android:id="@+id/checkedTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:padding="10dp"
android:gravity="center"
android:drawableStart="?android:attr/listChoiceIndicatorMultiple"
android:textColor="#f3f3f3" />
</LinearLayout>
下面是onCreate()
方法提取物,我想顯示該列表視圖的活動:
ListAdapter adapter = new SimpleAdapter(getApplicationContext(),chosenSongsListData,
R.layout.playlist_checked_item,new String[]{"songTitle"},new int[]{R.id.checkedTextView});
setListAdapter(adapter);
ListView lw = getListView();
lw.setItemsCanFocus(false);
lw.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
lw.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
然而,當我開始活動時,我試圖檢查它,它看起來像(屏幕處於按下狀態):
我的方法有什麼問題?
意思是,什麼是錯的,它沒有檢查? –
我建議使用RecyclerView代替,你可以單獨設置項目和複選框的點擊監聽器,也可以查看這個答案http://stackoverflow.com/a/10529176/2146871 – JuliusScript