问题
I am trying to change the textColor in my NavigationDrawer when item is selected.I am using RecyclerView as my swipe layout. This is based on the tutorial:
http://www.androidhive.info/2015/04/android-getting-started-with-material-design/
Anyone can suggest the solution
Thanks in Advance.
回答1:
Define a static int in NavigationDrawerAdapter class to represent 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);
}
...
}));
...
}
回答2:
I have figured out, how to change text color as well as the color of the entire view.
I've updated your 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've basically done is, in the onClick() method of TouchListener.
view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
This will change the background color of the view
Similarly since I've reference to the view, I could get reference to the TextView which is used inside layout of Drawer.
((TextView) view.findViewById(R.id.title)).setTextColor(getResources().getColor(R.color.material_blue_grey_800));
回答3:
You can do this:
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;
}
});
}
来源:https://stackoverflow.com/questions/31045563/navigationdrawer-recyclerview-selected-items-change-color