我的Fragment中有一个RecyclerView,我想将数据从Fragment传递给MyAdapter。请告诉我是否有任何方法可以做到这一点,或者是否有更好的方法来做到这一点。
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();
}
}
答案 0 :(得分:0)
我认为您可以将item
传递给MyAdapter
。
更改MyAdapter
的构造函数,如下所示:
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。
mAdapter = new MyAdapter(lessons, item);