当我选择项目时,我正在尝试更改NavigationDrawer中的textColor。我使用RecyclerView作为我的滑动布局。这是基于教程:
http://www.androidhive.info/2015/04/android-getting-started-with-material-design/
任何人都可以建议解决方案
先谢谢。
答案 0 :(得分:7)
在NavigationDrawerAdapter类中定义静态int以表示所选项
在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);
}
}
...
}
在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);
}
...
}));
...
}
答案 1 :(得分:1)
我已经想通了,如何更改文本颜色以及整个视图的颜色。
我已更新 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;
}
我基本上做的是,在TouchListener的onClick()方法中。
view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
这将更改视图的背景颜色
同样,因为我引用了视图,所以我可以参考在Drawer布局中使用的 TextView 。
((TextView) view.findViewById(R.id.title)).setTextColor(getResources().getColor(R.color.material_blue_grey_800));
答案 2 :(得分:0)
你可以这样做:
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;
}
});
}