我对编写应用程序还比较陌生,目前正在第二个项目中工作。我有一个名为AddNetwork
的活动,其中包含一个应打开AlertDialog
的按钮。在AlertDialog
中,我想要一个带有两个标签的TabLayout
,每个标签显示不同的内容。我一直在努力实现这一目标大约8个小时,并且发现了一些教程可以部分解决我的问题,但实际上并没有什么要解决的。我想我可以使用ViewPager2
解决我的问题,但是我还没有设法使它起作用。到目前为止,我的代码如下:
AddNetwork.java
创建AlertDialogSelectWifi.java
的实例,该实例扩展DialogFragment
并扩展包含alertdialog_select_wifi.XML
和TabLayout
的ViewPager2
布局文件。同样在DialogFragment
中,我试图为viewPager
设置适配器,即ViewPagerAdapter.java
,该适配器扩展了FragmentStateAdapter
。从那里,我想附加两个片段AlertDialogRecentFragment.java
(带有fragment_alert_dialog_recent.xml
)和AlertDialogTypeFragment.java
(带有fragment_alert_dialog_type.xml
)。请注意,后者尚未包含在代码中,因此截至目前,我只向选项卡添加一个片段(我想)。因此,目前,我的代码是我发现并遵循的所有教程的混乱混合。它不会引起任何错误,并且我可以打开Alertdialog,但是fragment_alert_dialog_recent.xml
和选项卡不可见。我真的不知道我还能尝试或更改代码中的什么。总的来说,我不确定在ViewPagerAdapter
文件中设置AlertDialogSelectWifi.java
是否正确。我想念什么或做错什么?任何帮助都非常感谢!
这是我的代码:
AddNetwork.java
public class AddNetwork extends AppCompatActivity {
Context context = this;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_network);
//BUTTON
final MaterialButton buttonSelectWifi;
buttonSelectWifi = (MaterialButton) findViewById(R.id.button2);
buttonSelectWifi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//CREATE ALERT DIALOG
FragmentManager fm = getSupportFragmentManager();
DialogFragment dialog = AlertDialogSelectWifi.newInstance(context);
dialog.show(fm, "dialog");
}
});
}
}
AlertDialogSelectWifi.java
public class AlertDialogSelectWifi extends DialogFragment {
static Context mContext;
public static AlertDialogSelectWifi newInstance(Context context) {
mContext = context;
return new AlertDialogSelectWifi();
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AlertDialogTheme);
LayoutInflater inflater = requireActivity().getLayoutInflater(); //getActivity().getLayoutInflater();
View alertLayout = inflater.inflate(R.layout.alertdialog_select_wifi, null);
builder.setTitle("Add Saved Network")
.setView(alertLayout)
.setCancelable(true)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return builder.create();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.alertdialog_select_wifi, container, false);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ViewPager2 viewPager = view.findViewById(R.id.viewpager);
TabLayout tabLayout = view.findViewById(R.id.tablayout);
ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity(), mContext, viewPager);
viewPager.setAdapter(adapter);
new TabLayoutMediator(tabLayout, viewPager,
new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
tab.setText("Tab " + (position + 1));
}
}).attach();
}
}
ViewPageAdapter.java
public class ViewPagerAdapter extends FragmentStateAdapter {
private LayoutInflater mInflater;
private ViewPager2 viewPager;
public ViewPagerAdapter(FragmentActivity fragmentActivity, Context context, ViewPager2 viewPager) {
super(fragmentActivity);
this.mInflater = LayoutInflater.from(context);
this.viewPager = viewPager;
}
@NonNull
@Override
public Fragment createFragment(int position) {
return AlertDialogRecentFragment.newInstance();
}
@Override
public int getItemCount() {
return 0;
}
}
如果我理解您的问题,请像这样正确更改适配器:
public class ViewPagerAdapter extends FragmentStateAdapter {
public ViewPagerAdapter(FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
@NonNull
@Override
public Fragment createFragment(int position) {
if(position == 0)
return new AlertDialogRecentFragment();
else
return new AlertDialogTypeFragment();
}
@Override
public int getItemCount() {
return 2;
}
}