Recyclerview在ViewPager片段上未刷新

时间:2019-01-08 08:45:20

标签: android android-recyclerview android-viewpager page-refresh

我有一个名为MainActivity.Class的活动,该活动加载诸如homeexplorecommyou之类的片段。首先,当我打开片段探索时,它具有带有4个选项卡的分页器,例如barclubdinerest等。它们全部包含RecyclerView以及从API加载的数据并显示列表,但同时移至Comm的下一个片段。然后从该片段返回到barclubrestdining,列表中没有显示其完全空白。

有关更多流程,请参考附加图片或链接

https://drive.google.com/open?id=1APDzpcDe2HQyr_QRFbT4YjlgfGS8n5zw

ExploreFragment

public class ExploreFragment extends BaseFragment {


    public ExploreFragment() {
    }


    public static ExploreFragment newInstance() {
        return new ExploreFragment();
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        initFrag();
    }

    private void initFrag() {
        exploreBarFragment = ExploreBarFragment.newInstance();
        exploreBarFragment.getMainFragment(ExploreFragment.this);
        exploreClubFragment = ExploreClubFragment.newInstance();
        exploreClubFragment.getMainFragment(ExploreFragment.this);
        exploreDiningFragment = ExploreDiningFragment.newInstance();
        exploreDiningFragment.getMainFragment(ExploreFragment.this);
    }

    MyPagerAdapter adapter;
    public ViewPager pager;
    private PagerSlidingTabStrip tabs;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_explore, container, false);
        MainActivity.getCurrentFragment = Common.CurrentFragmentType.EXPLORE.getValue();
        ((MainActivity) Objects.requireNonNull(getActivity())).setBottomNavigationVisibility(View.VISIBLE);
        ((MainActivity) Objects.requireNonNull(getActivity())).manageSelectedFragmentMenu(false);
        bindingId(view);
        return view;
    }

    private void bindingId(View view) {
        tabs = view.findViewById(R.id.tabs);
        pager = view.findViewById(R.id.pager);
        setUpPager();
    }

    private void setUpPager() {
        pager.setAdapter(null);
        tabs.setViewPager(null);
        adapter = new MyPagerAdapter(Objects.requireNonNull(getActivity()).getSupportFragmentManager());
        pager.setAdapter(adapter);
        tabs.setViewPager(pager);
        pager.setOffscreenPageLimit(adapter.TITLES.length);
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
            }

            @Override
            public void onPageSelected(int i) {
                if (i == 0)
                    exploreBarFragment.onSearch(etExploreTopFilter.getText().toString());
                if (i == 1)
                    exploreClubFragment.onSearch(etExploreTopFilter.getText().toString());
                if (i == 2)
                    exploreDiningFragment.onSearch(etExploreTopFilter.getText().toString());
            }

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

    public class MyPagerAdapter extends FragmentPagerAdapter {

        private final String[] TITLES = {getString(R.string.tab_bars_cap), getString(R.string.tab_clubs_cap), getString(R.string.tab_dining_cap)};

        MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }

        @Override
        public int getCount() {
            return TITLES.length;
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                return exploreBarFragment;
            } else if (position == 1) {
                return exploreClubFragment;
            } else if (position == 2) {
                return exploreDiningFragment;
            }
            return null;
        }
    }
}

ExploreBarFragment

public class ExploreBarFragment extends ScrollTabHolderFragment {

    public ExploreBarFragment() {
    }

    public static ExploreBarFragment newInstance() {
        return new ExploreBarFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_explore_bar, container, false);
        ((MainActivity) Objects.requireNonNull(getActivity())).manageSelectedFragmentMenu(false);
        bindingId(view);
        return view;
    }


    private void bindingId(View view) {
        recyclerViewBars = view.findViewById(R.id.recyclerViewBars);


    }

    private void setRecyclerVIewForBars(final GetListByTypeResponse getListResponse) {


        barListData.addAll(getListResponse.getData());
        if (barListData.isEmpty())
            recyclerViewBars.setVisibility(View.GONE);
        else
            recyclerViewBars.setVisibility(View.VISIBLE);
        exploreListingAdpForBars = null;
        recyclerViewBars.setHasFixedSize(false);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerViewBars.setLayoutManager(layoutManager);
        recyclerViewBars.setItemAnimator(new DefaultItemAnimator());
        exploreListingAdpForBars = new ViewAllListingExploreAdapter(recyclerViewBars, barListData, getActivity());
        recyclerViewBars.setAdapter(exploreListingAdpForBars);
        if (onRefreshAdapter != null && indexForBar == 1)
            onRefreshAdapter.onLoadMore();
        exploreListingAdpForBars.setOnLoadMoreListener(new ViewAllListingExploreAdapter.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                if (indexForBar <= getListResponse.getTotalpages() && getListResponse.getTotalpages() > 1) {
                    indexForBar = indexForBar + 1;
                    getListingByPageAndTypeWise(indexForBar, Common.ListByType.BARS.toString());
                }
            }
        });
    }

    public void onEvent(GetListByTypeResponse getListResponse) {
        if (getListResponse.isSuccess()) {
            if (getListResponse.getType().equalsIgnoreCase(Common.ListByType.BARS.toString()))
                setRecyclerVIewForBars(getListResponse);
            swipeBar.setRefreshing(false);
        }
    }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

使用public class MyPagerAdapter extends FragmentStatePagerAdapter代替public class MyPagerAdapter extends FragmentPagerAdapter

更新 初始化适配器,如下所示。

adapter = new MyPagerAdapter(Objects.requireNonNull(getActivity()).getChildFragmentManager());