Tablayout / Viewpager

时间:2018-08-13 08:26:51

标签: android android-fragments android-recyclerview android-adapter android-tablayout

所以我急切地想找到解决我问题的方法。我花了几个小时尝试重试一些事情,但是那没有用。 所以这是我的问题:

我的应用程序只有一个活动,即主要活动。 此活动包含底部导航栏和框架布局,以根据底部导航栏上项目的选择加载片段。

我的PredictionsFragment(由底部导航栏加载)包含一个TabLayout(使用SmartTabLayout库)。在此片段中,数据列表被加载到线程中,因为它使用Internet连接。 TabLayout包含显示RecyclerView的PredictionsViewFragment。 当我在底部导航栏中选择PredictionsFragment时,将完美显示tablayout和所有recyclerview。 但是当我转到另一个项目并返回到PredictionsFragment时,它将显示一个空白视图...

PredictionsFragment 代码:

public class PredictionsFragment extends Fragment {

    private static List<Country> countryList = new ArrayList<>();
    private ViewPager viewPager;
    private SmartTabLayout viewPagerTab;
    private FragmentStatePagerItemAdapter adapter;
    private FragmentPagerItems pages;
    private ProgressBar progressBar;
    private ViewGroup tab;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_predictions_tab, container, false);
        initView(view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (countryList.size() == 0) {
            refresh();
        } else {
            refreshViewPager();
        }
    }

    private void initView(View view) {
        tab = (ViewGroup) view.findViewById(R.id.tab);
        tab.addView(LayoutInflater.from(getContext()).inflate(R.layout.fragment_view_predictions, tab, false));
        progressBar = (ProgressBar) view.findViewById(R.id.progressbar);
        viewPager = (ViewPager) view.findViewById(R.id.viewpager);
        viewPagerTab = (SmartTabLayout) view.findViewById(R.id.viewpagertab);

        viewPager.setOffscreenPageLimit(countryList.size());

        pages = new FragmentPagerItems(getContext());
        adapter = new FragmentStatePagerItemAdapter(getFragmentManager(), pages){
            public int getItemPosition(Object object) {
                return POSITION_NONE;
            }
        };
        viewPager.setAdapter(adapter);
        viewPagerTab.setViewPager(viewPager);
    }

    private void refresh() {
        // Thread car connexion internet
        new Thread(new Runnable() {
            @Override
            public void run() {
                countryList = InternetData.getCountryList();
                refreshViewPager();
            }
        }).start();
    }


    private void refreshViewPager() {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
            for (Country country : countryList) {
                pages.add(FragmentPagerItem.of(country.getCode(), PredictionsViewFragment.class));
            }
            adapter.notifyDataSetChanged();
            viewPagerTab.setViewPager(viewPager);
            progressBar.setVisibility(View.GONE);
            }
        });
    }

    public static List<Prediction> getPredictionList(int index) {
        return countryList.get(index).getPredictionList();
    }
}

PredictionsViewFragment 代码:

public class PredictionsViewFragment extends Fragment {
    private RecyclerView recyclerView;
    private PredictionAdapter adapter;
    private int position;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.recycler_view_pronos, container, false);
        position = FragmentPagerItem.getPosition(getArguments());
        initRecyclerView(view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    private void initRecyclerView(View view) {
        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);
        adapter = new PredictionAdapter(PredictionsFragment.getPredictionList(position));
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(adapter);
    }
}

Screenshot of the blank fragment and the app

编辑:

当我使用getChildFragmentManager()方法时:

adapter = new FragmentStatePagerItemAdapter(getChildFragmentManager(), pages){
            public int getItemPosition(Object object) {
                return POSITION_NONE;
            }
        };

当我第二次访问Fragment时,我会收到此错误(我第一次执行此操作时,它运行良好):

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.util.SparseArray.get(int)' on a null object reference
                      at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:936)
                      at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:217)
                      at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1461)
                      at android.view.View.dispatchRestoreInstanceState(View.java:18608)
                      at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3821)
                      at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3827)
                      at android.view.View.restoreHierarchyState(View.java:18586)
                      at android.support.v4.app.Fragment.restoreViewState(Fragment.java:494)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1486)
                      at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
                      at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
                      at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
                      at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
                      at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
                      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
                      at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
                      at android.os.Handler.handleCallback(Handler.java:873)
                      at android.os.Handler.dispatchMessage(Handler.java:99)
                      at android.os.Looper.loop(Looper.java:193)
                      at android.app.ActivityThread.main(ActivityThread.java:6669)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

编辑2:

我将这些行添加到PredictionsFragment的onViewCreated方法中:

else {
      refreshViewPager();
     }

但是它绝对不执行任何操作...选项卡的名称正确显示,但recyclerview却不正确... 就像未加载TabLayout中的Fragment

编辑3: 我在我的recyclerview中添加了SwipeRefreshLayout,对于第2个选项卡,我无法进行拉动以刷新,从第3个选项卡中,仅出现一个recyclerview项,并拉动以刷新“作品”,但它仅显示一个项recyclerview

如果您有任何疑问,请告诉我,我会尽力回答! 在此先感谢您的帮助

3 个答案:

答案 0 :(得分:0)

在viewPager中使用setOffsetScreenPageLimit:当您浏览底部标签时,这将帮助您将数据存储在寻呼机片段中。

viewPager.setOffscreenPageLimit(the number of tab you are setting);
  1. 要在回收站视图中刷新数据,可以使用“刷卡刷新”

答案 1 :(得分:0)

所以我解决了我的问题...

您必须使用:

adapter = new FragmentPagerItemAdapter(getChildFragmentManager(), pages);

使用FragmentPagerItemAdapter和 FragmentStatePagerItemAdapter 此外,您必须使用 getChildFragmentManager()

答案 2 :(得分:0)

我也使用BottomNavigationView用viewPager打开tabLayout并面临相同的问题。 FragmentStatePagerAdapter可以用于此目的。

此类处理片段状态的保存和恢复-> It's in the documentation