У меня есть viewpager
с 3 фрагментами в нем.В каждом фрагменте я делаю запрос firebase для заполнения recycleview
.Я использую addsinglevaluelistener
для запроса базы данных реального времени Firebase. Проблема:
Когда я обновляю свои данные, затем закрываю приложение и снова открываю его, оно не обновляет recyclerview
.Я должен закрыть его и открыть еще 1 раз для обновления данных.
Это также происходит в моем swipe refresh
в каждом фрагменте, мне нужно swipe refresh
2 раза для обновленияданные.
Я пытался сделать:
- Создать мой запрос в
onResume
, Onstart
- Переопределить
onPageSelected
Это также не работает для меня
Основная активность
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//Tab Layout
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(3);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(position);
authListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user == null) {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
}
};}
private void setupTabIcons() {
TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabOne.setText("Matakuliah");
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.baseline_home_white_24dp, 0, 0);
tabLayout.getTabAt(0).setCustomView(tabOne);
tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabTwo.setText("Tugas");
tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.baseline_assignment_late_white_24dp, 0, 0);
tabLayout.getTabAt(1).setCustomView(tabTwo);
tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabThree.setText("Pengumuman");
tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_notifications_black_24dp, 0, 0);
tabLayout.getTabAt(2).setCustomView(tabThree);
BadgeView badge3 = new BadgeView(getApplicationContext(), tabThree);
}
private void setupViewPager(ViewPager viewPager) {
adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new OneFragment(), "ONE");
adapter.addFrag(new TwoFragment(), "TWO");
adapter.addFrag(new ThreeFragment(), "THREE");
viewPager.setAdapter(adapter);
viewPager.getAdapter().notifyDataSetChanged();
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
switch (position){
case 2 : ((ThreeFragment)adapter.getItem(position)).LoadDataPengumuman();
case 1 : ((TwoFragment)adapter.getItem(position)).LoadData();
viewPager.getAdapter().notifyDataSetChanged();
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
ThreeFragment
@Override
public void onResume() {
super.onResume();
userRef = FirebaseDatabase.getInstance().getReference("user_course");
users = FirebaseAuth.getInstance().getCurrentUser();
mataKuliahRef = FirebaseDatabase.getInstance().getReference("courses");
daftarPengumumanRef = FirebaseDatabase.getInstance().getReference("pengumuman");
pengumumanRef = FirebaseDatabase.getInstance().getReference("pengumuman_course");
mFlagPengumuman = FirebaseDatabase.getInstance().getReference("flag_pengumuman");
LoadDataPengumuman();
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
userRef = FirebaseDatabase.getInstance().getReference("user_course");
users = FirebaseAuth.getInstance().getCurrentUser();
mataKuliahRef = FirebaseDatabase.getInstance().getReference("courses");
daftarPengumumanRef = FirebaseDatabase.getInstance().getReference("pengumuman");
pengumumanRef = FirebaseDatabase.getInstance().getReference("pengumuman_course");
mFlagPengumuman = FirebaseDatabase.getInstance().getReference("flag_pengumuman");
LoadDataPengumuman();
}
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_three, container, false);
userRef = FirebaseDatabase.getInstance().getReference("user_course");
users = FirebaseAuth.getInstance().getCurrentUser();
mataKuliahRef = FirebaseDatabase.getInstance().getReference("courses");
daftarPengumumanRef = FirebaseDatabase.getInstance().getReference("pengumuman");
pengumumanRef = FirebaseDatabase.getInstance().getReference("pengumuman_course");
mFlagPengumuman = FirebaseDatabase.getInstance().getReference("flag_pengumuman");
swipeLayout = (SwipeRefreshLayout) v.findViewById(R.id.swiperefresh);
View b= ((MainActivity)getActivity()).tabThree;
badge3 = new BadgeView(getActivity(), b);
badge3.setBadgeMargin(90,0);
badge3.setTextSize(10);
counter = new Counter() {
@Override
public void setCounter(int count) {
Log.e("Ser_Counter", "total countnya " + asd);
Log.e("Ser_Counter", "masuk count ");
asd=asd+count;
if(asd==0){
Log.e("Ser_Counter", "masuk asd=0");
badge3.hide();
}else{
Log.e("Ser_Counter", "masuk asd=else");
badge3.setText(asd + "");
badge3.show();
}
}
};
swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override public void run() {
swipeLayout.setRefreshing(false);
listPengumuman.clear();
consolidatedList.clear();
LoadDataPengumuman();
}
private void onLoaded() {
swipeLayout.setEnabled(true);
swipeLayout.setRefreshing(false);
}
}, 500);
}
});
mJumlahPengumuman = (TextView) v.findViewById(R.id.jumlah_pengumuman);
mRecyclerView = (RecyclerView) v.findViewById(R.id.PengumumanRView);
mRecyclerView.setHasFixedSize(true);
adapter = new myAdapterPengumuman(getActivity().getApplicationContext(), consolidatedList,counter);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(layoutManager);
RecyclerViewHeader header = (RecyclerViewHeader) v.findViewById(R.id.header3);
header.attachTo(mRecyclerView);
mRecyclerView.setAdapter(adapter);
LoadDataPengumuman();
return v;
}