简体   繁体   中英

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.

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 {

     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 {

     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

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

  • LevelFragment:

    public class LevelFragment extends 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.

AlertDialog.Builder.setView(View) is the method which allows to add a custom view in the dialog content area.

You can inflate a compound view which derives from ViewPager ; or create a new ViewPager instance when using this method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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