I made a nested action bar tabs with viewpager like this. However, the data does not appear in the Recycler view when I click the menu at first, and then the data is loaded and displayed in the Recycler view after clicking all the menus in order. I want to show the data as soon as I enter the page. What should I do?
when I first enter the page, data won't show up.
after clicking all menus then the list shows up.
Here is my code. I have many menus in tab layout so, I will attach only a part.
merge.java activity code(first tab layout)
package com.eos.youareheroine;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;
import com.eos.youareheroine.MyPage.MyPageActivity;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.tabs.TabLayout;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.kakao.sdk.user.UserApiClient;
import kotlin.Unit;
import kotlin.jvm.functions.Function1;
public class merge extends FragmentActivity {
private Button main;
private ImageButton search;
TabLayout tabs;
MergePageAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_merge);
tabs = findViewById(R.id.merge_tab);
ViewPager viewPager = findViewById(R.id.merge_view_pager);
adapter = new MergePageAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
tabs.setupWithViewPager(viewPager);
main = findViewById(R.id.merge_btn_title);
search = findViewById(R.id.merge_ibtn_search);
Drawable drawable = getDrawable(R.drawable.iv_search_black);
search.setImageDrawable(drawable);
main.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(merge.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(merge.this, SearchPageActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onResume() {
super.onResume();
adapter.notifyDataSetChanged();
public void refresh() {
adapter.notifyDataSetChanged();
}
}
merge.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".merge">
<androidx.appcompat.widget.Toolbar
android:id="@+id/main_toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#EDD0FF"
android:gravity="center"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:titleMargin="4dp"
app:titleTextColor="#000000" />
<Button
android:id="@+id/merge_btn_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00D6D5F4"
android:fontFamily="@font/kalam"
android:gravity="center"
android:textAllCaps="false"
android:stateListAnimator="@null"
android:text="Story for me"
android:textColor="#000000"
android:textSize="26dp"
app:layout_constraintBottom_toBottomOf="@+id/main_toolbar"
app:layout_constraintEnd_toEndOf="@+id/main_toolbar"
app:layout_constraintStart_toStartOf="@+id/main_toolbar"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="@+id/merge_ibtn_search"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginEnd="10dp"
android:background="#00FFFFFF"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="@+id/main_toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/iv_search_black" />
<ImageButton
android:id="@+id/ibtn_navi"
android:layout_width="35dp"
android:layout_height="40dp"
android:layout_marginStart="10dp"
android:background="#00FFFFFF"
android:clickable="false"
android:scaleType="fitXY"
android:tint="#B888FF"
app:layout_constraintBottom_toBottomOf="@+id/main_toolbar"
app:layout_constraintStart_toStartOf="@+id/main_toolbar"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_list" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/merge_tab"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#EDD0FF"
android:elevation="1dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/main_toolbar"
app:tabGravity="fill"
app:tabIndicatorColor="#9C46E6"
app:tabMode="fixed"
app:tabSelectedTextColor="#9C46E6"
app:tabTextColor="#000000">
<com.google.android.material.tabs.TabItem
android:id="@+id/tab_latest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="??" />
<com.google.android.material.tabs.TabItem
android:id="@+id/tab_finish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="??" />
<com.google.android.material.tabs.TabItem
android:id="@+id/tab_rank"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="??" />
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/merge_view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/merge_tab" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/navi_my_page"
app:menu="@menu/navi_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
first tab page adapter(FragRankAdapter.java)
package com.eos.youareheroine;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentStatePagerAdapter;
import java.util.ArrayList;
public class FragRankPageAdapter extends FragmentStatePagerAdapter {
private ArrayList<Fragment> mData;
private ArrayList<String> name;
public FragRankPageAdapter(@NonNull FragmentManager fm){
super(fm);
mData = new ArrayList<>();
mData.add(new frag_rank_week());
mData.add(new frag_rank_month());
mData.add(new frag_rank_hof());
name = new ArrayList<>();
name.add("?? ??");
name.add("?? ??");
name.add("??? ??");
}
public CharSequence getPageTitle(int position){
return name.get(position);
}
@NonNull
@Override
public Fragment getItem(int position) {
return mData.get(position);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public int getItemPosition(@NonNull Object object) {
return POSITION_NONE;
}
}
second tab layout xml(fragment_rank.xml)
(inside in first tab layout)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".rank">
<!-- TODO: Update blank fragment layout -->
<com.google.android.material.tabs.TabLayout
android:id="@+id/frag_rank_tab"
android:layout_width="match_pa
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…