title: Load a recyclerview fragment on clicking another recyclerview fragment's specific item click using tabbed fragment?
body: <p>I have checked other answers and articles. But couldn't find what I am looking for. I might be missing something. </p>
  
  <p>I have a tabbed activity which contains three fragments: </p>
  
  <ol>
  <li>FragmentHome (Tab 1), </li>
  <li>FragmentTwo (Tab 2), </li>
  <li>FragmentThree (Tab 3). </li>
  </ol>
  
  <p>Now I want to load another Fragment ("FragmentAnother") on clicking HomeFragment's 
  specific Item. Say, I want to load "FragmentAnother" (RecyclerViewFragment) on clicking "5th" item in HomeFragment (Recyclerview fragment). And I want to load a specific detailView for a specific item in HomeFragment. How to achieve this?  </p>
  
  <p>MainActivity.java</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  private SectionsPagerAdapter mSectionsPagerAdapter;
  
  private ViewPager mViewPager;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
      // Create the adapter that will return a fragment for each of the three
      // primary sections of the activity.
      mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
  
      // Set up the ViewPager with the sections adapter.
      mViewPager = (ViewPager) findViewById(R.id.container);
      mViewPager.setAdapter(mSectionsPagerAdapter);
  
      TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
  
      mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
      tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
  
  
      FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
      fab.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                      .setAction("Action", null).show();
          }
      });
  
  }
  
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.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);
  }
  
  public class SectionsPagerAdapter extends FragmentPagerAdapter {
  
      public SectionsPagerAdapter(FragmentManager fm) {
          super(fm);
      }
  
      @Override
      public Fragment getItem(int position) {
         switch (position) {
             case 0:
                 FragmentHome fragmentTabOne = new FragmentHome();
                 return fragmentTabOne;
             case 1:
                 FragmentTabTwo fragmentTabTwo = new FragmentTabTwo();
                 return fragmentTabTwo;
             case 2:
                 FragmentTabThree fragmentTabThree = new FragmentTabThree();
                 return fragmentTabThree;
                 default:
                     return null;
         }
  
  
      }
  
      @Override
      public int getCount() {
          // Show 3 total pages.
          return 3;
      }
  
  
      }
  }
  </code></pre>
  
  <p>FragmentHome.java (Tab One or first tab)</p>
  
  <pre><code>public class FragmentHome extends android.support.v4.app.Fragment {
  private RecyclerView homeRecyclerView;
  private RecyclerView.Adapter homeListAdapter;
  
  private ArrayList&lt;HomeListItem&gt; homeListItems;
  
  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  
      // Inflate the layout for this fragment
      View rootView = inflater.inflate(R.layout.fragment_home, container, false);
  
      homeRecyclerView = (RecyclerView) rootView.findViewById(R.id.homeRecyclerView);
      homeRecyclerView.setHasFixedSize(true);
  
      homeListItems = HomeListItemData.getItem();
  
      homeListAdapter = new HomeListRecyclerViewAdapter(homeListItems, getContext());
      homeRecyclerView.setAdapter(homeListAdapter);
  
      LinearLayoutManager llm = new LinearLayoutManager(getActivity());
      homeRecyclerView.setLayoutManager(llm);
  
      return rootView;
  }
  
  @Override
  public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
      super.onViewCreated(view, savedInstanceState);
  }
  
  
  }
  </code></pre>
  
  <p>HomeListItem.java </p>
  
  <pre><code>public class HomeListItem {
  private int CardImageId;
  private String Heading;
  private String Description;
  
  public HomeListItem(int cardImageId, String heading, String description) {
      this.CardImageId = cardImageId;
      Heading = heading;
      Description = description;
  }
  
  public String getHeading() {
      return Heading;
  }
  
  public String getDescription() {
      return Description;
  }
  
  public int getCardImageId() {
      return CardImageId;
  }
  }
  </code></pre>
  
  <p>HomeListData.java</p>
  
  <pre><code>public class HomeListItemData {
  public static ArrayList&lt;HomeListItem&gt; getItem() {
      ArrayList&lt;HomeListItem&gt; homeListItemArrayList = new ArrayList&lt;&gt;();
  
      homeListItemArrayList.add(new HomeListItem(R.drawable.ic_launcher_background, "First 10 Reasons Why", "First 10 reasons accordig to"));
      homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, "Second 10 Reasons Why", "Second 10 reasons short description"));
      homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, "Third 14 Reasons Why", "Third 14 reasons short description"));
      homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, "Forth 5 Reasons Why", "Forth 5 reasons short description"));
      homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, "Fifth 12 Reasons Why", "Fifth 12 reasons Je suis content de savoir short description"));
      homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, "Sixth 25 Reasons Why", "Sixth 25 reasons short description"));
      homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, "Seventh 30 Reasons Why", "Seventh 30 reasons"));
      homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, "Eighth 8 Reasons Why", "Eighth 8 reasons Je suis content de savoir short description"));
      homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, "Ninth 34 Reasons Why", "Ninth 34 reasons Je suis content de savoir short description"));
      homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, "Tenth 9 Reasons Why", "Tenth 9 reasons Je suis content de savoir short description"));
      homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, "Eleventh 2 Reasons Why", "Eleventh 2 reasons Je suis content de savoir short description"));
      homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, "Twelve 15 Reasons Why", "Twelve 15 reasons Je suis content de savoir short description"));
      homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, "Thirteenth 17 Reasons Why", "Thirteenth 17 reasons Je suis content de savoir short description"));
  
  
      return homeListItemArrayList;
  }
  }
  </code></pre>
  
  <p>HomeListRecyclerViewAdapter</p>
  
  <pre><code> public class HomeListRecyclerViewAdapter extends 
   RecyclerView.Adapter&lt;HomeListRecyclerViewAdapter.ViewHolder&gt; {
  
  List&lt;HomeListItem&gt; homeListItems;
  Context context;
  
  public HomeListRecyclerViewAdapter(List&lt;HomeListItem&gt; homeListItems, Context context) {
      this.homeListItems = homeListItems;
      this.context = context;
  }
  
  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  
      View homeListViewHolder = LayoutInflater.from(parent.getContext())
              .inflate(R.layout.home_list_item, parent, false);
  
      return new ViewHolder(homeListViewHolder);
  }
  
  @Override
  public void onBindViewHolder(ViewHolder holder, final int position) {
      final HomeListItem homeListItem = homeListItems.get(position);
  
      if (position==0){
   `      holder.cardImageView.setBackgroundResource(R.drawable.circle_background_green_blue_deep);
  
      } else if (position==1)
  </code></pre>
  
  <p><code>{holder.cardImageView.setBackgroundResource(R.drawable.circle_background_purple__
      middle_islam_pro);
          } else if (position==2){</code></p>
  
  <pre><code>holder.cardImageView.setBackgroundColor(Color.parseColor("#FFC107"));
      } else if (position==3){
          holder.cardImageView.setBackgroundColor(Color.CYAN);
      }
  
      holder.cardImageView.setImageResource(homeListItem.getCardImageId());
      holder.headingTextView.setText(homeListItem.getHeading());
      holder.shortDescriptionTextView.setText(homeListItem.getDescription());
  
      //Load another fragment on Click a card
      holder.homeListCardViewRelativeLayout.setOnClickListener(new 
  View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Toast.makeText(context, "You clicked " + position, 
  Toast.LENGTH_SHORT).show();
          }
      });
  
  }
  
  @Override
  public int getItemCount() {
      return homeListItems.size();
  }
  
  public class ViewHolder extends RecyclerView.ViewHolder {
      public ImageView cardImageView;
      public TextView headingTextView;
      public TextView shortDescriptionTextView;
      public RelativeLayout homeListCardViewRelativeLayout;
  
      public ViewHolder(View itemView) {
          super(itemView);
  
          cardImageView = (ImageView) 
  itemView.findViewById(R.id.cardImageView);
          headingTextView = (TextView) 
  itemView.findViewById(R.id.headingTextView);
          shortDescriptionTextView = (TextView) 
  itemView.findViewById(R.id.shortDescriptionTextView);
  
          homeListCardViewRelativeLayout = (RelativeLayout) 
  itemView.findViewById(R.id.homeListCardItemRelativeLayoutId);
      }
  }
  }
  </code></pre>
  
  <p>activity main xml file</p>
  
  <pre><code>&lt;android.support.design.widget.CoordinatorLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/main_content"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fitsSystemWindows="true"
  tools:context="com.hello.abc.activity.MainActivity"&gt;
  
  &lt;android.support.design.widget.AppBarLayout
      android:id="@+id/appbar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:theme="@style/AppTheme.AppBarOverlay"&gt;
  
      &lt;android.support.v7.widget.Toolbar
          android:id="@+id/toolbar"
          android:layout_weight="1"
          android:layout_width="match_parent"
          android:background="?attr/colorPrimary"
          android:layout_height="?attr/actionBarSize"
          app:layout_scrollFlags="scroll|enterAlways"
          app:popupTheme="@style/AppTheme.PopupOverlay"
          app:title="@string/app_name"&gt;
  
      &lt;/android.support.v7.widget.Toolbar&gt;
  
      &lt;View
          android:id="@+id/toolbarDropShadow"
          android:layout_width="match_parent"
          android:layout_height="3dp"
          android:background="@drawable/toolbar_drop_shadow"/&gt;
  
  
      &lt;android.support.design.widget.TabLayout
          android:id="@+id/tabs"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          app:tabMaxWidth="0dp"
          app:tabGravity="fill"
          app:tabMode="fixed"&gt;
  
          &lt;android.support.design.widget.TabItem
              android:id="@+id/tabItem"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Home" /&gt;
  
          &lt;android.support.design.widget.TabItem
              android:id="@+id/tabItem2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Tab Two" /&gt;
  
          &lt;android.support.design.widget.TabItem
              android:id="@+id/tabItem3"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Tab Three" /&gt;
  
      &lt;/android.support.design.widget.TabLayout&gt;
  &lt;/android.support.design.widget.AppBarLayout&gt;
  
  &lt;android.support.v4.view.ViewPager
      android:id="@+id/container"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:layout_behavior="@string/appbar_scrolling_view_behavior" /&gt;
  
  &lt;android.support.design.widget.FloatingActionButton
      android:id="@+id/fab"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="end|bottom"
      android:layout_margin="@dimen/fab_margin"
      app:srcCompat="@android:drawable/ic_dialog_email" /&gt;
  
  &lt;/android.support.design.widget.CoordinatorLayout&gt;
  </code></pre>
  
  <p>Fragment Home xml file (First Tab)</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/relativeLayout"
  
  
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.hello.abc.activity.MainActivity"&gt;
  
  &lt;ScrollView
  android:paddingTop="9dp"
  android:paddingBottom="9dp"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_marginBottom="?attr/actionBarSize"&gt;
  
  &lt;android.support.v7.widget.RecyclerView
      android:id="@+id/homeRecyclerView"
      android:background="@color/backgroundColor"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"&gt;
  
  &lt;/android.support.v7.widget.RecyclerView&gt;
  &lt;/ScrollView&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Home List Item xml file</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:card_view="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"&gt;
  
  &lt;android.support.v7.widget.CardView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="#FFFFFF"
      card_view:cardCornerRadius="@dimen/cardview_default_radius"&gt;
  
      &lt;RelativeLayout
          android:id="@+id/homeListCardItemRelativeLayoutId"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:clickable="true"
          android:foreground="?android:selectableItemBackground"
          android:orientation="vertical"
          android:padding="12dp"&gt;
  
          &lt;ImageView
              android:id="@+id/cardImageView"
              android:layout_width="64dp"
              android:layout_height="64dp"
              android:layout_marginRight="10dp"
              android:background="@drawable/circle_bg_islam_pro_green"
              android:padding="13dp"
              android:src="@drawable/ic_launcher_background" /&gt;
  
          &lt;TextView
              android:id="@+id/headingTextView"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_alignParentEnd="true"
              android:layout_alignParentRight="true"
              android:layout_alignTop="@+id/cardImageView"
              android:layout_marginTop="-5dp"
              android:layout_toEndOf="@+id/cardImageView"
              android:layout_toRightOf="@+id/cardImageView"
              android:text="Heading"
  
  android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" /&gt;
  
          &lt;TextView
              android:id="@+id/shortDescriptionTextView"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_alignParentEnd="true"
              android:layout_alignParentRight="true"
              android:layout_below="@+id/headingTextView"
              android:layout_toEndOf="@id/cardImageView"
              android:layout_toRightOf="@+id/cardImageView"
              android:text="This is short description"
              android:textColor="@color/grayDark" /&gt;
      &lt;/RelativeLayout&gt;
  &lt;/android.support.v7.widget.CardView&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Now my another fragment class which I want to load on click HomeFragment item click.</p>
  
  <pre><code>public class FragmentAnother extends Fragment {
  private RecyclerView homeRecyclerView;
  private RecyclerView.Adapter homeListAdapter;
  
  private ArrayList&lt;HomeListItem&gt; homeListItems;
  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
  container, @Nullable Bundle savedInstanceState) {
      // Inflate the layout for this fragment
      View rootView = inflater.inflate(R.layout.fragment_home, container, 
  false);
  
      homeRecyclerView = (RecyclerView) 
  rootView.findViewById(R.id.homeRecyclerView);
      homeRecyclerView.setHasFixedSize(true);
  
      homeListItems = FragmentAnotherItemData.getItem();
  
      homeListAdapter = new HomeListRecyclerViewAdapter(homeListItems, 
  getContext());
      homeRecyclerView.setAdapter(homeListAdapter);
  
      LinearLayoutManager llm = new LinearLayoutManager(getActivity());
      homeRecyclerView.setLayoutManager(llm);
  
      return rootView;
  }
  
  @Override
  public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
      super.onViewCreated(view, savedInstanceState);
  }
  }
  </code></pre>
  
  <p>Another Fragment Data Class</p>
  
  <pre><code>public class HomeListItemData {
  public static ArrayList&lt;HomeListItem&gt; getItem() {
  ArrayList&lt;HomeListItem&gt; homeListItemArrayList = new ArrayList&lt;&gt;();
  
  homeListItemArrayList.add(new 
  HomeListItem(R.drawable.ic_launcher_background, "First 4 Reasons Why", 
  "First 4 reasons accordig to"));
  homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, 
  "Second 8 Reasons Why", "Second 8 reasons short description"));
  homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, 
  "Third 24 Reasons Why", "Third 24 reasons short description"));
  homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, 
  "Forth 15 Reasons Why", "Forth 15 reasons short description"));
  homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, 
  "Fifth 10 Reasons Why", "Fifth 10 reasons short description"));
  homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, 
  "Sixth 25 Reasons Why", "Sixth 25 reasons short description"));
  homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, 
  "Seventh 30 Reasons Why", "Seventh 30 reasons Je suis content de savoir 
  short description"));
  homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, 
  "Eighth 8 Reasons Why", "Eighth 8 reasons Je suis content de savoir short 
  description"));
  homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, 
  "Ninth 34 Reasons Why", "Ninth 34 reasons Je suis content de savoir short 
  description"));
  homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, 
  "Tenth 9 Reasons Why", "Tenth 9 reasons Je suis content de savoir short 
  description"));
  homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, 
  "Eleventh 2 Reasons Why", "Eleventh 2 reasons Je suis content de savoir 
  short description"));
  homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, 
  "Twelve 15 Reasons Why", "Twelve 15 reasons Je suis content de savoir short 
  description"));
  
  return homeListItemArrayList;
   }
   }
  </code></pre>