首先,我知道有很多关于同一主题的问题,但我尝试了很多解决方案而且我没有实现它。
我有一个DrawerLayout
的应用,我想更改代码选择的项目,选项更改但项目不突出显示,我不知道为什么。它在版本> = 2.3(api 9)中工作
如果我手动点击它可以工作
项目的布局是:我将android:focusableInTouchMode
设置为false,因为我读到所选项目如果启用则不起作用。
我太过背景了。
choiceMode:drawerList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llItem"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@drawable/list_selector_holo_light"
android:focusableInTouchMode="false">
<ImageView
android:id="@+id/iconoMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:paddingLeft="5dp"
android:paddingRight="12dp"/>
<TextView
android:id="@+id/etTitulo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/menu_item"/>
</LinearLayout>
xml文件到后台:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:drawable="@android:color/transparent" />
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/list_selector_disabled_holo_light" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="@drawable/list_selector_disabled_holo_light" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition_holo_light" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition_holo_light" />
<item android:state_focused="true" android:drawable="@drawable/list_focused_holo" />
<item android:state_focused="false" android:state_selected="true" android:drawable="@drawable/list_pressed_holo_light"/>
</selector>
然后我用这段代码点击项目上的点击(主页是选中的选项):
drawerList.performItemClick(drawerList, home, drawerList.getItemIdAtPosition(home));
drawerList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view,
int position, long id) {
changeOption(position, true);
view.setSelected(true);
}
});
private void changeOption(int position, Boolean cerrar) {
fragment = new Home();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
tituloSeccion = ((MenuLateralItem) opcionesMenu[position])
.getTitulo();
getSupportActionBar().setTitle(tituloSeccion);
if ((cerrar) && (!isDrawerLocked)) {
drawerLayout.closeDrawer(drawerList);
}
}
谢谢!对不起我的英语。
答案 0 :(得分:2)
一个简单的解决方法是在DrawerLayout中保存适配器中所选项的值。然后当你进行convertView时,检查当前项是否是所选项并相应地设置其自定义背景
public class YourDrawerAdapter extends BaseAdapter {
int selectedItem;
// all your code goes here
//call this method in the onItemClick listener of the listview
public void setSelectedItem(int position) {
selectedItem = position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//the code for the generating the view..
//check here if the current item is the selected one
if(position == selectedItem) {
//set the selected layout for listItem
} else {
//set the normal layout for listItem
}
return convertView;
}
}