我正在努力定制我的列表视图。
我更改了listView中的TextViews的背景。
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_list_item_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#000"
android:background="@drawable/list_selector_orange"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>
然后我在drawable文件夹中创建了list_selector_orange
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/selector_press_orange"/>
<item android:state_selected="true" android:drawable="@drawable/selector_focus_orange" />
如果我按下按钮,它会起作用:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="270"
android:endColor="#FE9A2E"
android:startColor="#FE9A2E" />
</shape>
但是当前选择的项目无论如何都没有颜色......
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:endColor="#FF8000"
android:startColor="#FF8000" />
</shape>
编辑:这是listview的代码 XML文件... ListView是导航抽屉的一部分
<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/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"
android:listSelector="@drawable/list_selector_orange" />
这是监听器的代码
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
// update the main content by replacing fragments
if(fragment != null){
//fragmentManager.beginTransaction().remove(fragment).commit();
}
Bundle args;
switch(position){
case 1:
fragment = new de.sebspr.app08.viewpager.PagerFragment();
args = new Bundle();
args.putInt(de.sebspr.app08.viewpager.PagerFragment.ARG_PLAYER_NUMBER, position);
fragment.setArguments(args);
break;
case 2:
fragment = new de.sebspr.app08.viewpager.PagerFragment();
args = new Bundle();
args.putInt(de.sebspr.app08.viewpager.PagerFragment.ARG_PLAYER_NUMBER, position);
fragment.setArguments(args);
break;
case 4:
fragment = new de.sebspr.app08.halle.FragHalle();
break;
}
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mListTitles[position].getTitle());
mDrawerLayout.closeDrawer(mDrawerList);
}
我刚刚选择了自定义适配器,因为我希望有节标题。所以我创建了一个可点击的项目和一个不是
的项目public View getView(int position, View convertView, ViewGroup parent){
Item item = items[position];
ViewHolder holder;
if(convertView == null){
holder = new ViewHolder();
if(item.isSection()){
convertView = inflater.inflate(R.layout.drawer_list_section, null);
holder.txt = (TextView) convertView.findViewById(R.id.drawer_list_section_txt);
convertView.setLongClickable(false);
convertView.setClickable(false);
convertView.setOnClickListener(null);
tFace = Typeface.createFromAsset(context.getAssets(),
"fonts/Roboto-BoldCondensed.ttf");
holder.txt.setTypeface(tFace);
convertView.setTag(holder);
} else {
convertView = inflater.inflate(R.layout.drawer_list_item, null);
holder.txt= (TextView) convertView.findViewById(R.id.drawer_list_item_txt);
tFace = Typeface.createFromAsset(context.getAssets(),
"fonts/Roboto-Regular.ttf");
holder.txt.setTypeface(tFace);
convertView.setTag(holder);
}
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txt.setText(item.getTitle());
return convertView;
}
答案 0 :(得分:1)
从TextView中删除此行:
android:background="@drawable/list_selector_orange"
并将其放到ListView
<ListView
.....
android:listSelector"@drawable/list_selector_orange"
.....
/>
之后,您选择的项目将改变颜色。
希望我帮助你。
答案 1 :(得分:1)
将android:duplicateParentState="true"
添加到TextView,这样它就会获得其父级的状态。