title: How can I Create a generic method for these?
body: <p>Please how can I create method for each of these two generic list of Fragments??</p>
  <p>As constructor won't allow me to create multiple
  Of these methods.</p>
  <ol>
  <li>I want to create a method that looks like the constructor <strong><strong>Semesters (int position){
  mData=getCsList(position);
  }</strong></strong></li>
  </ol>
  <p>When I run the program my constructor is called by the compiler, which does the job I want it. How can I achieve this by creating and using methods that does same Job because I have many list of Fragments,and as to contractor will not allow me due to it's parameters signature,that's why I want to use methods and keep the same signature throughout. Thanks</p>
  <p>................... EDIT CODE.......…..</p>
  <p>SemesterFragment.java</p>
  <pre><code> package com.example.viewpager2;
  
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  import androidx.annotation.NonNull;
  import androidx.fragment.app.Fragment;
  import androidx.viewpager2.widget.ViewPager2;
  
  import com.google.android.material.tabs.TabLayout;
  import com.google.android.material.tabs.TabLayoutMediator;
  
  public class SemesterFragment extends Fragment {
      TabLayout tabLayout;
      ViewPager2 viewPager2;
      MyViewPager adapter;
  
      public SemesterFragment() {
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_semester, container, false);
          viewPager2 = (ViewPager2) view.findViewById(R.id.viewPager2);
          tabLayout = view.findViewById(R.id.tabLayout);
          adapter = new MyViewPager(getChildFragmentManager(), this.getLifecycle());
          viewPager2.setAdapter(adapter);
          new TabLayoutMediator(tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
              @Override
              public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                  tab.setText(&quot;Sem&quot; + (position + 1));
              }
          }).attach();
          addComputerScienceSem();
          adapter.notifyDataSetChanged();
          return view;
      }
  
      private void addComputerScienceSem() {
          adapter.addFrags(new Semesters().newInstance(0, 1));
          adapter.addFrags(new Semesters().newInstance(0, 2));
          adapter.addFrags(new Semesters().newInstance(0, 3));
          adapter.addFrags(new Semesters().newInstance(0, 4));
      }
  }
  </code></pre>
  <p>MyViewPager.java</p>
  <pre><code>package com.example.viewpager2;
  
  import androidx.annotation.NonNull;
  import androidx.fragment.app.Fragment;
  import androidx.fragment.app.FragmentManager;
  import androidx.lifecycle.Lifecycle;
  import androidx.viewpager2.adapter.FragmentStateAdapter;
  
  import java.util.ArrayList;
  import java.util.List;
  
  public class MyViewPager extends FragmentStateAdapter {
      List&lt;Fragment&gt; fragmentList = new ArrayList&lt;&gt;();
  
      public MyViewPager(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
          super(fragmentManager, lifecycle);
      }
  
      @NonNull
      @Override
      public Fragment createFragment(int position) {
          return fragmentList.get(position);
      }
  
      @Override
      public int getItemCount() {
          return fragmentList.size();
      }
  
      public void addFrags(Fragment fragment) {
          fragmentList.add(fragment);
      }
  }
  </code></pre>
  <p>Semesters.java</p>
  <pre><code> package com.example.viewpager2;
  
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  import androidx.fragment.app.Fragment;
  import androidx.recyclerview.widget.LinearLayoutManager;
  import androidx.recyclerview.widget.RecyclerView;
  
  import java.util.ArrayList;
  import java.util.List;
  
  public class Semesters extends Fragment {
      List mData;
      RVFragment adapter;
      RecyclerView recyclerView;
  
      public Semesters() {
      }
  
      public static Semesters newInstance(int index, int position) {
          Semesters fragment = new Semesters();
          Bundle args = new Bundle();
          args.putInt(&quot;index&quot;, index);
          args.putInt(&quot;position&quot;, position);
          fragment.setArguments(args);
          return fragment;
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment, container, false);
          recyclerView = view.findViewById(R.id.recyclerView);
          LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
          recyclerView.setLayoutManager(layoutManager);
          adapter = new RVFragment(getContext(), mData);
          if (getArguments().getInt(&quot;index&quot;, 0) == 0) {
              mData = getCSList(getArguments().getInt(&quot;position&quot;, 0));
          } else if (getArguments().getInt(&quot;index&quot;, 0) == 1) {
              mData = getSTAList(getArguments().getInt(&quot;position&quot;, 0));
          }
          recyclerView.setAdapter(adapter);
          return view;
      }
  
      private List getCSList(int position) {
          List list = new ArrayList&lt;&gt;();
          if (position == 0) { //fragment one inside viewpager whenever item1 is clicked from the recyclerView 
              list.add(new SemesterModelClass(&quot;intro. to comp&quot;, &quot;com101&quot;, &quot;3&quot;));
              list.add(new SemesterModelClass(&quot;digital electonics&quot;, &quot;com112&quot;, &quot;3&quot;));
              list.add(new SemesterModelClass(&quot;intro to programming&quot;, &quot;com113&quot;, &quot;4&quot;));
          } else if (position == 1) { //fragment two inside viewpager whenever item1 is clicked from the recyclerView 
              list.add(new SemesterModelClass(&quot;sem2&quot;, &quot;com111&quot;, &quot;3&quot;));
              list.add(new SemesterModelClass(&quot;sem2&quot;, &quot;com222&quot;, &quot;5&quot;));
              list.add(new SemesterModelClass(&quot;sem2&quot;, &quot;com121&quot;, &quot;4&quot;));
          } else if (position == 2) { //fragment three inside viewpager whenever item1 is clicked from the recyclerView 
              list.add(new SemesterModelClass(&quot;sem3&quot;, &quot;com111&quot;, &quot;3&quot;));
              list.add(new SemesterModelClass(&quot;sem3&quot;, &quot;com222&quot;, &quot;5&quot;));
              list.add(new SemesterModelClass(&quot;sem3&quot;, &quot;com121&quot;, &quot;4&quot;));
          } else if (position == 3) { //fragment four inside viewpager whenever item1 is clicked from the recyclerView 
              list.add(new SemesterModelClass(&quot;sem4&quot;, &quot;com111&quot;, &quot;3&quot;));
              list.add(new SemesterModelClass(&quot;sem4&quot;, &quot;com222&quot;, &quot;5&quot;));
              list.add(new SemesterModelClass(&quot;sem4&quot;, &quot;com121&quot;, &quot;4&quot;));
          }
          return list;
      }
  
      private List getSTAList(int position) {
          List list = new ArrayList&lt;&gt;();
          if (position == 4) { //fragment one inside viewpager whenever item2 is clicked from the recyclerView 
              list.add(new SemesterModelClass(&quot;probability&quot;, &quot;STA 111&quot;, &quot;3&quot;));
              list.add(new SemesterModelClass(&quot;algebra&quot;, &quot;STA 112&quot;, &quot;4&quot;));
              list.add(new SemesterModelClass(&quot;logic&quot;, &quot;STA 113&quot;, &quot;3&quot;));
              list.add(new SemesterModelClass(&quot;geometri func&quot;, &quot;STA 114&quot;, &quot;4&quot;));
          } else if (position == 5) { //fragment two inside viewpager whenever item2 is clicked from the recyclerView 
              list.add(new SemesterModelClass(&quot;set theory&quot;, &quot;STA 111&quot;, &quot;3&quot;));
              list.add(new SemesterModelClass(&quot;dedvwvdv&quot;, &quot;STA 112&quot;, &quot;4&quot;));
              list.add(new SemesterModelClass(&quot;fbcfvdv&quot;, &quot;STA 113&quot;, &quot;3&quot;));
              list.add(new SemesterModelClass(&quot;cvsvsdvs&quot;, &quot;STA 114&quot;, &quot;4&quot;));
          } else if (position == 6) { //fragment three inside viewpager whenever item2 is clicked from the recyclerView 
              list.add(new SemesterModelClass(&quot;geo&quot;, &quot;STA 111&quot;, &quot;3&quot;));
              list.add(new SemesterModelClass(&quot;semdvbv3&quot;, &quot;STA 112&quot;, &quot;4&quot;));
              list.add(new SemesterModelClass(&quot;dedgsedv&quot;, &quot;STA 113&quot;, &quot;3&quot;));
              list.add(new SemesterModelClass(&quot;frgs&quot;, &quot;STA 114&quot;, &quot;4&quot;));
          } else if (position == 7) { //fragment four inside viewpager whenever item2 is clicked from the recyclerView 
              list.add(new SemesterModelClass(&quot;log&quot;, &quot;STA 111&quot;, &quot;3&quot;));
              list.add(new SemesterModelClass(&quot;sem4fff&quot;, &quot;STA 112&quot;, &quot;4&quot;));
              list.add(new SemesterModelClass(&quot;sem4gtgg&quot;, &quot;STA 113&quot;, &quot;3&quot;));
              list.add(new SemesterModelClass(&quot;sem4uku&quot;, &quot;STA 114&quot;, &quot;4&quot;));
          }
          return list;
      }
  }
  </code></pre>
  <p>...................EDITED this……........</p>
  <p>DepartmentRVAdapter.java</p>
  <pre><code> public void onClick(View v){
  
  
      clickInterface.onItemClickListener(getLayoutPosition());
          switch(getAdapterPosition()){
          //loads department best on the position that user click
          case 0:                         // it loads all departments in the school
          Objects.requireNonNull(departmentFragment.getActivity()).getSupportFragmentManager().beginTransaction() //
          .setCustomAnimations(R.anim.slide_in_left,R.anim.exit_to_right)
          .replace(R.id.frameLayout,new SemesterFragment()).addToBackStack(null).commit();break;
  
       case 1:                         // it loads all departments in the school
          
  Objects.requireNonNull(departmentFragment.getActivity()).getSupportFragmentManager().beginTransaction() //
          .setCustomAnimations(R.anim.slide_in_left,R.anim.exit_to_right)
          .replace(R.id.frameLayout,new SemesterFragment()).addToBackStack(null).commit();break;
  
  }
  </code></pre>