0

I am developing an app which has navigation drawer which calls RecyclerView.Adapter. RecyclerView.Adapter has list of schedules. I have footer in RecyclerView which enabled user to create new schedule.

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

Adapter Class

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 Fragment

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?
    }

I have 2 questions

  1. Is it a good architecture to implement Navigation drawer -> Fragment to show existing schedules with RecycleView -> Fragment to show selected schedule details?

  2. How to pass information to fragment from RecycleView?. I know communication between fragment could be achieved using interface but wanted to check if there is any other way to implement communication between RecycleView and Fragment?

PP23
  • 3
  • 2

1 Answers1

0

Is it a good architecture to implement Navigation drawer -> Fragment to show existing schedules with RecycleView -> Fragment to show selected schedule details?

That is how the default calendar app on android works. So, yes, that's a good architecture. The default calendar app has a nav drawer. Clicking the drawer's "Schedule" item opens up the Schedule fragment with a list of events. Clicking on one of the events opens up the detail screen for that particular event.

How to pass information to fragment from RecycleView?. I know communication between fragment could be achieved using interface but wanted to check if there is any other way to implement communication between RecycleView and Fragment?

Use bundles: How to transfer some data to another Fragment?

denvercoder9
  • 2,979
  • 3
  • 28
  • 41