我有viewpager
和tablayout
,并且其中有3个fragment
。在我的fragment
2和3中,我滑动以刷新以更新Firebase数据库中的数据。
问题
我必须刷两次刷新,然后才更新。但是,如果我滑动1次,则什么也不会发生。另一种情况是,我滑动
tablayout
1次,然后回到要刷新的fragment
,然后更新了。
TwoFragment
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
} else {
mAuth = FirebaseAuth.getInstance();
userRef = FirebaseDatabase.getInstance().getReference("user_course");
users = FirebaseAuth.getInstance().getCurrentUser();
daftarTugasRef = FirebaseDatabase.getInstance().getReference("tugas");
mTugasRef = FirebaseDatabase.getInstance().getReference("tugas_course");
mFlagTugas = FirebaseDatabase.getInstance().getReference("flag_tugas");
LoadData();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
v = inflater.inflate(R.layout.fragment_two, container, false);
swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override public void run() {
LoadData();
swipeLayout.setRefreshing(false);
listTugas.clear();
}
private void onLoaded() {
swipeLayout.setEnabled(true);
swipeLayout.setRefreshing(false);
}
}, 500);
}
});
MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(3);
setupViewPager(viewPager);
viewPager.setCurrentItem(position);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
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();
}
class ViewPagerAdapter extends FragmentStatePagerAdapter {
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 int getItemPosition(@NonNull Object object) {
return POSITION_NONE;
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
// viewPager.getAdapter().notifyDataSetChanged();
// adapter.notifyDataSetChanged();
}
@Override
public void onPageScrollStateChanged(int state) {
}