title: ViewPager2 with RecyclerView not displaying correctly
body: <p>I've been working on an app that uses a vertical RecyclerView inside a fragment, all inside a ViewPager2 object with a tabLayout. I can't seem to figure out how to make all these things work together. Currently, it looks like I got it to show the recycler view with the data but the tabs aren't showing up and the items in the list are spaced like they're different pages. Could someone explain how I'm messing up?</p>
  <p>It looks like this:</p>
  <p><a href="https://i.stack.imgur.com/YPUsu.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YPUsu.gif" alt="Current app look" /></a></p>
  <p>I'd like it to be like this:</p>
  <p><a href="https://i.stack.imgur.com/ICsdZ.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ICsdZ.gif" alt="What I'd like it to look like" /></a></p>
  <p>Here's the code so far:</p>
  <h1>Classes</h1>
  <h2>MainActivity.java</h2>
  <pre><code>import androidx.annotation.NonNull;
  import androidx.appcompat.app.AppCompatActivity;
  import androidx.viewpager2.widget.ViewPager2;
  import android.os.Bundle;
  import com.google.android.material.tabs.TabLayout;
  import com.google.android.material.tabs.TabLayoutMediator;
  import com.mapbox.mapboxsdk.Mapbox;
  import com.mapbox.mapboxsdk.maps.MapView;
  import com.mapbox.mapboxsdk.maps.Style;
  
  public class MainActivity extends AppCompatActivity {
  
      private MapView mapView;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          // Mapbox Access token
          Mapbox.getInstance(getApplicationContext(), getString(R.string.mapbox_api_key));
  
          setContentView(R.layout.activity_main);
  
          mapView = findViewById(R.id.mapView);
          mapView.onCreate(savedInstanceState);
          mapView.getMapAsync(mapboxMap -&gt; mapboxMap.setStyle(Style.DARK, style -&gt; {
  
              // Map is set up and the style has loaded. Now you can add data or make other map adjustments
  
  
          }));
  
          ViewPager2 pager = findViewById(R.id.view_pager);
  
          ViewPageAdapter pageAdapter = new ViewPageAdapter(getSupportFragmentManager(), getLifecycle());
          pager.setAdapter(pageAdapter);
  
          pager.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
  
          TabLayout tabLayout = findViewById(R.id.tabLayout2);
          tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
          new TabLayoutMediator(tabLayout, pager,
                  (tab, position) -&gt; {
                      switch (position) {
                          case 0:
                              tab.setText(getString(R.string.stats_tab));
                              break;
                          case 1:
                              tab.setText(getString(R.string.news_tab));
                              break;
                          case 2:
                              tab.setText(getString(R.string.symptoms_tab));
                              break;
                          case 3:
                              tab.setText(getString(R.string.safety_tab));
                              break;
                          default:
                              break;
                      }
                  }).attach();
  
          // Get data
          new StatsLoader(&quot;https://api.covid19api.com/summary&quot;, pageAdapter, pager).execute();
  
      }
  }
  </code></pre>
  <h2>StatsFragment.java</h2>
  <pre><code>import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  import androidx.annotation.NonNull;
  import androidx.annotation.Nullable;
  import androidx.fragment.app.Fragment;
  import androidx.recyclerview.widget.LinearLayoutManager;
  import androidx.recyclerview.widget.RecyclerView;
  import org.json.JSONArray;
  
  public class StatsFragment extends Fragment {
  
      View view;
      RecyclerView recyclerView;
      JSONArray covidData;
  
      public StatsFragment(JSONArray data) {
          covidData = data;
      }
  
      @Nullable
      @Override
      public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          view = inflater.inflate(R.layout.view_page, container, false);
          recyclerView = view.findViewById(R.id.recyclerView2);
          LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
          recyclerView.setLayoutManager(layoutManager);
          recyclerView.setHasFixedSize(false);
          RecyclerView.Adapter&lt;CustomListAdapter.ListViewHolder&gt; mAdapter = new CustomListAdapter(covidData);
          recyclerView.setAdapter(mAdapter);
          return view;
      }
  }
  </code></pre>
  <h2>StatsLoader.java</h2>
  <pre><code>import android.os.AsyncTask;
  import android.util.Log;
  import androidx.annotation.Nullable;
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.util.ArrayList;
  import javax.net.ssl.HttpsURLConnection;
  
  public class StatsLoader extends AsyncTask&lt;String, Void, JSONArray&gt; {
  
      Exception exception;
      String urlString = &quot;&quot;;
      static JSONArray covidData = new JSONArray();
      ViewPageAdapter pageAdapter;
  
      public StatsLoader(String url, ViewPageAdapter adapter) {
          super();
          urlString = url;
          pageAdapter = adapter;
      }
  
      @Nullable
      @Override
      public JSONArray doInBackground(String ... urls) {
          HttpsURLConnection connection = null;
          BufferedReader reader = null;
  
          try {
  
              URL url = new URL(urlString);
              connection = (HttpsURLConnection) url.openConnection();
              connection.connect();
              InputStream inputStream = connection.getInputStream();
              reader = new BufferedReader(new InputStreamReader(inputStream));
  
              StringBuilder buffer = new StringBuilder();
              String line = &quot;&quot;;
  
              while ((line = reader.readLine()) != null) {
                  buffer.append(line);
              }
  
              JSONObject json = new JSONObject(buffer.toString());
              covidData = json.getJSONArray(&quot;Countries&quot;);
              ArrayList&lt;Object&gt; list = new ArrayList&lt;&gt;();
              for (int i = 0; i &lt; covidData.length(); i++) {
                  list.add(covidData.get(i));
              }
              SortJsonArray sortJsonArray = new SortJsonArray();
              sortJsonArray.sortArray(list, &quot;TotalConfirmed&quot;, false);
              covidData = new JSONArray();
              for (Object object : list) {
                  covidData.put(object);
              }
              return covidData;
  
          } catch (MalformedURLException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          } catch (JSONException e) {
              e.printStackTrace();
          } finally {
              if (connection != null) {
                  connection.disconnect();
              }
              try {
                  if (reader != null) {
                      reader.close();
                  }
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
  
          return null;
      }
  
      protected void onPostExecute(JSONArray coviddata) {
  
          if (this.exception == null) {
              Log.d(&quot;Check&quot;, &quot;Works!&quot;);
              pageAdapter.addFragment(new StatsFragment(coviddata), &quot;Stats&quot;);
          }
  
      }
  
  
  }
  </code></pre>
  <h1>Layouts</h1>
  <h2>activity_main</h2>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;&gt;
  
      &lt;com.google.android.material.appbar.AppBarLayout
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;&gt;
  
          &lt;com.google.android.material.appbar.CollapsingToolbarLayout
              android:id=&quot;@+id/collapsing_toolbar&quot;
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;wrap_content&quot;
              android:layout_gravity=&quot;top&quot;
              android:fitsSystemWindows=&quot;true&quot;
              app:contentScrim=&quot;?attr/colorPrimary&quot;
              app:layout_scrollFlags=&quot;scroll|exitUntilCollapsed&quot;
              app:statusBarScrim=&quot;@null&quot;
              app:titleEnabled=&quot;false&quot;
              android:minHeight=&quot;?attr/actionBarSize&quot; &gt;
  
              &lt;fragment
                  android:id=&quot;@+id/mapView&quot;
                  android:name=&quot;com.mapbox.mapboxsdk.maps.MapFragment&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;350sp&quot;
                  android:apiKey=&quot;@string/mapbox_api_key&quot;
                  android:clickable=&quot;true&quot;
                  android:enabled=&quot;true&quot;
                  android:focusable=&quot;true&quot;
                  app:layout_collapseMode=&quot;parallax&quot; /&gt;
  
              &lt;androidx.appcompat.widget.Toolbar
                  android:id=&quot;@+id/appbar&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;?attr/actionBarSize&quot;
                  android:layout_marginBottom=&quot;48dp&quot;
                  android:gravity=&quot;top&quot;
                  app:layout_collapseMode=&quot;pin&quot;
                  app:layout_scrollFlags=&quot;scroll|exitUntilCollapsed&quot;
                  app:popupTheme=&quot;@style/ThemeOverlay.AppCompat.Dark&quot;
                  app:title=&quot;&quot; /&gt;
  
              &lt;com.google.android.material.tabs.TabLayout
                  android:id=&quot;@+id/tabLayout2&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:layout_gravity=&quot;bottom&quot;
                  app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
                  app:layout_constraintEnd_toEndOf=&quot;parent&quot;
                  app:layout_constraintStart_toStartOf=&quot;parent&quot;
                  app:layout_constraintTop_toBottomOf=&quot;parent&quot;&gt;
  
              &lt;/com.google.android.material.tabs.TabLayout&gt;
  
  
          &lt;/com.google.android.material.appbar.CollapsingToolbarLayout&gt;
      &lt;/com.google.android.material.appbar.AppBarLayout&gt;
  
      &lt;androidx.constraintlayout.widget.ConstraintLayout
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_gravity=&quot;bottom&quot;
          app:layout_behavior=&quot;@string/appbar_scrolling_view_behavior&quot;&gt;
  
          &lt;androidx.viewpager2.widget.ViewPager2
              android:id=&quot;@+id/view_pager&quot;
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;match_parent&quot;
              app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
              app:layout_constraintEnd_toEndOf=&quot;parent&quot;
              app:layout_constraintStart_toStartOf=&quot;parent&quot;
              app:layout_constraintTop_toBottomOf=&quot;parent&quot;
              tools:listitem=&quot;@layout/view_page&quot; &gt;
  
          &lt;/androidx.viewpager2.widget.ViewPager2&gt;
  
  
      &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  
  
  &lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;
  </code></pre>
  <h2>view_page</h2>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;&gt;
  
      &lt;androidx.recyclerview.widget.RecyclerView
          android:id=&quot;@+id/recyclerView2&quot;
          android:layout_width=&quot;0dp&quot;
          android:layout_height=&quot;match_parent&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toBottomOf=&quot;parent&quot;
          tools:listitem=&quot;@layout/list_view&quot;&gt;
  
      &lt;/androidx.recyclerview.widget.RecyclerView&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  </code></pre>
  <h2>list_view</h2>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:card_view=&quot;http://schemas.android.com/apk/res-auto&quot;
      android:orientation=&quot;vertical&quot; android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;&gt;
  
      &lt;androidx.cardview.widget.CardView
          android:id=&quot;@+id/card_view&quot;
          android:layout_gravity=&quot;center&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;wrap_content&quot;
          card_view:cardCornerRadius=&quot;14sp&quot;
          android:layout_margin=&quot;15sp&quot;&gt;
  
          &lt;RelativeLayout
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;match_parent&quot;
              android:padding=&quot;40sp&quot;&gt;
  
              &lt;ImageView
                  android:id=&quot;@+id/image&quot;
                  android:layout_width=&quot;wrap_content&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:layout_toLeftOf=&quot;@+id/name&quot;
                  android:layout_toStartOf=&quot;@+id/name&quot;
                  android:contentDescription=&quot;Image of country flag&quot; /&gt;
  
              &lt;TextView
                  android:id=&quot;@+id/name&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:paddingBottom=&quot;10sp&quot; /&gt;
  
              &lt;TextView
                  android:id=&quot;@+id/cases&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;wrap_content&quot;
                  android:layout_below=&quot;@+id/name&quot; /&gt;
  
          &lt;/RelativeLayout&gt;
  
      &lt;/androidx.cardview.widget.CardView&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>