1. Are you ready for the Galaxy S20? Here is everything we know so far!

Is there any way to put a ViewPager2 inside an AlertDialog?

Discussion in 'Android Development' started by Dnlaua, Oct 9, 2020.

  1. Dnlaua

    Dnlaua Lurker
    Thread Starter

    I'm losing my mind. Spent over 5 hours rearranging code thinking I was one step away from fixing the issue due to being able to have half the functionality with one parameter change, and the other half of the functionality with a different parameter. I WAS WRONG. NOT CLOSE. I'm hoping you guys can help me.

    I'm trying to put a ViewPager2 inside of an AlertDialog that's inside of a RecyclerView. I was going back and forth between having the AlertDialog have the .xml with the ViewPager2 scrolling functionality with the save button not working to dismiss dialog, to having the .xml load, save button work, but no ViewPager2 scrolling. Here's my code for the latter:

    editItem()
    Code (Text):
    1.  
    2. {
    3. final AlertDialog.Builder alertDialogBuilder;
    4.  
    5. inflater = LayoutInflater.from(context);
    6.  
    7. final View contentEspressoList = inflater.inflate(R.layout.content_espresso_list, null);
    8. //final View view = inflater.inflate(R.layout.activity_espresso_list, null);
    9. final View editItemPageView = inflater.inflate(R.layout.edit_item_page, null);
    10.  
    11. viewPager2 = (ViewPager2) contentEspressoList.findViewById(R.id.editPager);
    12.  
    13. alertDialogBuilder = new AlertDialog.Builder(context);
    14.  
    15. //viewPager2.removeView(viewPager2.getRootView());
    16.  
    17. alertDialogBuilder.setView(editItemPageView);
    18.  
    19. final AlertDialog dialog = alertDialogBuilder.create();
    20.  
    21. viewPager2.setAdapter(new EditViewPagerAdapter(dialog.getContext()));
    22.  
    23. dialog.show();
    24.  
    25. saveButton.setOnClickListener(new View.OnClickListener()
    26. {
    27.     [USER=1021285]@override[/USER]
    28.     public void onClick(View v)
    29.     {
    30.         dialog.dismiss();
    31.     }
    32. });
    33. }
    34.  
    EditViewPagerAdapter.java
    Code (Text):
    1.  
    2. public class EditViewPagerAdapter extends RecyclerView.Adapter<EditViewPagerAdapter.ViewHolder>
    3. {
    4.     private String mName;
    5.     private LayoutInflater mInflater;
    6.     private Context ctx;
    7.  
    8.     public EditViewPagerAdapter(Context context)
    9.     {
    10.         this.ctx = context;
    11.         this.mInflater = LayoutInflater.from(context);
    12.     }
    13.  
    14.     [USER=1021285]@override[/USER]
    15.     public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    16.     {
    17.         View view = mInflater.inflate(R.layout.edit_item_page, parent, false);
    18.  
    19.         return new ViewHolder(view);
    20.     }
    21.  
     


    #1 Dnlaua, Oct 9, 2020
    Last edited: Oct 9, 2020
Loading...

Share This Page

Loading...