使用适配器时如何访问片段ArrayList



我正在编写代码,以生成颜色列表并将其存储在ColorList数组中。现在我想从ColorList数组中访问颜色,但我不知道在使用适配器时,当我在片段中创建了列表时,如何访问ColorList数组。

这是我的收件箱碎片代码:

private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
mInboxAdapter = new InboxAdapter(getActivity(), mInbox);
int itemsCount = recyclerView.getChildCount();
for (int i = 0; i < itemsCount; i++) {
View view = recyclerView.getChildAt(i);
GradientDrawable gradientDrawable = (GradientDrawable) view.findViewById(R.id.tvIcon).getBackground();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
int intColor = gradientDrawable.getColor().getDefaultColor();
String hexColor = Integer.toHexString(intColor).substring(2);
colors = "#" + hexColor;
colorList.add(index1, colors);
index1++;
}
}
recyclerView.setAdapter(mInboxAdapter);
GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 1);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
}

这是收件箱的Adadater代码:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
inbox inbox = mInboxes.get(position);
holder.subject.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v) {
int idx = holder.getAdapterPosition();
String id = mInboxes.get(idx).getId();
String color = color_list.get(idx).toString();
openEmailActivity(mailbox, id, color, short_name, subject1, from, from_email, datetime, isImportant);
}
});

你能给我举一个例子吗?当我使用适配器获取colorList数组时,我如何访问fragment colorList数组??

谢谢。

编辑:这是我的适配器:

public class InboxAdapter extends RecyclerView.Adapter<InboxAdapter.ViewHolder> {
private static final String LOG_TAG = InboxAdapter.class.getSimpleName();
private boolean reverseAllAnimations = false;
private SparseBooleanArray selectedItems;
public List<inbox> mInboxes;
public ArrayList<String> colorList;
public InboxAdapter(Context mContext, List<inbox> inboxes, List<String> colorList) {
this.mContext = mContext;
mInboxes = inboxes;
}
@Override
public InboxAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View contactView = inflater.inflate(R.layout.recyclerview_mail_item, parent, false);
ViewHolder viewHolder = new ViewHolder(contactView);
return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
inbox inbox = mInboxes.get(position);
holder.subject.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v) {
int idx = holder.getAdapterPosition();
String id = mInboxes.get(idx).getId();
String color = color_list.get(idx).toString();
openEmailActivity(mailbox, id, color, short_name, subject1, from, from_email, datetime, isImportant);
}
});

public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView tvIcon;
public TextView from;
public TextView subject;
public TextView message;
public TextView time;
public ImageView iconImp;
public RelativeLayout layout1;
public ImageView attachment;
public Integer color1;
public ViewHolder(View itemView) {
super(itemView);
tvIcon = itemView.findViewById(R.id.tvIcon);
from = itemView.findViewById(R.id.from);
subject = itemView.findViewById(R.id.subject);
message = itemView.findViewById(R.id.message);
time = itemView.findViewById(R.id.time);
iconImp = itemView.findViewById(R.id.icon_star);
layout1 = itemView.findViewById(R.id.layout1);
attachment = itemView.findViewById(R.id.attachment);
}
}

private int getRandomMaterialColor(String typeColor) {
int returnColor = Color.GRAY;
int arrayId = mContext.getResources().getIdentifier("mdcolor_" + typeColor, "array", mContext.getPackageName());
if (arrayId != 0) {
TypedArray colors = mContext.getResources().obtainTypedArray(arrayId);
int index = (int) (Math.random() * colors.length());
returnColor = colors.getColor(index, Color.GRAY);
colors.recycle();
}
return returnColor;
}

public void openEmailActivity(String mailbox, String id, String color, String short_name, String subject1, String from, String from_email, String datetime, String isImportant) {
Intent intent = new Intent(mContext, MainActivity5.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("mailbox", mailbox);
intent.putExtra("id", id);
intent.putExtra("bg_color", color);
intent.putExtra("short_name", short_name);
intent.putExtra("subject", subject1);
intent.putExtra("from_sender", from);
intent.putExtra("from_email", from_email);
intent.putExtra("datetime", datetime);
intent.putExtra("is_important", isImportant);
mContext.startActivity(intent);
}

适配器

public class InboxAdapter extends RecyclerView.Adapter<InboxAdapter.ViewHolder> {
private static final String LOG_TAG = InboxAdapter.class.getSimpleName();
private boolean reverseAllAnimations = false;
private SparseBooleanArray selectedItems;
public List<inbox> mInboxes;
public ArrayList<String> mColorList;
public InboxAdapter(Context mContext, List<inbox> inboxes, List<String> colorList) {
this.mContext = mContext;
mInboxes = inboxes;
mColorList = colorList;
}
@Override
public InboxAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View contactView = inflater.inflate(R.layout.recyclerview_mail_item, parent, false);
ViewHolder viewHolder = new ViewHolder(contactView);
return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
inbox inbox = mInboxes.get(position);
holder.subject.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v) {
int idx = holder.getAdapterPosition();
String id = mInboxes.get(idx).getId();
String color = mColorList.get(idx);
openEmailActivity(mailbox, id, color, short_name, subject1, from, from_email, datetime, isImportant);
}
});
}

收件箱碎片

private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
int itemsCount = recyclerView.getChildCount();
for (int i = 0; i < itemsCount; i++) {
View view = recyclerView.getChildAt(i);
GradientDrawable gradientDrawable = (GradientDrawable) view.findViewById(R.id.tvIcon).getBackground();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
int intColor = gradientDrawable.getColor().getDefaultColor();
String hexColor = Integer.toHexString(intColor).substring(2);
colors = "#" + hexColor;
colorList.add(index1, colors);
index1++;
}
}
mInboxAdapter = new InboxAdapter(getActivity(), mInbox, colorList);
recyclerView.setAdapter(mInboxAdapter);
GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 1);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
}

最新更新