2

我正在尝试使用显示片段的 viewpager 来实现 bottomSheet,如模型图像所示。

容纳 viewpager 的 bottomSheetFragment 使用 childFragmentManager 来管理 viewpager 片段。下面是代码。

MainActivity.java

        @Override
        public void onClick(View view) {
            BottomSheetFilterDialogFragment bottomSheetFilterDialogFragment = new BottomSheetFilterDialogFragment();
            bottomSheetFilterDialogFragment.show(getSupportFragmentManager(), "bottomSheetFrag");

            bottomSheetFilterDialogFragment.setPojoList(simplePojos, MainActivity.this);
        }
    });

BottomSheetFragment.java

    public class BottomSheetFilterDialogFragment extends BottomSheetDialogFragment {

    private static final String TAG = "BottomSheetFilt";
    ViewPager pojo_pager;
    PojoViewPagerAdapter pojoViewPagerAdapter;
    ArrayList<SimplePojo> pojoList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_bottomsheet_filter, container, false);

        Log.i(TAG, "onCreateView: started");

        pojo_pager = (ViewPager) view.findViewById(R.id.pojoPager);
        TabLayout pagerTabStrip = (TabLayout) view.findViewById(R.id.sliding_tabs);

        pojo_pager.setAdapter(pojoViewPagerAdapter);

        pagerTabStrip.setupWithViewPager(pojo_pager);

        return view;
    }

    public void setPojoList(List<SimplePojo> pojoList, FragmentActivity activity) {

        ArrayList<PojoChildFragment> pojoChildFragments = new ArrayList<>();

        Log.i(TAG, "setPojoList: started");

        for (int index = 0; index < pojoList.size(); index++) {

            PojoChildFragment pojoChildFragment = new PojoChildFragment();
            Bundle pojoBundle = new Bundle();

            SimplePojo simplePojo = new SimplePojo();
            simplePojo = pojoList.get(index);

            pojoBundle.putSerializable("simplepojo", simplePojo);
            pojoChildFragment.setArguments(pojoBundle);

            pojoChildFragments.add(pojoChildFragment);
            pojoViewPagerAdapter = new PojoViewPagerAdapter(getChildFragmentManager(), pojoChildFragments);

        }

        pojoViewPagerAdapter.notifyDataSetChanged();
    }

    class PojoViewPagerAdapter extends FragmentStatePagerAdapter {
        private ArrayList<PojoChildFragment> fragments;

        public PojoViewPagerAdapter(FragmentManager fm, ArrayList<PojoChildFragment> pojoFragmentList) {
            super(fm);
            if (fragments == null) {
                fragments = new ArrayList<>();
            }
            this.fragments = pojoFragmentList;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return "" + position;
        }

        @Override
        public Fragment getItem(int position) {
            return this.fragments.get(position);
        }

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

PojoChildFragment.java

    public class PojoChildFragment extends Fragment {


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_pojo_child, container, false);


        return rootView;
    }

    @Override
    public void setArguments(Bundle args) {
        super.setArguments(args);
        SimplePojo pojo = (SimplePojo) args.getSerializable("simplepojo");

    }

}
4

0 回答 0