本文介绍了有没有办法将数据从 Fragment 传递到它的 Adapter 的 onBindViewHolder?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Fragment 中有一个 RecyclerView,我想将数据从 Fragment 传递到 MyAdapter.请告诉我是否有任何方法可以做到这一点,或者是否有更好的方法来做到这一点.

I have a RecyclerView in my Fragment and I want to pass data from the Fragment to MyAdapter. Please, tell me if there is any way to do this or if there is a better way to do this.

public class Monday extends Fragment {
    protected RecyclerView mRecyclerView;
    protected RecyclerView.Adapter mAdapter;
    protected RecyclerView.LayoutManager mLayoutManager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.monday, container, false);
        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.monRecycler);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);


        ArrayList<MyLessons> lessons = new ArrayList<>();

        LocalTime now = LocalTime.now();
        LocalTime firstStart = new LocalTime("08:20");
        LocalTime firstEnd = new LocalTime("09:40");
        LocalTime secondStart = new LocalTime("09:50");
        LocalTime secondEnd = new LocalTime("11:10");
        LocalTime thirdStart = new LocalTime("11:30");
        LocalTime thirdEnd = new LocalTime("12:50");
        LocalTime fourthStart = new LocalTime("13:00");
        LocalTime fourthEnd = new LocalTime("14:20");
        LocalTime fifthStart = new LocalTime("14:40");
        LocalTime fifthEnd = new LocalTime("16:00");
        LocalTime sixthStart = new LocalTime("16:10");
        LocalTime sixthEnd = new LocalTime("17:30");
        LocalTime seventhStart = new LocalTime("17:50");
        LocalTime seventhEnd = new LocalTime("19:10");
        LocalTime eighthStart = new LocalTime("19:20");
        LocalTime eighthEnd = new LocalTime("20:40");


        Calendar dayOfWeek = Calendar.getInstance();
        int today = dayOfWeek.get(Calendar.DAY_OF_WEEK);

        int item;

        if (today == Calendar.MONDAY) {
            if ((now.isAfter(firstStart)) && (now.isBefore(firstEnd)))          item = 0;
            else if ((now.isAfter(secondStart)) && (now.isBefore(secondEnd)))   item = 1;
            else if ((now.isAfter(thirdStart)) && (now.isBefore(thirdEnd)))     item = 2;
            else if ((now.isAfter(fourthStart)) && (now.isBefore(fourthEnd)))   item = 3;
            else if ((now.isAfter(fifthStart)) && (now.isBefore(fifthEnd)))     item = 4;
            else if ((now.isAfter(sixthStart)) && (now.isBefore(sixthEnd)))     item = 5;
            else if ((now.isAfter(seventhStart)) && (now.isBefore(seventhEnd))) item = 6;
            else if ((now.isAfter(eighthStart)) && (now.isBefore(eighthEnd)))   item = 7;
            else item = 8;
        }
        else item = 8;

        //I want to pass "item" to MyAdapter

        lessons.add(new MyLessons("Lesson 1", "Teacher"));
        lessons.add(new MyLessons("Lesson 2", "Teacher"));
        lessons.add(new MyLessons("Lesson 3", "Teacher"));
        lessons.add(new MyLessons("Lesson 4", "Teacher"));
        lessons.add(new MyLessons("Lesson 5", "Teacher"));
        lessons.add(new MyLessons("Lesson 6", "Teacher"));
        lessons.add(new MyLessons("Lesson 7", "Teacher"));
        lessons.add(new MyLessons("Lesson 8", "Teacher"));

        mAdapter = new MyAdapter(lessons);
        mRecyclerView.setAdapter(mAdapter);

        return rootView;
    }

}
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private ArrayList<MyLessons> mondayLessons;
    public MyAdapter(ArrayList<MyLessons> mondayLessons) {
        this.mondayLessons = mondayLessons;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView subjectName;
        private TextView teacherName;
        private TextView itemNumber;
        private CardView cardView;
        private ProgressBar mProgress;

        public ViewHolder(View itemView) {
            super(itemView);

            subjectName = (TextView) itemView.findViewById(R.id.textView);
            teacherName = (TextView) itemView.findViewById(R.id.textView2);
            itemNumber = (TextView) itemView.findViewById(R.id.textView3);
            cardView = (CardView) itemView.findViewById(R.id.view);
            mProgress = (ProgressBar) itemView.findViewById(R.id.lessonProgress);
        }
    }

    // Create new views (invoked by the layout manager)
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        View view = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.my_text_layout, parent, false);
        // set the view's size, margins, paddings and layout parameters
        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        MyLessons place = mondayLessons.get(position);

        viewHolder.subjectName.setText(place.getsName());
        viewHolder.teacherName.setText(place.gettName());
        viewHolder.itemNumber.setText(place.getItem());
        viewHolder.itemNumber.setVisibility(View.GONE);
        //String num = viewHolder.itemNumber.getText().toString();
        //int item = Integer.parseInt(num);


        //A place where I want to get "item"

        viewHolder.mProgress.setVisibility(View.GONE);

        if (position == item) {
            viewHolder.cardView.setCardBackgroundColor(Color.parseColor("#9FA8DA"));
            viewHolder.mProgress.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return mondayLessons.size();
    }
}

推荐答案

我认为你可以将 item 传递给 MyAdapter.

I think you can pass item to MyAdapter.

像这样改变MyAdapter的构造函数:

Change the constructor of MyAdapter like this:

public class MyAdapter extends RecyclerView.Adapter {

    private ArrayList<MyLessons> mondayLessons;
    private int item

    public MyAdapter(ArrayList<MyLessons> mondayLessons, inte item) {
        this.mondayLessons = mondayLessons;
        this.item = item;
    }

    ...

}

item 传递给 MyAdapter.

Pass item to MyAdapter.

mAdapter = new MyAdapter(lessons, item);

这篇关于有没有办法将数据从 Fragment 传递到它的 Adapter 的 onBindViewHolder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:59