Search code examples
androidandroid-viewpagerandroid-dialogfragment

What is the best practice to use a ViewPager inside a Dialog


I'm trying to show a viewPager inside a dialog. So first I tried with a simple Dialog I got no error but an empty view and I couldn't figure out why. Then I read that: a viewPager can't work with a normal Dialog you need to use a DialogFragment.

So now I'm using a DialogFragment but I still have the same problem no ViewPager, I just have the RelativeLayout of my dialog which is displayed but empty.

Here my DialogFragment:

public class NewFeatureDialog extends DialogFragment {

    private Activity activity;
    private Dialog newFeatureDialog;
    private ViewPager viewPager;
    private NewFeatureAdapter adapter;
    private ArrayList<NewFeature> nfList;
    private View view;

    public NewFeatureDialog(Activity activity) {
        this.activity = activity;
    }

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

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        fillNewFeatureList();// create my list of data

        setUi();
    }

    private void setUi() {
        viewPager = (ViewPager)view.findViewById(R.id.view_pager_new_feature);
        adapter = new NewFeatureAdapter(activity, nfList);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(0);
    }
}

Dialog layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_demo_area"
    android:orientation="vertical">

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager_new_feature"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

ViewPager adapter:

public class NewFeatureAdapter extends PagerAdapter {

    private Activity activity;
    private ArrayList<NewFeature> newFeatureList;
    private LayoutInflater layoutInflater;

    public NewFeatureAdapter(Activity activity, ArrayList<NewFeature> newFeatureList) {
        this.activity = activity;
        this.newFeatureList = newFeatureList;
    }

    @Override
    public int getCount() {
        return newFeatureList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((RelativeLayout)object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        layoutInflater = LayoutInflater.from(activity);
        View view = layoutInflater.inflate(R.layout.item_new_feature, container, false);

        ImageView featureScreen = (ImageView) view.findViewById(R.id.image_new_feature);
        TextView featureTitle = (TextView) view.findViewById(R.id.new_feature_title);
        TextView featureDescription = (TextView) view.findViewById(R.id.description_new_feature);

        NewFeature newFeature = newFeatureList.get(position);
        if (newFeature != null) {
            if (featureScreen != null) {
                featureScreen.setImageResource(newFeature.getResourceImage());
            }
            if (featureTitle != null) {
                featureTitle.setText(newFeature.getName());
            }
            if (featureDescription != null) {
                featureDescription.setText(newFeature.getDescription());
            }
        }
        return view;
    }

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

Here everything seems to work fine. I don't have any null values or others error, but just nothing is displayed in my dialog in my app.

My activity extend Activity and I just use inside my dialoFragment like that:

FragmentManager fragmentManager = getFragmentManager();
NewFeatureDialog dialog = new NewFeatureDialog(this);
dialog.show(fragmentManager,"dialogFragment");

What is the best practice here to do what I want ? Is a normal dialog could be used to show a viewPager ?


Solution

  • So here I found a solution about that issue.

    Indeed I forgot a line into instantiateItem(..) method, relative to the moment where the view which is creating in the adapter need to be attached to my viewPager container like that:

    container.addView(view, 0);
    

    Here my instantiateItem(..) method:

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
    
            layoutInflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = layoutInflater.inflate(R.layout.item_new_feature, container, false);
    
            ImageView featureScreen = (ImageView) view.findViewById(R.id.image_new_feature);
            TextView featureTitle = (TextView) view.findViewById(R.id.new_feature_title);
            TextView featureDescription = (TextView) view.findViewById(R.id.description_new_feature);
    
            NewFeature newFeature = newFeatureList.get(position);
            if (newFeature != null) {
                if (featureScreen != null) {
                    featureScreen.setImageResource(newFeature.getResourceImage());
                }
                if (featureTitle != null) {
                    featureTitle.setText(newFeature.getName());
                }
                if (featureDescription != null) {
                    featureDescription.setText(newFeature.getDescription());
                }
            }
            container.addView(view, 0);
            return view;
        }
    

    I checked you can use this in a dialog but also in a fragment, it works fine.