現在Recyclerビューがあります。ユーザーがビューからボタンをクリックすると、新しいアクティビティに移動します。新しいアクティビティのアクションバーのタイトルをリサイクラビューの色のタイトルに設定したいと思います。したがって、以下の私の例では、「London Overground」のタイトルをRecyclerの表示アイテムと同じ色に変更したいと考えています。 RecyclerViewでユーザーが選択したオプションに応じてActionBarタイトルの色を設定します。
私はonBindViewHolderメソッドにあるRGBカラー値を保持する配列を持っています。私は正しい色ではなく、名前を渡すことができます。以下の私のコードを見てください。
LineAdapter
public class LineAdapter extends RecyclerView.Adapter<LineAdapter.LineViewHolder> {
private List<Line> lineList;
public LineAdapter(List<Line> lineList) {
this.lineList = lineList;
}
@Override
public LineViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.line_list_row, parent, false);
return new LineViewHolder(itemView);
}
@Override
public void onBindViewHolder(LineViewHolder holder, int position) {
holder.lineName.setText(lineList.get(position).getLineName());
holder.lineStatus.setText(lineList.get(position).getLineStatus());
Integer[] colors = {Color.rgb(179, 99, 5),Color.rgb(227, 32, 23),Color.rgb(255, 211, 0),Color.rgb(0, 120, 42),Color.rgb(0, 164, 167),Color.rgb(243, 169, 187),Color.rgb(160, 165, 169),Color.rgb(238, 124, 14),Color.rgb(155, 0, 86),Color.rgb(0, 0, 0),Color.rgb(0, 54, 136),Color.rgb(113, 86, 165),Color.rgb(0, 152, 212),Color.rgb(149, 205, 186)};
holder.lineName.setBackgroundColor(colors[position]);
}
@Override
public int getItemCount() {
return lineList.size();
}
public class LineViewHolder extends RecyclerView.ViewHolder {
public TextView lineName;
public TextView lineStatus;
public LineViewHolder(View view) {
super(view);
lineName = (TextView) view.findViewById(R.id.lineName);
lineStatus = (TextView) view.findViewById(R.id.lineStatus);
}
}
}
私のonclickリスナー
recyclerView.addOnItemTouchListener(new touchListener(getApplicationContext(), recyclerView, new clickListener() {
@Override
public void onClick(View view, int position) {
String a = (lineList.get(position).getLineName());
// Toast.makeText(line_activity.this, a, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(line_activity.this, line_info.class);
intent.putExtra("string", a);
startActivity(intent);
}
}
)
);