导航抽屉选择的项目 textColor by Custom Adapter

Navigation drawer selected item textColor by Custom Adapter

请问如何更改导航抽屉中所选项目的文本颜色。

listview 的 android:textColor 安装后无法正常工作 appcompat_v7 22.1.1 所以我认为我无法使用可绘制的选择器进行更改。

我设法使用自定义适配器更改抽屉项目的默认颜色。

但这只能在初始化时将第一项设置为绿色。

我可以知道如何在选择项目时将所选项目的颜色(onclick)更改为我想要的颜色。

适配器:

public class NavigationDrawerAdapter extends ArrayAdapter<String> {
    private int mSelectedItem;
    private final Context context;
    private String[] sections;

    public NavigationDrawerAdapter(Context context, int resource, String[] objects) {
        super(context, resource, objects);
        this.context = context;
        sections = objects;
    }
    public int getSelectedItem() {
        return mSelectedItem;
    }
    public void setSelectedItem(int selectedItem) {
        mSelectedItem = selectedItem;
    }

    private String getMenuItemTitle(int position) {
        if (position >= 0 && position <= sections.length)
            return sections[position];
        return "Invalid section requested";
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //Get item TextView
        TextView view = (TextView) super.getView(position, convertView, parent);

        view.setText(getMenuItemTitle(position));

        if (position == mSelectedItem) {
            view.setTextColor(getContext().getResources().getColor(R.color.green));
        } else {
            view.setTextColor(getContext().getResources().getColor(R.color.white));        }
        return view;
    }
}

导航抽屉:

mDrawerListView.setAdapter(new NavigationDrawerAdapter(
                getActionBar().getThemedContext(),
                android.R.layout.simple_list_item_activated_1,
                new String[]{
                        getString(R.string.title_home),
                        getString(R.string.title_stamp),
                        getString(R.string.title_scanner),
                        getString(R.string.title_setting),}) {

        });
        mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
        return mDrawerListView;
..
...
private void selectItem(int position) {
        mCurrentSelectedPosition = position;

        if (mDrawerListView != null) {
            mDrawerListView.setItemChecked(position, true);
        }
        if (mDrawerLayout != null) {
            mDrawerLayout.closeDrawer(mFragmentContainerView);
        }
        if (mCallbacks != null) {
            mCallbacks.onNavigationDrawerItemSelected(position);
        }
    }

我找到了解决方案。

因为我已经扩展了适配器。 我只需要在点击项目进行更改时按位置调用和更改所选项目。

ListDrawer onItemClickListener:

 mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectItem(position);

                myadapter.setSelectedItem(position);

            }

并且来自自定义适配器:

public void setSelectedItem(int selectedItem) {
        mSelectedItem = selectedItem;
    }

    private String getMenuItemTitle(int position) {
        if (position >= 0 && position <= sections.length)
            return sections[position];
        return "Invalid section requested";
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //Get item TextView
      TextView view = (TextView) super.getView(position, convertView, parent);

        view.setText(getMenuItemTitle(position));

        if (position == mSelectedItem) {
            view.setTextColor(getContext().getResources().getColor(R.color.green));
        } else {
            view.setTextColor(getContext().getResources().getColor(R.color.normal));
        }

        return view;
    }

*适配器 class 有问题。

来源:samsaodev.com

final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            // Do something after some time
            for (int i = 0; i < recyclerViewDrawer1.getChildCount(); i++) {
                ImageView imageViewDrawerItemIcon = (ImageView) recyclerViewDrawer1
                        .getChildAt(i).findViewById(
                                R.id.imageViewDrawerItemIcon);
                TextView textViewDrawerItemTitle = (TextView) recyclerViewDrawer1
                        .getChildAt(i).findViewById(
                                R.id.textViewDrawerItemTitle);
                LinearLayout linearLayoutItem = (LinearLayout) recyclerViewDrawer1
                        .getChildAt(i).findViewById(R.id.linearLayoutItem);
                if (i == 0) {
                    imageViewDrawerItemIcon.setColorFilter(colorPrimary);

                    textViewDrawerItemTitle.setTextColor(colorPrimary);
                    linearLayoutItem
                            .setBackgroundColor(colorControlHighlight);
                } else {
                    imageViewDrawerItemIcon
                            .setColorFilter(textColorPrimary);

                    textViewDrawerItemTitle.setTextColor(textColorPrimary);
                    linearLayoutItem.setBackgroundColor(colorBackground);
                }
            }
        }
    }, 250);

无需解析导航抽屉的所有项目,只需创建颜色状态列表资源即可 See documentation here

drawer_text_selector.xml 在您的 res/color 文件夹中

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item 
     android:state_activated="false"
     android:color="@color/drawer_text_normal"/>
   <item
     android:state_pressed="true"
     android:color="@color/drawer_text_pressed"/>
   <item
     android:state_activated="true"
     android:color="@color/drawer_text_pressed"/>
</selector> 

然后在抽屉列表项中将 TextView 的 textColor 设置为:

<TextView
    android:id="@+id/drawerText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/drawer_text_selector" />