gpt4 book ai didi

java - 当用户可见时更新 View 寻呼机上的 fragment

转载 作者:行者123 更新时间:2023-11-30 01:51:44 24 4
gpt4 key购买 nike

我有 50 个页面,需要不断更新当前 fragment ,ChangeListener 工作正常但第一次刷新不起作用,当 fragment 对用户可见时如何实现 fragment 刷新?

MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager(), data);
mViewPager.setAdapter(adapter);
int position = data.size() - 1;
adapter.getFragment(position).refresh();
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

@Override
public void onPageSelected(int position) {
adapter.getFragment(position).refresh();
}

@Override
public void onPageScrollStateChanged(int state) {}
});

页面适配器

private class MyPagerAdapter extends FragmentStatePagerAdapter {

private final List<DateTime> mData;
private Map<Integer, PagerFragment> mMap = new HashMap<>();

public MyPagerAdapter(FragmentManager fm, List<DateTime> data) {
super(fm);
mData = data;
}

@Override
public Fragment getItem(int position) {
return PagerFragment.newInstance(mData.get(position));
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
PagerFragment f = (PagerFragment) super.instantiateItem(container, position);
mMap.put(position, f);
return f;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
mMap.remove(position);
}

@Override
public int getCount() {
return mData.size();
}

public PagerFragment getFragment(int position) {
return mMap.get(position);
}
}

最佳答案

第一个问题不正确,我需要“当它们在 View 寻呼机上对用户可见时更新 fragment ”,我找到了答案:

编辑:

public abstract class PageFragment extends Fragment {

protected Boolean mVisibleToUserFlag = null;

@Override
public void onResume() {
super.onResume();

if (mVisibleToUserFlag != null && mVisibleToUserFlag) {
onVisible();
}
}

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);

if (isVisibleToUser && (mVisibleToUserFlag == null || mVisibleToUserFlag == false)) {
mVisibleToUserFlag = true;
if (isResumed()) {
onVisible();
}
} else if (!isVisibleToUser && mVisibleToUserFlag == null) {
mVisibleToUserFlag = false;
} else if (!isVisibleToUser && mVisibleToUserFlag) {
onHide();
mVisibleToUserFlag = false;
}
}

@Override
public void onDestroyView() {
super.onDestroyView();

mVisibleToUserFlag = null;
}

/**
* This method invoked when page stand visible to user.
*/
public abstract void onVisible();

/**
* This method invoked then page change state visibility change to hidden.
*/
public abstract void onHide();

关于java - 当用户可见时更新 View 寻呼机上的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32937131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com