所以我知道关于我的问题有很多回答的问题,但是我不知道为什么它不起作用
导航抽屉正在按预期方式工作,但在实现选项卡时遇到了麻烦。创建项目时,如果有帮助,我会在开始时选择抽屉活动
寻求帮助
所以,我清理了代码并实现了新版本,为以后的问题,我将发布所有代码
这是主要活动
package com.example.myapplicationteeeeeeeeeest;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import android.view.View;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.google.android.material.navigation.NavigationView;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.Menu;
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
Toolbar toolbar = findViewById( R.id.toolbar );
setSupportActionBar( toolbar );
FloatingActionButton fab = findViewById( R.id.fab );
fab.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make( view, "Replace with your own action", Snackbar.LENGTH_LONG )
.setAction( "Action", null ).show();
}
} );
DrawerLayout drawer = findViewById( R.id.drawer_layout );
NavigationView navigationView = findViewById( R.id.nav_view );
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
R.id.nav_tools, R.id.nav_share, R.id.nav_send )
.setDrawerLayout( drawer )
.build();
NavController navController = Navigation.findNavController( this, R.id.nav_host_fragment );
NavigationUI.setupActionBarWithNavController( this, navController, mAppBarConfiguration );
NavigationUI.setupWithNavController( navigationView, navController );
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate( R.menu.main, menu );
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController( this, R.id.nav_host_fragment );
return NavigationUI.navigateUp( navController, mAppBarConfiguration )
|| super.onSupportNavigateUp();
}
}
主要活动的布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.drawerlayout.widget.DrawerLayout>
这是我要在其中添加选项卡的片段(图库片段)
package com.example.myapplicationteeeeeeeeeest.ui.gallery;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTabHost;
import androidx.lifecycle.ViewModelProviders;
import androidx.viewpager.widget.ViewPager;
import com.example.myapplicationteeeeeeeeeest.MainActivity;
import com.example.myapplicationteeeeeeeeeest.R;
import com.google.android.material.tabs.TabLayout;
public class GalleryFragment extends Fragment {
private GalleryViewModel galleryViewModel;
private ViewPager viewPager;
private TapAdapter tapAdapter;
private TabLayout tabLayout;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
galleryViewModel =
ViewModelProviders.of( this ).get( GalleryViewModel.class );
View root = inflater.inflate( R.layout.fragment_gallery, container, false );
viewPager = (ViewPager) root.findViewById(R.id.viewPager);
tabLayout = (TabLayout) root.findViewById(R.id.tabLayout);
tapAdapter = new TapAdapter( this,getChildFragmentManager() );
tapAdapter.addFragment(new ShoppingListTab(), "Shopping List");
tapAdapter.addFragment(new RecipeTab(), "Receipt List");
viewPager.setAdapter(tapAdapter);
tabLayout.setupWithViewPager(viewPager);
return root;
}
}
画廊片段的布局文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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=".MainActivity">
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
两个标签片段(RecipeTab和ShoppingListTab)
package com.example.myapplicationteeeeeeeeeest.ui.gallery;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import com.example.myapplicationteeeeeeeeeest.R;
public class RecipeTab extends GalleryFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view1 = inflater.inflate( R.layout.tab_fragment_one, container, false );
return view1;
}
}
package com.example.myapplicationteeeeeeeeeest.ui.gallery;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.myapplicationteeeeeeeeeest.R;
public class ShoppingListTab extends GalleryFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view1 = inflater.inflate( R.layout.tab_fragment_two, container, false );
return view1;
}
}
两个选项卡的布局文件
<?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="match_parent">
</LinearLayout>
<?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="match_parent">
</LinearLayout>
和tapAdapter
package com.example.myapplicationteeeeeeeeeest.ui.gallery;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import java.util.ArrayList;
import java.util.List;
public class TapAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
TapAdapter(GalleryFragment galleryFragment, FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
}
我希望我什么都没错过