简体   繁体   中英

How to access fragment ArrayList when using adapter?

I'm working on my code to generate the list of colors to store it in the ColorList array. Now I want to get access to the color from the ColorList array, but I dont know how I can get access to the colorList array when I have created the list in the fragement while I am using the adapter.

Here is my InboxFragement code:

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

Here is the inboxApadater code:

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

Can you please show me an example how I can get access to the fragment colorList array when I am using the adapter to get the colorList array??

Thank you.

Edit: Here is my adapter:

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

adapter

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

InboxFragement

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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