2014-02-19 65 views
0

以下是一個針對Android 4.1+的應用程序。 我想在列表視圖上的活動狀態。我在其他應用程序中使用代碼,我在哪裏可以正常工作。不知道這裏有什麼區別(除了API版本更高)。Android ListView CHOICE_MODE_SINGLE不會設置

我有這樣的Java:

DrawerAdapter lAdapter = new DrawerAdapter(this, modeList); 
    mDrawerList.setAdapter(lAdapter); 
    mDrawerList.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
    mDrawerList.setItemChecked(0, true); 

    mDrawerList.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> arg0, View v, int pos, 
       long id) { 

      mDrawerList.setItemChecked(pos, true); 
      } 

抽屜佈局:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <FrameLayout 
     android:id="@+id/main_frag" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <ListView 
     android:id="@+id/drawer_list" 
     android:layout_width="250dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:cacheColorHint="#ececec" 
     android:background="@drawable/tile_repeat" 
     android:fadingEdge="none" 
     android:listSelector="@drawable/nav_selector" 
     android:scrollbars="none" /> 

</android.support.v4.widget.DrawerLayout> 

選擇

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 


    <!-- pressed --> 
    <item android:drawable="@drawable/nav_press" android:state_activated="false" android:state_pressed="true"/> 

    <!-- CHECKED --> 
    <item android:drawable="@drawable/nav_active" android:state_activated="true" android:state_pressed="false" /> 

</selector> 

nav_active:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" > 
    <solid android:color="#cecece" /> 
</shape> 

我是否錯過了?按下狀態工作正常,只是沒有激活。的DrawerAdapterrow_drawer_layout.xml

<?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="vertical" > 

     <TextView 
      android:id="@+id/tvMenuItem" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="TextView" 
      android:textColor="#005348"/> 

    </LinearLayout> 

根視圖DrawerAdapter:

public class DrawerAdapter extends ArrayAdapter<String> { 
private final Context context; 

public DrawerAdapter(Context context, String[] sizeList) { 
    super(context, 0, sizeList); 
    this.context = context; 
} 


static class ViewHolder { 
    public TextView tv1; 
    public TextView tv2; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    View rowView = convertView; 

    if (rowView == null) { 
     LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
     rowView = inflater.inflate(R.layout.row_drawer_layout, null, true); 

     holder = new ViewHolder(); 
     holder.tv1 = (TextView) rowView.findViewById(R.id.tvMenuItem); 
     rowView.setTag(holder); 
    } else { 
     holder = (ViewHolder) rowView.getTag(); 
    } 

    String s = getItem(position); 
    holder.tv1.setText(s); 

    return rowView; 

} 

}

+0

什麼是'DrawerAdapter.getView'方法的根視圖。 – Blaz

+0

@blazsolar我添加了上面的代碼,.. .. – KickingLettuce

回答

2

ListView項目的根視圖應該實現Checkable接口。在你的情況下,是LinearLayout女巫確實注意到工具Checkable。您應該在佈局中使用CheckedTextView

它應該看起來像這樣。

<?xml version="1.0" encoding="utf-8"?> 
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/tvMenuItem" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="TextView" 
     android:textColor="#005348" 
     android:background="@drawable/checkable_item"/> 

如果您希望您的項目中更復雜的佈局可以擴展Android的佈局之一,並添加Checkable的接口吧。 Here是RelativeLayout``

的`一個例子

要更改檢查項目的背景你應該選擇使用checked狀態:

checkable_item.xml 
<selector> 
    ... 
    <item android:state_checked="true" android:drawable="@drawable/some_drawable" /> 
    ... 
</selector> 
+0

那麼,我不是想改變'TextView',而是行的背景顏色,所以這不適用呢? – KickingLettuce

+1

要更改已檢查項目的背景,您應該將'android:background'設置爲'selector',並將'android:state_checked =「true」'設置爲您想要的顏色。 – Blaz

0

要解決,這就是我所做的:

我加android:background="@drawable/nav_selector"row_drawer_layout.xml的根LinearLayout;現在該行在用戶選擇時保持活動狀態。

我感謝@blazsolar的幫助。他的第二個評論使我朝着正確的方向前進。

0

對不起,爲時已晚,但我對此非常困難。您所要做的就是將android:background="?android:attr/activatedBackgroundIndicator"添加到row_drawer_layout.xml文件中。 最後,它應該是這樣的:

<?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="vertical" 
    android:background="?android:attr/activatedBackgroundIndicator"> 

    <TextView 
     android:id="@+id/tvMenuItem" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView" 
     android:textColor="#005348"/> 

</LinearLayout> 

這是正確的方式,因爲它使用的樣式,這意味着當你設置的自定義樣式會影響所定義的activatedBackgroundIndicator這樣做所有列表視圖(如果沒有另行指定)。