0

Basically I just want to have a view pager in a dialog. That's it, but for some reason, the DIALOG IS EMPTY?

Here is my dialog code:

final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.voicedialog);
    dialog.setCanceledOnTouchOutside(false);

    MyPageAdapter adapter = new MyPageAdapter();
    ViewPager pager = (ViewPager) dialog.findViewById(R.id.viewpager);
    pager.setOffscreenPageLimit(3);
    pager.setAdapter(adapter);

    dialog.show();

And here is the dialog layout, voicedialog, just an empty view pager:

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    />

Here's the adapter:

public class MyPageAdapter extends PagerAdapter {
    public Object instantiateItem(ViewGroup collection, int position) {

        int resId = 0;
        switch (position) {
            case 0:
                resId = R.id.voice1;
                break;
            case 1:
                resId = R.id.voice2;
                break;
            case 2:
                resId = R.id.voice3;
                break;
        }
        return collection.findViewById(resId);
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View)object);
    }
}

SO SIMPLE, RIGHT? However, when I run this code, I get an EMPTY dialog:

Instead, it should show my three layouts (voice1, voice2, voice3) that I can swipe through! Those layouts just have a textview, but it is not showing.

Please let me know how I can accomplish this. It's frustrating-I've spent almost 6 hours trying to solve this :)

Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83
  • you get any logcat warning? – Divyesh Patel Dec 26 '16 at 05:45
  • @Divyesh No :(( – Ruchir Baronia Dec 26 '16 at 05:48
  • Do you need to add the views to the passed in collection in `instantiateItem`? I'm not super familiar with view pagers, but the documentation states mentions ". By the time finishUpdate returns the views associated with the key objects returned by instantiateItem should be added to the parent ViewGroup passed to these methods" - also the accepted example [shown here](http://stackoverflow.com/questions/7277892/instantiateitem-in-pageradapter-and-addview-in-viewpager-confusion) adds the view in `instantiateItem`. – Lyla Dec 26 '16 at 05:57
  • @Lyla Yes, I'm trying to add the three layouts `voice1` `voice2` and `voice3`, which each just contain a textview onto the three pages of the ViewPager. So are you suggesting to call a `.add` method in `instantiateItem`? – Ruchir Baronia Dec 26 '16 at 06:02
  • @Lyla If so, then what's the point of returning the ID in the first place? – Ruchir Baronia Dec 26 '16 at 06:03
  • @Lyla I'm pretty confused on this myself :/ – Ruchir Baronia Dec 26 '16 at 06:03
  • @RuchirBaronia yeah, this is a bit of a guess, I'd try calling `((ViewPager) collection).addView(layout);` and see if it helps. The linked post describes him constructing his layout and after that " Then the whole Layout is added to the ViewPager. And finally the Layout is also returned." So it sounds like you need to add the view and return it - but this is a guess – Lyla Dec 26 '16 at 06:10
  • @Lyla If I try doing `collection.addView(findViewById(resId));`, I get `cannot resolve findViewById`. How should I reference the layout to add to the collection. I think you're right by the way, because it makes sense to add it to the ViewGroup before returning. – Ruchir Baronia Dec 26 '16 at 06:14
  • Use should add view to the ViewGroup collection not findiviewbyId – sohan shetty Dec 26 '16 at 06:36
  • @sohanshetty How should I do that? – Ruchir Baronia Dec 26 '16 at 06:41
  • @RuchirBaronia in the [accepted answer](http://stackoverflow.com/questions/7277892/instantiateitem-in-pageradapter-and-addview-in-viewpager-confusion) they use an inflater to inflate the view, from the id, and then add it to the collection. -- This is actually looks pretty similar to how you inflate and add the layout for a Fragment in onCreateView of the Fragment class. -- In the example they also return the inflated view. As your code currently is, does `collection.findViewById(resId);` actually return a valid view? – Lyla Dec 26 '16 at 06:53
  • @Lyla Let's continue this discussion in chat http://chat.stackoverflow.com/rooms/131473/viewpager-question – Ruchir Baronia Dec 26 '16 at 06:54

2 Answers2

0

Firstly inside your PagerAapter get the context , list and LayoutInflator through constructor like this

Context mContext;
LayoutInflater mLayoutInflater;
List<String> mList;

public MyPageAdapter(Context context, List<String> list) {
    mContext = context;
    mList = list;
    mLayoutInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

Then return your list size in getItemCount method

@Override
public int getCount() {
    return mList.Size;
}

Then

public Object instantiateItem(ViewGroup collection, int position) {
     View view = inflater.inflate(R.layout.your_layout, null);
     TextView textView = (TextView) layout.findViewById(R.id.text_view);
     textView.setText(mList.get(postiion);
     collection.addView(view);
     return view;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
     container.removeView((View)object);
  }
}
sohan shetty
  • 289
  • 1
  • 16
0

Follow below steps to open viewPager in dialog

1 - On button click, open fragment that extends DialogFragment, declare and initialize viewpager in that DialogFragment

2 - Next create an adapter that extends FragmentPagerAdapter that is from android.support.v13.app.FragmentPagerAdapter, for this adapter you have to use this gradle:- compile 'com.android.support:support-v13:+'

Open DialogFragment on button click

private void ShowDialog() {
    DialogViewPager dialogViewPager = new DialogViewPager();
    dialogViewPager.show(getFragmentManager(),"DialogViewPager");
}

DialogFragment

public class DialogViewPager extends DialogFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.dialog_layout, container);
    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ViewPager viewPager = (ViewPager) getView().findViewById(R.id.view_pager);
    viewPager.setAdapter(new MyViewPagerAdapter(getChildFragmentManager()));
}

class MyViewPagerAdapter extends FragmentPagerAdapter {

    public MyViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return FirstFragment.newInstance();
            case 1:
                return SecondFragment.newInstance();
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return 2;
    }
  }
}

First Fragment

Second Fragment

Bhavnik
  • 2,020
  • 14
  • 21