我让viewpager2运行了三个选项卡,并且在每个页面之间都显示了选项卡,但是即使我在onCreateView中夸大了片段,它也在左上角显示了“ Hello空白片段”,所以我不确定这是什么错误。我对片段和viewpager1 / 2没有任何经验,所以这对我来说是全新的。知道为什么它不显示吗?
主要:
public class TabActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
ViewPager2 viewPager2 = findViewById(R.id.viewPager2);
viewPager2.setAdapter(new ScreenSlidePagerAdapter(this));
final TabLayout tabLayout = findViewById(R.id.tabLayout);
tabLayout.getTabAt(1).select();
viewPager2.setUserInputEnabled(false);
/* TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
switch(position){
case 0:
tab.setText("fragment1");
break;
case 1: tab.setText("fragment2");
break;
}
}
});
tabLayoutMediator.attach();
*/
}
private class ScreenSlidePagerAdapter extends FragmentStateAdapter {
public ScreenSlidePagerAdapter(FragmentActivity fa) {
super(fa);
}
@Override
public Fragment createFragment(int position) {
switch (position) {
case 0:
return new SwipeFragment();
case 1:
return new NavigationFragment();
case 2:
return new MatchFragment();
default:
return null;
}
}
@Override
public int getItemCount() {
return 3;
}
}
}
片段示例:
public class NavigationFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment NavigationFragment.
*/
// TODO: Rename and change types and number of parameters
public static NavigationFragment newInstance(String param1, String param2) {
NavigationFragment fragment = new NavigationFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public NavigationFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_navigation, container, false);
}
}
这只是他们给您的样板空白片段。另一方面,与此片段相关联的XML具有很多EFAB和线性布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Tabs.NavigationFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="94"
android:gravity="center_horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginTop="32dp"
android:layout_marginBottom="23dp"
android:gravity="center"
android:orientation="vertical">
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/stats"
app:icon="@drawable/stats_icon"
app:iconTint="@color/color_add"
android:layout_width="188dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:fontFamily="@font/roboto_medium"
android:layoutDirection="rtl"
android:text="Stats"
android:textColor="@color/color_add" />
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/myCard"
app:icon="@drawable/card_icon"
app:iconTint="@color/color_add"
android:layout_width="188dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:fontFamily="@font/roboto_medium"
android:layoutDirection="rtl"
android:text="My Card"
android:textColor="@color/color_add" />
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/filter"
app:icon="@drawable/match_filter_icon"
app:iconTint="@color/color_add"
android:layout_width="188dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:fontFamily="@font/roboto_medium"
android:layoutDirection="rtl"
android:text="Filter"
android:textColor="@color/color_add" />
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/qna"
app:icon="@drawable/qna_icon"
app:iconTint="@color/color_add"
android:layout_width="188dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:fontFamily="@font/roboto_medium"
android:layoutDirection="rtl"
android:text="Q&A"
android:textColor="@color/color_add" />
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/photos"
app:icon="@drawable/photo_icon"
app:iconTint="@color/color_add"
android:layout_width="188dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:fontFamily="@font/roboto_medium"
android:layoutDirection="rtl"
android:text="Photos"
android:textColor="@color/color_add" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
MainActivity XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Tabs.TabActivity"
android:background="@drawable/green_to_white_gradient">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="@android:color/transparent"
app:tabMode="fixed"
app:tabIconTint="@color/color_add"
app:tabIndicatorColor="@color/color_add"
app:tabIndicatorHeight="1dp"
app:tabRippleColor="@color/light_green_color">
<com.google.android.material.tabs.TabItem
android:id="@+id/tab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/menu_icon" />
<com.google.android.material.tabs.TabItem
android:id="@+id/tab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/card_icon" />
<com.google.android.material.tabs.TabItem
android:id="@+id/tab3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/people_icon" />
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" />
</LinearLayout>
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager2.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
也许有更好的选择,但我不知道。
根据此代码更改您的代码:
TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
viewPager2.setCurrentItem(position);
}
});
tabLayoutMediator.attach();