![](/img/trans.png)
[英]How to change the background color of selected item in RecyclerView
[英]RecyclerView change selected item background and text color
我正在使用RecyclerView.
我试图使用selector.xml
文件更改项目单击中的背景颜色。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/trains_vagon_selected_departure" android:state_pressed="true" />
<item android:drawable="@drawable/trains_vagon_selected_departure" android:state_focused="true" />
<item android:drawable="@drawable/trains_vagon_selected_white" />
</selector>
适配器代码:
public class TrainsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public ArrayList<Train> trains;
public Context context;
private LayoutInflater inflater;
private String statusString = "";
public TrainsAdapter(Context context, ArrayList<Train> trains, String statusString) {
this.context = context;
this.trains = trains;
inflater = LayoutInflater.from(context);
this.statusString = statusString;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.trains_adapter_item, parent, false);
GenericViewHolder holder = new GenericViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof GenericViewHolder) {
final GenericViewHolder genericViewHolder = (GenericViewHolder) holder;
genericViewHolder.vagoncontainer.removeAllViews();
Train train = trains.get(position);
if (statusString.equals("departure"))
genericViewHolder.statusImageView.setImageResource(R.mipmap.departure_icon);
else
genericViewHolder.statusImageView.setImageResource(R.mipmap.return_icon);
ArrayList<Vagons> vagons = train.getVagons();
for (int i = 0; i < vagons.size(); i++) {
if (vagons.get(i).getEnable().equals("1")) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
final LinearLayout vagonView = (LinearLayout) layoutInflater.inflate(R.layout.trains_vagon_child, null);
genericViewHolder.vagoncontainer.setBackgroundColor(Color.WHITE);
RelativeLayout relativeLayout = (RelativeLayout) vagonView.findViewById(R.id.selected_layout);
if (statusString.equals("departure"))
relativeLayout.setBackgroundResource(R.drawable.recyclerview_selected_departure);
else
relativeLayout.setBackgroundResource(R.drawable.recyclerview_selected_returned);
TextView vagonClassName = (TextView) vagonView.findViewById(R.id.vagon_class_name);
TextView vagonClassPrice = (TextView) vagonView.findViewById(R.id.vagon_class_price);
vagonClassName.setText(vagons.get(i).getName());
vagonClassPrice.setText(vagons.get(i).getAmount());
genericViewHolder.vagoncontainer.addView(vagonView);
}
}
genericViewHolder.trainTime.setText(train.getDeparture());
}
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemCount() {
return trains.size();
}
public Train getItem(int position) {
return trains.get(position);
}
@Override
public int getItemViewType(int position) {
return position;
}
public class GenericViewHolder extends RecyclerView.ViewHolder {
public TextView trainTime;
public LinearLayout vagoncontainer;
private ImageView statusImageView;
public GenericViewHolder(View view) {
super(view);
trainTime = (TextView) view.findViewById(R.id.train_time);
statusImageView = (ImageView) view.findViewById(R.id.departure_next_img);
vagoncontainer = (LinearLayout) view.findViewById(R.id.vagon_container);
}
}
}
我有两个问题。
1)当我在RecyclerView
运行第一个元素时,也会自动选择它。 为什么? 我该如何解决?
2)是否可以在选择器xml
文件中更改文本颜色? 我知道如何按位置更改简单的recyclerView的背景和文本颜色,但是此刻每个按位置的元素都有一些数组,我将其添加到我的位置,然后尝试更改子视图的背景和文本颜色(请参阅我的代码。我的意思是vagonView视图)
感谢大家。
它可能是由于在运行应用程序时由于state_focus="true"
而被选中的。
android:textColor
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.