我正在使用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视图)
谢谢大家。
答案 0 :(得分:0)
由于在state_focus="true"
上运行app,因此可能会选择它。
答案 1 :(得分:0)
android:textColor
即可
醇>