简体   繁体   English

如何在AlertDialog中使用ViewPager? - Android 4.0+

[英]How to use ViewPager in a AlertDialog? - Android 4.0+

I am trying to show a dialog when user clicks on the button. 我试图在用户点击按钮时显示一个对话框。 In the dialog, I want to create a viewpager to show multiple fragments. 在对话框中,我想创建一个viewpager来显示多个片段。

Please give me a example or tutorial. 请给我一个例子或教程。 I searched and used many example code. 我搜索并使用了许多示例代码。 But they still cannot work. 但他们仍然无法工作。

EDIT: My current code 编辑:我目前的代码

  • The class for customizing the dialog 用于自定义对话框的类

    public class ViewPagerInDialog { 公共类ViewPagerInDialog {

     private final ActionBarActivity context; private AlertDialog.Builder builder; private int currentLv = 1; private static final int NUM_PAGES = 5; private PagerAdapter pagerAdapter; private ViewPager pager; public ViewPagerInDialog(ActionBarActivity context){ this.context = context; } public void show(){ builder = new AlertDialog.Builder(context); LayoutInflater inflater = LayoutInflater.from(context); View view = inflater.inflate(R.layout.level_dialog, null, false); pager = (ViewPager) view.findViewById(R.id.pager); pagerAdapter = new ScreenSlidePagerAdapter(context.getSupportFragmentManager()); pager.setAdapter(pagerAdapter); pager.setCurrentItem(currentLv - 1); builder.setView(view); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); AlertDialog a = builder.create(); a.show(); } 

    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { 私有类ScreenSlidePagerAdapter扩展FragmentStatePagerAdapter {

     private Fragment currentFragment; public Fragment getCurrentFragment() { return currentFragment; } public ScreenSlidePagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return new LevelFragment(); } @Override public void setPrimaryItem(ViewGroup container, int position, Object object) { if (getCurrentFragment() != object) { currentFragment = ((Fragment) object); } super.setPrimaryItem(container, position, object); } @Override public int getCount() { return NUM_PAGES; } 

    } } }}

  • dialog layout: a LinearLayout contains the ViewPager 对话框布局:LinearLayout包含ViewPager

     <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/blue" /> 

  • LevelFragment: LevelFragment:

    public class LevelFragment extends Fragment { 公共类LevelFragment扩展Fragment {

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

    } }

=> To show dialog: =>显示对话框:

 private void showLevels() {
    ViewPagerInDialog dialog = new ViewPagerInDialog((ActionBarActivity) getActivity());
    dialog.show();
}

This can show the dialog but with only the blue background. 这可以显示对话框,但只有蓝色背景。 The ViewPager cannot slide as I want. ViewPager无法按我的意愿滑动。

AlertDialog.Builder.setView(View) is the method which allows to add a custom view in the dialog content area. AlertDialog.Builder.setView(View)是允许在对话框内容区域中添加自定义视图的方法。

You can inflate a compound view which derives from ViewPager ; 您可以膨胀从ViewPager派生的复合视图; or create a new ViewPager instance when using this method. 或者在使用此方法时创建新的ViewPager实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM