以下是一個針對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>
我是否錯過了?按下狀態工作正常,只是沒有激活。的DrawerAdapter
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" >
<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;
}
}
什麼是'DrawerAdapter.getView'方法的根視圖。 – Blaz
@blazsolar我添加了上面的代碼,.. .. – KickingLettuce