繁体   English   中英

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

[英]How to access fragment ArrayList when using adapter?

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

这是我的 InboxFragment 代码:

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);
}

这是收件箱Apadate代码:

    @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 数组时,如何访问片段 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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM