我正在开发一个具有导航抽屉的应用程序,该应用程序调用RecyclerView.Adapter。 RecyclerView.Adapter具有日程表列表。我在RecyclerView中有页脚,该页脚使用户可以创建新时间表。
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
protected void onCreate(Bundle savedInstanceState) {
…
}
public void selectItemDrawer(MenuItem menuItem){
Fragment myFragment = null;
Class fragmentClass;
switch (menuItem.getItemId()) {
case R.id.menu_home:
fragmentClass = dashboardFragment.class;
break;
case R.id.menu_plans:
fragmentClass = ScheduleListFragment.class;
break;
default:
fragmentClass = dashboardFragment.class;
}
try {
myFragment = (Fragment) fragmentClass.newInstance();
}
catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent,myFragment).commit();
menuItem.setCheckable(true);
setTitle(menuItem.getTitle());
mDrawerLayout.closeDrawers();
}
}
适配器类
public class MyRecyclerAdapter extends Adapter<RecyclerAdapter.ViewHolder> {
private ArrayList<ScheduleModel> activeSchedules;
private ShowScheduleListener mListener;
public MyRecyclerAdapter(Context ctx, ShowScheduleListener listener){
mListener = listener;
...
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.itemView.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
mListener.onItemClicked(mItems[position]);
}
});
}
interface ShowScheduleListener{
void onItemClicked(ItemType item);
}
}
}
RecyclerView片段
public class ScheduleList extends Fragment implements MyRecyclerAdapter.ShowScheduleListener {
View v;
private RecyclerView scheduleRecyclerView;
private ArrayList<ScheduleModel> activeSchedules;
private DatabaseHandler databaseHandler;
public ScheduleList() {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_schedule_list,container,false);
scheduleRecyclerView=v.findViewById(R.id.RecyclerView_schedule_list);
scheduleRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
activeSchedules = new ArrayList<>();
databaseHandler=DatabaseHandler.getInstance(getContext());
activeSchedules=databaseHandler.getActiveSchedules();
return v;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MyRecyclerAdapter myRecyclerAdapter = new MyRecyclerAdapter (getContext(), activeSchedules);
scheduleRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
scheduleRecyclerView.setAdapter(myRecyclerAdapter);
}
@Override
public void onScheduleItemClick(int position) {
// as per other posts I am supposed to initiate Fragment here
// but should fragment talk to another fragment via interface implemented this way?
}
我有2个问题
实现导航抽屉->使用RecycleView片段显示现有计划是否是一个好的架构->使用片段显示所选计划详细信息的片段?
如何将信息传递到RecycleView的片段?我知道片段之间的通信可以使用接口来实现,但想检查是否有其他方法可以实现RecycleView和Fragment之间的通信?
答案 0 :(得分:0)
实现导航抽屉->片段以使用RecycleView显示现有时间表是否是一个好的体系结构->片段以显示选定的时间表详细信息?
这就是android上默认日历应用程序的工作方式。所以,是的,这是一个好的架构。默认的日历应用程序具有一个导航抽屉。单击抽屉的“时间表”项,将打开带有事件列表的时间表片段。单击事件之一会打开该特定事件的详细信息屏幕。
如何将信息传递到RecycleView的片段?我知道片段之间的通信可以使用接口来实现,但想检查是否有其他方法来实现RecycleView和Fragment之间的通信?