title: How to replace a fragment with another fragment with a web view to open different URLs by clicking on different items in the recyclerview?
body: <p>My problem is, I have a recyclerview implemented in a fragment called "FragmentNewsItems" that lists out the set of news (images + text) in cardview. So my task is, to make the app, so that when I click on one of those items in the recyclerview, the app would replace it with another fragment with a web view. </p>
  
  <p>Some good people advised me to create a click event:</p>
  
  <pre><code>public class RecyclerViewAdapter extends RecyclerView.Adapter&lt;RecyclerViewAdapter.ViewHolder&gt; {
  
  // add this line
  private AdapterView.OnItemClickListener onItemClickListener;
  
      public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
              private ImageView image;
              private TextView text;
              FragmentNewsItems fragmentNewsItems = new FragmentNewsItems();
  
  
              public ViewHolder(@NonNull View itemView) {
                  super(itemView);
                  image = itemView.findViewById(R.id.image);
                  text = itemView.findViewById(R.id.text);
  
                  itemView.setOnClickListener(this);
              }
  
        @Override
              public void onClick(View view) {
                  mAdapter.onItemHolderClick(ViewHolder.this);
              }
  
          }
  
          public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
              this.onItemClickListener = onItemClickListener;
          }
  
          private void onItemHolderClick(ViewHolder holder) {
              if (onItemClickListener != null)
                  onItemClickListener.onItemClick(null, holder.itemView, holder.getAdapterPosition(), holder.getItemId());
          }
  }
  </code></pre>
  
  <p>and then add the click event to the fragment:</p>
  
  <pre><code>public class FragmentNewsItems extends Fragment implements AdapterView.OnItemClickListener{
  
  
  View view;
      private RecyclerView recyclerView;
      private List&lt;NewsItems&gt; cyberNews;
      private FragmentNewsItems fragmentNewsItems;
  
      public FragmentNewsItems(){
      }
  
      @Nullable
      @Override
      public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          view = inflater.inflate(R.layout.f_news_items_layout, container, false);
          recyclerView = (RecyclerView) view.findViewById(R.id.news_items_recycler_view);
  
          RecyclerViewAdapter recyclerAdapter = new RecyclerViewAdapter(getContext(), cyberNews);
  // add this line
  recyclerAdapter .setOnItemClickListener(this);
  
          recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
          recyclerView.setAdapter(recyclerAdapter);
          return view;
      }
  
      @Override
      public void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          cyberNews = new ArrayList&lt;&gt;();
          cyberNews.add(new NewsItems(R.drawable.cyber1, "The  of cybersecurity in 5G-connected world"));
          cyberNews.add(new NewsItems(R.drawable.cyber2, "Google discovered several iPhone security flaws, and Apple still hasn't patched one"));
          cyberNews.add(new NewsItems(R.drawable.cyber3, "WhatsApp and Telegram media files aren't so secure"));
          cyberNews.add(new NewsItems(R.drawable.cyber4, "Microsoft Exposes Russian Cyberattacks on Phones, Printers, Video Decoders"));
          cyberNews.add(new NewsItems(R.drawable.cyber5, "Tesla demonstrated the power of The Internet of Things"));
  
      }
  
  
  @Override
      public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
  // here open new activity or replace fragment which have webview.
  }
  </code></pre>
  
  <p>Like this:</p>
  
  <pre><code>@Override
      public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
  // here open new activity or replace fragment which have webview.
  }
  </code></pre>
  
  <p>But you see a comment which says open a new activity or replace fragment which have a webview. I tried multiple times to replace with another fragment, but it simply does not open my fragment. I mean, it loads the layout, but does not replace with another fragment. I am a new to Android, and to be honest I have no idea what's wrong. Can you help me with coding?</p>