2015-12-24 135 views
1

我已閱讀關於此事的一些帖子,但仍然無法讓它工作。我想有一個複選框的列表視圖。這裏是我的activity_choose_songs.xmlandroid,列表視圖與複選框

<?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) { 

      } 
     }); 

然而,當我開始活動時,我試圖檢查它,它看起來像(屏幕處於按下狀態): enter image description here

我的方法有什麼問題?

+1

意思是,什麼是錯的,它沒有檢查? –

+0

我建議使用RecyclerView代替,你可以單獨設置項目和複選框的點擊監聽器,也可以查看這個答案http://stackoverflow.com/a/10529176/2146871 – JuliusScript

回答

2

我認爲問題是你的項目視圖中有一個LinearLayout。它不能把它的地位傳播給孩子們。

我想你有2個選項。

第一個是刪除LinearLayout。您的觀點目前尚未實施Checkable。當你刪除它CheckedTextView應該工作。

第二個選擇是使用CheckedLinearLayout,讓您的孩子有這樣的屬性:

android:duplicateParentState="true"` 

你可以找到CheckedLinearLayout網上實現。

+1

嗨,謝謝,真棒回答:) –

-1

您必須在您的on項目單擊偵聽器中執行此操作,示例代碼如下。

 lw.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      CheckBox check = (CheckBox) view.findViewById(R.id.checkedTextView); 

      check.toggle(); //check or un-check the checkbox 

      } 
     }); 
+0

切勿手動更改視圖的狀態。只有'getView'方法應該改變它。這將無法在重新創建視圖的滾動和配置更改中生存。 – tasomaniac

+0

@tasomaniac,是的,謝謝你的評論。我只是建議他,因爲他提到「無法通過單擊列表項檢查複選框」,我認爲這是最簡單的方法。我沒有想到保留狀態:)。 –