所以我在<{>>材料设计后RecyclerView
使用了Drawerlayout
。
我想要的是当我按下RecyclerView
中的特定项目时,它的背景颜色和文字颜色将以我想要的方式突出显示。但是,当我单击每个项目时,它仅在该特定时刻突出显示,并且在抽屉关闭后不保持其状态。那么我该如何创建一个像 youtube 导航抽屉这样的选择器,它在选中时会保留其高亮属性。感谢。
所以这是RecyclerView
中每个项目的布局:
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:background="@drawable/nav_drawer_row_selector"
>
<ImageView
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_centerVertical="true"
android:id="@+id/navIcon" />
<TextView
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textStyle="bold"
android:layout_marginLeft="72dp"
android:text="Sample Text"
/>
</RelativeLayout>
这是我的选择:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/primaryColorDark"
android:state_pressed="true" />
<item android:drawable="@color/primaryColorDark"
android:state_activated="true" />
<item android:drawable="@android:color/transparent"
android:activated="false" />
</selector>
答案 0 :(得分:3)
所以这是我解决问题的方法。我是在RecyclerView.Adapter的onBindViewHolder上做的。
这是我的整个适配器类:
public class NavDrawerAdapter extends RecyclerView.Adapter<NavDrawerAdapter.MyViewHolder>{
List<NavDrawerItem> data = Collections.emptyList();
List<MyViewHolder> holders = new ArrayList<MyViewHolder>();
private LayoutInflater inflater;
private Context context;
public NavDrawerAdapter(Context context, List<NavDrawerItem> data) {
this.context = context;
inflater = LayoutInflater.from(context);
this.data = data;
}
public void delete(int position) {
data.remove(position);
notifyItemRemoved(position);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.nav_drawer_row, parent, false);
MyViewHolder holder = new MyViewHolder(view);
//storing holders instance to an array
holders.add(holder);
return holder;
}
//Set the contents of different views of the row.
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
final NavDrawerItem current = data.get(position);
holder.title.setText(current.getTitle());
holder.img.setImageResource(current.getIcon());
holder.root.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(current.isShowNotify()){
current.setShowNotify(false);
holder.root.setBackgroundColor(context.getResources().getColor(R.color.white));
}else{
current.setShowNotify(true);
holder.root.setBackgroundColor(context.getResources().getColor(R.color.primaryColor));
//looping through MyViewHolder instances and removing the background color if not selected
for(int i= 0; i < holders.size(); i++){
if(i != position){
holders.get(i).root.setBackgroundColor(context.getResources().getColor(R.color.white));
}
}
}
}
});
//Log.d("JC", "onBindViewHolder() position : " + position);
}
@Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView title;
ImageView img;
RelativeLayout root;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
img = (ImageView) itemView.findViewById(R.id.navIcon);
root = (RelativeLayout) itemView.findViewById(R.id.root);
}
}
}
希望这会对某人有所帮助。
答案 1 :(得分:2)
这是保存选择状态的pesudo代码。实现取决于适配器使用的数据。原则上,您需要将选择存储在成员中(在我的示例中为isActive
),并为需要突出显示并单击的行的根视图元素指定一个id。
class Item {
...
boolean isActive;
}
class View extends RecyclerView.ViewHolder {
ImageView navIcon;
TextView title;
RelativeLayout root;
public ViewHolder(View itemView) {
...
root = (RelativeLayout)itemView.findViewByid(R.id.root);
}
}
class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
List<Item> items;
...
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Item item = items.get(position);
holder.root.setOnClickListener(new OnClickListener(){
item.isActive = !item.isActive;
});
if(item.isActive){
holder.root.setBackgroundColor(R.color.red);
}else{
holder.root.setBackgroundColor(R.color.transparent);
}
}
}