在Dialog中使用ViewPager的最佳做法是什么?

时间:2015-03-25 16:09:08

标签: android android-viewpager android-dialogfragment

我试图在对话框中显示viewPager。所以首先我尝试使用一个简单的Dialog我没有错误,但是一个空视图,我无法弄清楚原因。然后我读到:viewPager无法使用正常的Dialog,你需要使用DialogFragment。

所以现在我使用DialogFragment但是我仍然遇到同样的问题没有ViewPager,我只是显示了对话框的RelativeLayout,但是显示为空。

这是我的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);
    }
}

对话框布局:

<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适配器:

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);
    }
}

这里的一切似乎都很好。我没有任何空值或其他错误,但我的应用中的对话框中没有显示任何内容。

我的活动扩展了Activity,我只是在我的dialoFragment中使用:

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

这里做我想做的最佳做法是什么?是否可以使用普通对话框来显示viewPager?

1 个答案:

答案 0 :(得分:2)

所以我在这里找到了解决这个问题的方法。

实际上我忘记了一行到instantiateItem(..)方法,相对于在适配器中创建的视图需要像我那样附加到我的viewPager容器的那一刻:

container.addView(view, 0);

这是我的instantiateItem(..)方法:

    @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;
    }

我检查过你可以在对话框中使用它,但也可以在片段中使用它,它可以正常工作。