title: How to implement a Fragment in RecyclerView.Adapter
body: <p>Good day How can I call a fragment when the users will click the card inside MyAdapter class. I want to replace view from the adapter. I am using recyclerview wants to perform click event using the view from my row layout.</p>
  <p>Like for example when the user clicks the card it will open a fragment and it will show more data. Hope you can help me guys.</p>
  <p>This is my main fragment</p>
  <blockquote>
  <p>AccountsFragment</p>
  <p>ReceivableFragment</p>
  <p>LoanAppFragment</p>
  </blockquote>
  <p>Those fragments hold different data. when the user clicks the cards inside that fragment it will call another fragment the <strong>SLDTLFragment</strong>.</p>
  <blockquote>
  <p>Btw this MyAdapter.java</p>
  </blockquote>
  <pre><code>public class MyAdapter extends RecyclerView.Adapter&lt;MyAdapter.MyViewHolder&gt; {
  
  private List&lt;Datas&gt; mDataset;
  
  // Provide a reference to the views for each data item
  // Complex data items may need more than one view per item, and
  // you provide access to all the views for a data item in a view holder
  public static class MyViewHolder extends RecyclerView.ViewHolder {
  
      public CardView mCardView;
      public TextView account_type;
      public TextView accnt_description;
      public TextView balance_label;
      public TextView account_balance;
  
      public MyViewHolder(View v) {
          super(v);
  
          mCardView = (CardView) v.findViewById(R.id.card_view);
  
          account_type = (TextView) v.findViewById(R.id.lblShareCapital);
          balance_label = (TextView) v.findViewById(R.id.lblAvailableBalance);
  
          accnt_description = (TextView) v.findViewById(R.id.sl_desc);
          account_balance = (TextView) v.findViewById(R.id.actual_balance);
      }
  }
  public MyAdapter(List&lt;Datas&gt; myDataset) {
      mDataset = myDataset;
  }
  
  // Create new views (invoked by the layout manager)
  @Override
  public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
      // create a new view
      View v = LayoutInflater.from(parent.getContext())
              .inflate(R.layout.card_item, parent, false);
      // set the view's size, margins, paddings and layout parameters
      MyViewHolder vh = new MyViewHolder(v);
      return vh;
  }
  @Override
  public void onBindViewHolder(MyViewHolder holder, final int position) {
      //holder.account_type.setText(mDataset[position]);
      Datas datas = mDataset.get(position);
      holder.accnt_description.setText(datas.getSL_DESC());
      holder.account_balance.setText(datas.getACTUAL_BALANCE());
      holder.mCardView.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
           // String sl_desc = mDataset.get(position).getSL_DESC();
           // String actual_balance = mDataset.get(position).getACTUAL_BALANCE();
              String sle = mDataset.get(position).getSLE();
              String slc = mDataset.get(position).getSLC();
              String slt = mDataset.get(position).getSLT();
              String ref_no = mDataset.get(position).getREF();
  
              Log.d(&quot;CardView Clicked&quot;, &quot;sle code: &quot; + sle);
              Log.d(&quot;CardView Clicked&quot;, &quot;slc code: &quot; + slc);
              Log.d(&quot;CardView Clicked&quot;, &quot;slt code: &quot; + slt);
              Log.d(&quot;CardView Clicked&quot;, &quot;ref no: &quot;   + ref_no);
          }
      });
  }
  
  @Override
  public int getItemCount() {
      return mDataset.size();
  }
  </code></pre>
  <p>}</p>
  <blockquote>
  <p>AccountsFragment.java</p>
  </blockquote>
  <pre><code>public class AccountsFragment extends Fragment {
  private SQLiteHandler db;
      public AccountsFragment() {
          // Required empty public constructor
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          View rootView = inflater.inflate(R.layout.fragment_accounts, container, false);
          RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
          rv.setHasFixedSize(true);
          SQLiteHandler db = new SQLiteHandler(getActivity());
          MyAdapter adapter = new MyAdapter(db.getUserSLDetails());
          rv.setAdapter(adapter);
  
          LinearLayoutManager llm = new LinearLayoutManager(getActivity());
          rv.setLayoutManager(llm);
  
          return rootView;
      }
  }
  </code></pre>
  <blockquote>
  <p>ReceivableFragment.java</p>
  </blockquote>
  <pre><code>    public class ReceivableFragment extends Fragment {
  
  
      public ReceivableFragment() {
          // Required empty public constructor
      }
  
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          View rootView = inflater.inflate(R.layout.fragment_receivable, container, false);
          RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
          rv.setHasFixedSize(true);
          SQLiteHandler db = new SQLiteHandler(getActivity());
          MyAdapter adapter = new MyAdapter(db.getUserSLARLoans());
          rv.setAdapter(adapter);
  
          LinearLayoutManager llm = new LinearLayoutManager(getActivity());
          rv.setLayoutManager(llm);
  
          return rootView;
      }
  
  }
  </code></pre>
  <blockquote>
  <p>MainActivity.java</p>
  </blockquote>
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      private TextView nav_header_name;
      private TextView nav_header_email;
  
      public TextView txtLogOut;
  
      private SQLiteHandler db;
      private SessionManager session;
  
      private Timer timer;
      private Toolbar toolbar;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.activity_main);
          toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
          //Will replace the activity_main.xml to relativelayout_for_fragment.xml
          if (savedInstanceState == null) {
              AccountsFragment f1= new AccountsFragment();
              FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
              fragmentTransaction.add(R.id.relativelayout_for_fragment, f1);// relativelayout_for_fragment is inside the content_main.xml
              fragmentTransaction.commit();
          }
  
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
  
          DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
          ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                  this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
          drawer.addDrawerListener(toggle);
          toggle.syncState();
  
          NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
          View header=navigationView.getHeaderView(0);
          navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
              @Override
              public boolean onNavigationItemSelected(MenuItem item) {
  
                  txtLogOut = (TextView) findViewById(R.id.nav_logout_menu);
  
                  int id = item.getItemId();
  
                  if (id == R.id.nav_deposit) {
                      // Handle the preference  action
                      AccountsFragment accounts= new AccountsFragment();
                      FragmentManager manager= getSupportFragmentManager();
                      manager.beginTransaction()
                              .replace(R.id.relativelayout_for_fragment, accounts)
                              .commit();
  
                      Toast.makeText(MainActivity.this, &quot;Deposit Accounts&quot;, Toast.LENGTH_SHORT).show();
  
                  }else if (id == R.id.nav_receivable) {
                      // Handle the Loan application();
                      ReceivableFragment receivable= new ReceivableFragment();
                      FragmentManager manager= getSupportFragmentManager();
                      manager.beginTransaction()
                              .replace(R.id.relativelayout_for_fragment, receivable)
                              .commit();
  
                      Toast.makeText(MainActivity.this, &quot;AR and Loan Accounts&quot;, Toast.LENGTH_SHORT).show();
  
                  }else if (id == R.id.nav_apply_loan_menu) {
                      // Handle the Loan application();
                      LoanAppFragment loan_app= new LoanAppFragment();
                      FragmentManager manager= getSupportFragmentManager();
                      manager.beginTransaction()
                              .replace(R.id.relativelayout_for_fragment, loan_app)
                              .commit();
  
                      Toast.makeText(MainActivity.this, &quot;This module is under developement&quot;, Toast.LENGTH_SHORT).show();
                  }else if (id == R.id.nav_logout_menu) {
                      // Handle the About action
                      logoutUser();
                      //Toast.makeText(MainActivity.this, &quot;successfully Logout&quot;, Toast.LENGTH_LONG).show();
                  }
  
                  DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                  drawer.closeDrawer(GravityCompat.START);
                  return true;
              }
          });
  
          nav_header_name = (TextView) header.findViewById(R.id.nav_name);
          nav_header_email = (TextView) header.findViewById(R.id.nav_email);
  
          // SqLite database handler
          db = new SQLiteHandler(getApplicationContext());
  
          // session manager
          session = new SessionManager(getApplicationContext());
  
          if (!session.isLoggedIn()) {
              logoutUser();
          }
  
          //Fetching user details from SQLite
          HashMap&lt;String, String&gt; user = db.getUserDetails();
  
          String username = user.get(&quot;username&quot;);
          String email = user.get(&quot;email&quot;);
          //Displaying the user info in nav_header_main.xml
          nav_header_name.setText(username);
          nav_header_email.setText(email);
  
      }
  
      @Override
      public void onBackPressed() {
          DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
          if (drawer.isDrawerOpen(GravityCompat.START)) {
              drawer.closeDrawer(GravityCompat.START);
          } else {
              super.onBackPressed();
          }
      }
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
      /**
       * Logging out the user. Will set isLoggedIn flag to false in shared
       * preferences Clears the user data from sqlite users table
       * */
      private void logoutUser() {
          session.setLogin(false);
  
          db.deleteUsers();
          db.deleteUserSLDTL();
  
          // Launching the login activity
          Intent intent = new Intent(MainActivity.this, LoginActivity.class);
          startActivity(intent);
          finish();
      }
  
      @Override
      protected void onPause() {
          super.onPause();
  
          timer = new Timer();
          Log.i(&quot;Main&quot;, &quot;Invoking logout timer&quot;);
          LogOutTimerTask logoutTimeTask = new LogOutTimerTask();
          timer.schedule(logoutTimeTask, 30000); // auto logout in 30secs inactivity
          Log.i(&quot;Main&quot;, &quot;Log out user due to inactivity&quot;);
      }
  
      @Override
      protected void onResume() {
          super.onResume();
          if (timer != null) {
              timer.cancel();
              Log.i(&quot;Main&quot;, &quot;cancel timer&quot;);
              timer = null;
          }
      }
  
      private class LogOutTimerTask extends TimerTask {
  
          @Override
          public void run() {
              session.setLogin(false);
  
              db.deleteUsers();
              db.deleteUserSLDTL();
              //redirect user to login screen
              Intent i = new Intent(MainActivity.this, LoginActivity.class);
              i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
              startActivity(i);
              finish();
          }
      }
  }
  </code></pre>