AlertDialog中的选项卡式Viewpager

问题描述 投票:2回答:1

我无法在其中创建带有Tabbed ViewPager的Custom AlertDialog。我能够在onCreateView中创建一个DialogFragment。但当我尝试将其更改为AlertDialog时,我将代码调整(尝试)到onCreateDialog方法,但在多次尝试后都失败了。

我得到的错误是:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.yoyo.bonosconsulta, PID: 28495
              java.lang.IllegalStateException: Fragment does not have a view

如果不使用dialog_viewpager.xml对视图进行膨胀,而是用fragment_date.xml对其进行膨胀(并删除以下7行代码),它不会给出任何错误。所以问题出在viewpager或tablayout中,我猜。

任何帮助将不胜感激。

这是onCreateView:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder =  new  AlertDialog.Builder(getActivity())
            .setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                            int whichButton) {
                            ((MainActivity) getActivity())
                                    .doPositiveClick();
                        }
                    })
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                            int whichButton) {
                            ((MainActivity) getActivity())
                                    .doNegativeClick();
                        }
                    });

    LayoutInflater i = getActivity().getLayoutInflater();
    View rootview = i.inflate(R.layout.dialog_viewpager,null);

    tabLayout = rootview.findViewById(R.id.tabLayout);
    viewPager = rootview.findViewById(R.id.masterViewPager);
    TabbedDialogAdapter adapter = new TabbedDialogAdapter(getChildFragmentManager());
    adapter.addFragment("Date", TabbedDateFragment.createInstance("John"));
    adapter.addFragment("Time", TabbedTimeFragment.createInstance("Stacy"));
    viewPager.setAdapter(adapter);
    tabLayout.setupWithViewPager(viewPager);

    builder.setView(rootview);
    return builder.create();
}

这是dialog_viewpager.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.design.widget.TabLayout
    android:id="@+id/tabLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
<android.support.v4.view.ViewPager
    android:id="@+id/masterViewPager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />

这是TabbedDialogAdapter:

public class TabbedDialogAdapter extends FragmentPagerAdapter {
List<Fragment> mFragmentCollection = new ArrayList<>();
List<String> mTitleCollection = new ArrayList<>();
public TabbedDialogAdapter(FragmentManager fm) {
    super(fm);
}
public void addFragment(String title, Fragment fragment)
{
    mTitleCollection.add(title);
    mFragmentCollection.add(fragment);
}
//Needed for
@Override
public CharSequence getPageTitle(int position) {
    return mTitleCollection.get(position);
}
@Override
public Fragment getItem(int position) {
    return mFragmentCollection.get(position);
}
@Override
public int getCount() {
    return mFragmentCollection.size();
}

}

最后是fragment_date.xml(fragment_time.xml类似)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
<DatePicker
    android:id="@+id/dp_datepicker"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="0dp"
    android:layout_gravity="center"
    android:background="@color/LVcolor"
    />

为了完成目的,当我有onCreateView()并且它工作(但没有AlertDialog按钮,代码是这样的:

    @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootview = inflater.inflate(R.layout.dialog_viewpager,container,false);
    tabLayout = rootview.findViewById(R.id.tabLayout);
    viewPager = rootview.findViewById(R.id.masterViewPager);
    TabbedDialogAdapter adapter = new TabbedDialogAdapter(getChildFragmentManager());
    adapter.addFragment("Date", TabbedDateFragment.createInstance("John"));
    adapter.addFragment("Time", TabbedTimeFragment.createInstance("Stacy"));
    viewPager.setAdapter(adapter);
    tabLayout.setupWithViewPager(viewPager);
    return rootview;
}
android android-viewpager android-alertdialog
1个回答
0
投票

如果您在正常活动中实现它可能会容易得多,并为您的活动提供像Theme.AppCompat.Light.Dialog这样的主题

© www.soinside.com 2019 - 2024. All rights reserved.