[英]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.