如何创建包含片段的Android选项卡式对话框?

任何人都可以给我一个例子,或者告诉我如何在Android中创建一个简单的Tabbed Dialog ,其中每个选项卡的内容都是Fragments ? 我发现的所有示例/教程都是关于Fragments和Tabs,但没有特定于DialogFragments

FragmentTabHost的文档显示了如何使用getChildFragmentManager()在正常片段中创建选项卡。 我假设当片段是DialogFragment时它也应该工作但是当我尝试它时我得到:

 java.lang.IllegalStateException: Fragment does not have a view at android.support.v4.app.Fragment$1.findViewById(Fragment.java:1425) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:901) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088) ... 

这是我设置视图的代码(然后传递给AlertDialog.setView() ):

 private void setupView(View v) { mTabHost = (FragmentTabHost) v.findViewById(android.R.id.tabhost); mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent); mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab1"), MyDialogFragment.class, null); } 

花了很多时间让它工作,但没有运气。 我发现的唯一解决方案是创建虚拟片段tabhost并使用带有片段而不是tabhost片段的viewpager

voters_dialog.xml

      

对话类,诀窍是使用onCreateView而不是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); } }); } } } 

结果如下,现在您可以按标签或滑动片段

在此处输入图像描述

我一直在寻找相同的解决方案,但没有任何效果,所以我构建了自己的包含片段的Android选项卡式对话框 。 可能它可以帮助某人。

你可以从这里查看完整的源代码

您应该使用DialogFragmentTabLayoutViewPager 。 例如,对话框片段的视图可能如下所示:

     

构建适配器并确保覆盖getPageTitleMethod,例如:

 public class CustomAdapter extends FragmentPagerAdapter { List mFragmentCollection = new ArrayList<>(); List mTitleCollection = new ArrayList<>(); public CustomAdapter(FragmentManager fm) { super(fm); } public void addFragment(String title, Fragment fragment) { mTitleCollection.add(title); mFragmentCollection.add(fragment); } //Needed for @Override public CharSequence getPageTitle(int position) { return mTitleCollection.get(position); } @Override public Fragment getItem(int position) { return mFragmentCollection.get(position); } @Override public int getCount() { return mFragmentCollection.size(); } } 

在对话框的OnCreateView中,您应该使用tabLayout设置viewpager:

  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootview = inflater.inflate(R.layout.dialog_sample,container,false); tabLayout = (TabLayout) rootview.findViewById(R.id.tabLayout); viewPager = (ViewPager) rootview.findViewById(R.id.masterViewPager); CustomAdapter adapter = new CustomAdapter(getChildFragmentManager()); adapter.addFragment("Boy",CustomFragment.createInstance("John")); adapter.addFragment("Girl",CustomFragment.createInstance("Stacy")); adapter.addFragment("Robot",CustomFragment.createInstance("Aeon")); viewPager.setAdapter(adapter); tabLayout.setupWithViewPager(viewPager); return rootview; } 

我在这里写了一篇博文: 这里 。

请尝试使用DialogFragment,并在对话框片段布局中包含fragmentTransaction的LinearLayout。

 FragmentTransaction t = getSupportFragmentManager().beginTransaction(); String Tag = fragment.getClass().getSimpleName(); t.replace(R.id.llTabFragmentContainer, fragment, Tag); t.commit(); 

片段是您的标签片段。 cheeers