Android抽屉选择器state_activated不起作用,并且不会对所选项目进行着色

时间:2017-09-22 13:09:10

标签: android navigation-drawer selector drawerlayout android-navigation-drawer

我正在努力解决这个可怕的错误。似乎一切都可以用来代替android:state_activated。我试图将<item>置于不同的顺序,但它没有帮助。此外,当应用程序启动时,抽屉中默认选择的片段选项也没有着色(因为它是默认版本)...

这是我的selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/colorPirmaryDarkerGray"
          android:state_activated="true"/>
    <item android:drawable="@color/colorPirmaryDarkerGray"
          android:state_pressed="true"/>
    <item android:drawable="@android:color/transparent"/>
</selector>

这是一个项目的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent">


    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:id="@+id/itemIcon"/>
    <TextView
            android:id="@android:id/text1"
            android:duplicateParentState="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="32sp"
            />

</LinearLayout>

这是抽屉布局:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:choiceMode="singleChoice"
          android:paddingTop="10dp"
          android:divider="@android:color/transparent"
          android:dividerHeight="0dp"
          android:background="#cccc"
          android:drawSelectorOnTop="false"
          android:listSelector="@drawable/my_drawer_selector"
          tools:context="com.myPackage.app.NavigationDrawerFragment"/>

这是onCreateView(我在这里添加了我自己的适配器,其余的是默认的)

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mDrawerListView = (ListView) inflater.inflate(
                R.layout.drawer_dashboard, container, false);
        mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectItem(position);
            }
        });
        String[] items = {
                getString(R.string.Contacts),
                getString(R.string.Data)
        };

        Integer[] images = {
                R.drawable.ic_contacts,
                R.drawable.ic_data
        };
        mDrawerListView.setAdapter(new MyCustomDrawerListAdapter(getActivity(), items, images));

//This should indicate the first coloring but it doesn't
        mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
        return mDrawerListView;
    }

最后,我的MyCustomDrawerListAdapter

private class MyCustomDrawerListAdapter extends ArrayAdapter<String>{
        private final Activity context;
        private final String[] itemname;
        private final Integer[] imgid;

        public MyCustomDrawerListAdapter(Activity context, String[] itemname, Integer[] imgid){
            super(context, R.layout.my_menu_drawer_item, itemname);
            this.context = context;
            this.itemname = itemname;
            this.imgid = imgid;
            for (Integer anImgid : imgid) {
                System.out.println(" image id " + anImgid);
            }
        }
        public View getView(int position,View view,ViewGroup parent) {
            if (view == null) {
                view = getActivity().getLayoutInflater().inflate(R.layout.my_menu_drawer_item, null, false);
            }

            ((TextView) view.findViewById(android.R.id.text1))
                    .setText(getItem(position));
            ((ImageView) view.findViewById(R.id.itemIcon))
                    .setImageResource(imgid[position]);
            return view;

        }

    }

2 个答案:

答案 0 :(得分:1)

在api 21设备上运行时遇到了一些问题。

我解决问题的方法是:

ListView背景中设置高亮颜色(在我的情况下为colorAccent)。

android:background="@color/colorAccent"

并将ListView选择器设置为:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
        <solid android:color="@color/colorAccent"/>
</shape>

对于抽屉项目,将LinearLayout顶级容器背景设置为

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/transparent" android:state_selected="true"/>
    <item android:drawable="@android:color/transparent" android:state_pressed="true"/>
    <item android:drawable="@android:color/transparent" android:state_activated="true"/>
    <item android:drawable="@color/colorMenuBackground"/>
</selector>

就我而言:

<color name="colorMenuBackground">#FFFFFF</color>

要选择第一项,请在主要活动的onPostCreate()中运行:

setItemChecked(0,true); 
列表视图上的

(0是项目的位置)

实际上,这就是我使用同样与Hamburguer菜单按钮同步的方法:

@Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        actionBarDrawerToggle.syncState();
        drawerList.setItemChecked(1,true);

    }

答案 1 :(得分:-1)

您需要在res文件夹中创建一个新的包名称颜色,在颜色包内创建my_drawer_selector文件

然后将此文件与

一起使用
"@color/my_drawer_selector"

这需要像这样:

enter image description here

nav_item_state_list是你的my_drawer_selector