我有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;
}