mikepenz / MaterialDrawer

The flexible, easy to use, all in one drawer library for your Android project. Now brand new with material 2 design.
https://mikepenz.dev
Apache License 2.0
11.67k stars 2.05k forks source link

Custom drawer item layout selected color #975

Closed elod91 closed 8 years ago

elod91 commented 8 years ago

Hey,

I've managed to create a custom drawer item, with a custom layout resource. In this layout there's a TextView with a selector as textColor. My problem is that the TextView remains selected after clicking on another drawer item.

Here's my menu item layout:

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

    <TextView
        android:id="@+id/drawer_menu_title"
        style="@style/drawer_menu_item" />

    <TextView
        android:id="@+id/drawer_menu_badge"
        style="@style/drawer_menu_badge" />
</RelativeLayout>

And as textColor for the drawer_menu_title the following selector is set:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/generalRed" android:state_selected="true" />
    <item android:color="@color/colorAccent" />
</selector>
mikepenz commented 8 years ago

@elod91 can you please also show the source of this item, i assume that you do not correctly handle the selected state within the bindView method

elod91 commented 8 years ago
package net.reea.shnapit.views;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.mikepenz.materialdrawer.model.BasePrimaryDrawerItem;
import com.mikepenz.materialdrawer.model.utils.ViewHolderFactory;

import net.reea.shnapit.R;

public class DrawerItem extends BasePrimaryDrawerItem<DrawerItem> {
    public static final String TYPE = "DRAWER_ITEM";

    private String title, badge;

    public DrawerItem withTitle(String title) {
        this.title = title;
        return this;
    }

    public DrawerItem withBadge(String badge) {
        this.badge = badge;
        return this;
    }

    @Override
    public ViewHolderFactory getFactory() {
        return new ItemFactory();
    }

    @Override
    public String getType() {
        return TYPE;
    }

    @Override
    public int getLayoutRes() {
        return R.layout.drawer_menu_item;
    }

    @Override
    public void bindView(RecyclerView.ViewHolder holder) {
        ViewHolder viewHolder = (ViewHolder) holder;

        if (title != null)
            viewHolder.title.setText(title);

        if (badge == null) {
            viewHolder.badge.setVisibility(View.GONE);
            return;
        }

        viewHolder.badge.setText(badge);
    }

    public static class ItemFactory implements ViewHolderFactory<ViewHolder> {
        public ViewHolder factory(View v) {
            return new ViewHolder(v);
        }
    }

    private static class ViewHolder extends BaseViewHolder {
        private TextView title, badge;

        public ViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.drawer_menu_title);
            badge = (TextView) view.findViewById(R.id.drawer_menu_badge);
        }
    }
}
mikepenz commented 8 years ago

You have to add the following:

//set the item selected if it is
viewHolder.itemView.setSelected(isSelected());

this makes sure the state of the view is correctly set depending on the item

(in v5.0.0 this will be handled be the underlaying FastAdapter)

elod91 commented 8 years ago

Thanks, it works.

JasiekRadzik commented 6 years ago

Hello, I'm quite new to android developing and I very appreaciate what you have done here. I started writing my app in Kotlin and I was wondering how to write simple, custom item in my Drawer.

I have a problem with creating CustomItem class. Could you please help me with that?