NavigationDrawer RecyclerView selected items change color
I am trying to change the textColor in my NavigationDrawer when an item is selected. I am using RecyclerView as my scroll layout. This is based on the tutorial:
http://www.androidhive.info/2015/04/android-getting-started-with-material-design/
Anyone can suggest a solution
Thanks in Advance.
source to share
Define a static int in the NavigationDrawerAdapter class to display the selected item
In NavigationDrawerAdapter.java
public class NavigationDrawerAdapter extends RecyclerView.Adapter<NavigationDrawerAdapter.MyViewHolder> {
public static int selected_item = 0;
...
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
NavDrawerItem current = data.get(position);
holder.title.setText(current.getTitle());
if(position == selected_item)
{
holder.title.setTextColor(Color.RED);
}
else
{
holder.title.setTextColor(Color.BLACK);
}
}
...
}
In FragmentDrawer.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new ClickListener() {
@Override
public void onClick(View view, int position) {
NavigationDrawerAdapter.selected_item = position;
recyclerView.getAdapter().notifyDataSetChanged();
drawerListener.onDrawerItemSelected(view, position);
mDrawerLayout.closeDrawer(containerView);
}
...
}));
...
}
source to share
I figured out how to change the text color as well as the color of the whole view.
I have updated FragmentDrawer.java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflating view layout
View layout = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
recyclerView = (RecyclerView) layout.findViewById(R.id.drawerList);
adapter = new NavigationDrawerAdapter(getActivity(), getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new ClickListener() {
@Override
public void onClick(View view, int position) {
view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
((TextView) view.findViewById(R.id.title)).setTextColor(getResources().getColor(R.color.material_blue_grey_800));
drawerListener.onDrawerItemSelected(view, position);
mDrawerLayout.closeDrawer(containerView);
}
@Override
public void onLongClick(View view, int position) {
}
}));
return layout;
}
What I basically did was the onClick () method of the TouchListener.
view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
This will change the background color of the view
Likewise, since I am referencing the View, I can get a link to the TextView that is being used inside the Drawer layout.
((TextView) view.findViewById(R.id.title)).setTextColor(getResources().getColor(R.color.material_blue_grey_800));
source to share
You can do it:
public class MainActivity extends AppCompatActivity {
.......
adapter.setOnItemClickLister(new MenuAdapter.OnItemSelecteListener() {
View selectedView;
@Override
public void onItemSelected(View v, int position) {
if(selectedView != null)
selectedView.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.defaultColor));
v.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.orange));
selectedView = v;
}
});
}
source to share