AlertDialog中的选项卡式Viewpager
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AlertDialog中的选项卡式Viewpager相关的知识,希望对你有一定的参考价值。
我无法在其中创建带有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;
}
答案
如果您在正常活动中实现它可能会容易得多,并为您的活动提供像Theme.AppCompat.Light.Dialog
这样的主题
以上是关于AlertDialog中的选项卡式Viewpager的主要内容,如果未能解决你的问题,请参考以下文章
约束布局中的 ViewPager 选项卡式活动在底部被剪切以用于 recyclerview
为啥这段代码在运行时会崩溃?它只是选项卡式视图中 SwiftUI 中的选择器