How to create an Android Tabbed Dialog containing fragments?

后端 未结 4 1734
一生所求
一生所求 2021-02-07 22:46

Can anyone point me to an example or show me how to create a simple Tabbed Dialog in Android where the contents of each tab are Fragments? All the exa

4条回答
  •  无人共我
    2021-02-07 23:01

    Spent a lot of time to get it working, but no luck. The only solution I found is create dummy fragment tabhost and use viewpager with fragments instead of tabhost fragments

    voters_dialog.xml

    
    
            
            
    
            
    
    
    

    Dialog class, the trick is use onCreateView not onCreateDialog

    public class VotersDialog extends DialogFragment {
    
    
        private FragmentTabHost mTabHost;
        private ViewPager viewPager;
        private VotersPagerAdapter adapter;
    
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.voters_dialog, container);
    
            getDialog().setTitle(getArguments().getString("title"));
    
            mTabHost = (FragmentTabHost) view.findViewById(R.id.tabs);
    
            mTabHost.setup(getActivity(), getChildFragmentManager());
            mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Плюсов"), Fragment.class, null);
            mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Минусов"), Fragment.class, null);
    
    
            adapter = new VotersPagerAdapter(getChildFragmentManager(), getArguments());
            adapter.setTitles(new String[]{"Плюсов", "Минусов"});
    
            viewPager = (ViewPager)view.findViewById(R.id.pager);
            viewPager.setAdapter(adapter);
    
            viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int i, float v, int i2) {
                }
    
                @Override
                public void onPageSelected(int i) {
                    mTabHost.setCurrentTab(i);
                }
    
                @Override
                public void onPageScrollStateChanged(int i) {
    
                }
            });
    
            mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
                @Override
                public void onTabChanged(String s) {
                    int i = mTabHost.getCurrentTab();
                    viewPager.setCurrentItem(i);
                }
            });
    
            return view;
        }
    
    
        public class VotersPagerAdapter extends FragmentPagerAdapter {
    
            Bundle bundle;
            String [] titles;
    
            public VotersPagerAdapter(FragmentManager fm) {
                super(fm);
            }
    
            public VotersPagerAdapter(FragmentManager fm, Bundle bundle) {
                super(fm);
                this.bundle = bundle;
            }
    
            @Override
            public Fragment getItem(int num) {
                Fragment fragment = new VotersListFragment();
                Bundle args = new Bundle();
                args.putSerializable("voters",bundle.getSerializable( num == 0 ? "pros" : "cons"));
                fragment.setArguments(args);
                return fragment;
            }
    
            @Override
            public int getCount() {
                return 2;
            }
    
            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }
    
            public void setTitles(String[] titles) {
                this.titles = titles;
            }
        }
    
        public static class VotersListFragment extends ListFragment {
    
            List voters;
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                View view = inflater.inflate(R.layout.list_fragment, container, false);
                return view;
            }
    
            @Override
            public void onActivityCreated(Bundle savedInstanceState) {
    
                super.onActivityCreated(savedInstanceState);
                voters = (ArrayList) getArguments().getSerializable("voters");
    
                ArrayAdapter adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, voters);
                setListAdapter(adapter);
    
                getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                        Intent intent = new Intent(getActivity(), ProfileActivity_.class);
                        String login = voters.get(i);
                        intent.putExtra("login", Utils.encodeString(login.substring(0, login.indexOf("(")).trim()));
                        startActivity(intent);
                    }
                });
    
            }
    
        }
    
    }
    

    Here is result, now you can press tabs or swipe fragments

    enter image description here

提交回复
热议问题