How do I use the ViewPager in AlertDialog? - Android 4.0+

I am trying to show a dialog when the user clicks a button. In a dialog box, I want to create a viewer to display multiple slices.

Please give me an example or tutorial. I have searched and used many code examples. But they still cannot work.

EDIT: My current code

  • Class for customizing 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 Box: LinearLayout contains a 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 a dialog:

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

      

This can show dialogue, but only with a blue background. The ViewPager cannot slide like I want.

+3


source to share


1 answer


AlertDialog.Builder.setView (View) is a method that allows you to add a custom view to the content area of ​​a dialog.



You can inflate the intricate look that comes from ViewPager

; or create a new instance ViewPager

when using this method.

+1


source







All Articles