我有带tablayout的viewpager,其中有3个片段。在片段2和片段3中,我滑动刷新以更新Firebase数据库中的数据。
我希望它用工具栏中的1个按钮刷新,并刷新其中的所有片段。
1个更多的问题是,我必须刷两次刷新片段才能更新recyclerview ..... 我不知道该怎么做,请帮助
TwoFragment
public static final String TAG = "TWO_FRAGMENT";
@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);
reloadFragment("TWO_FRAGMENT");
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) {
}
protected void reloadFragment(String TAG){
// Reload current fragment
Fragment frg = null;
frg = this.getSupportFragmentManager().findFragmentByTag(TAG);
FragmentTransaction ft =this.getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
}
答案 0 :(得分:1)
您可以使用EventBus,将其放在所有片段中:
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(ViewEvent viewEvent) {
if (viewEvent.getMessage().equals("updateViews")) {
updateView();
}
}
,当您要更新时,可以发送如下事件:
EventBus.getDefault().post(new ViewEvent("updateViews"));
PS :ViewEvent是一个对象,您必须创建该对象并将其发送给所有预定的片段。
public class ViewEvent {
private String message;
public ViewEvent( String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
答案 1 :(得分:0)
只需在按钮单击上调用以下方法即可刷新片段-
protected void reloadFragment(String TAG){
// Reload current fragment
Fragment frg = null;
frg = getActivity().getSupportFragmentManager().findFragmentByTag(TAG);
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
}
通过以下代码行在Fragment中声明您的TAG变量-
public static final String TAG = "HOME_FRAGMENT";
,每当您在活动中推送Fragment时,请使用相同的TAG-
getSupportFragmentManager().beginTransaction().setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out, android.R.anim.fade_in, android.R.anim.fade_out).replace(container, fragment, tag).commit();
您只需要为每个片段都有一个TAG。希望您可以使用此解决方案。