java重叠导航抽屉和tablayout片段
我对安卓非常陌生,我目前正在用java制作一个应用程序,其中包含一个TableAyout和一个导航抽屉,当我点击TableAyout的选项卡项时,片段工作得非常好!,但是当我在tabitem的一个片段中,并且我从导航抽屉中选择了一个选项时,片段开始重叠,没有回头路了,希望你能帮助我,因为我不知道还能做什么。我把密码留给你,看你能不能帮我
梅因。类别:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar;
NavigationView navigationView;
//variables para cargar el fragment
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
String personName;
ViewPager2 viewPager2;
ViewPagerAdapter adapter;
TabLayout tabLayout;
TabItem firstItem, secondItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);//se pasa el toolbar
viewPager2 = findViewById(R.id.vp_horizontal_ntb);
tabLayout = findViewById(R.id.tablayout);
firstItem = findViewById(R.id.perfil_user);
secondItem = findViewById(R.id.Debate);
drawerLayout = findViewById(R.id.drawer);
navigationView = findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
actionBarDrawerToggle.syncState();
adapter = new ViewPagerAdapter(this);
viewPager2.setAdapter(adapter);
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) {
}
});
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == R.id.user) {
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content, new perfil_user());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
} else if (item.getItemId() == R.id.friendslist) {
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content, new addFriends());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
} else if (item.getItemId() == R.id.exit) {
finish();
} else {
//pass
}
//aquí van las otros fragment
drawerLayout.closeDrawer(GravityCompat.START);
return false;
}
}
查看页面雷达。类别:
public class ViewPagerAdapter extends FragmentStateAdapter {
public ViewPagerAdapter(@NonNull MainActivity fragment) {
super(fragment);
}
@NonNull
@Override
public Fragment createFragment(int position) {
switch (position){
case 0:
return new perfil_user();
case 1:
return new genera_debate();
default:
return null;
}
}
@Override
public int getItemCount() {
return 2;
}
}
梅因。xml:
<安卓x.drawerlayout.widget.DrawerLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:id="@+id/drawer"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context=".MainActivity">
<include
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
layout="@layout/drawer_toolbar"
>
</include>
<include
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
layout="@layout/content_main"
>
</include>
<include
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
layout="@layout/test"
>
</include>
<com.google.安卓.material.navigation.NavigationView
安卓:id="@+id/navigationView"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
app:menu="@menu/drawer_menu"
app:headerLayout="@layout/drawer_header"
安卓:layout_gravity="start"
安卓:fitsSystemWindows="true"
>
</com.google.安卓.material.navigation.NavigationView>
</安卓x.drawerlayout.widget.DrawerLayout>
康顿梅因。xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:orientation="vertical" 安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
>
<FrameLayout
安卓:id="@+id/container"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
抽屉头。xml:
<安卓x.constraintlayout.widget.ConstraintLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent">
<TextView
安卓:id="@+id/textView"
安卓:layout_width="match_parent"
安卓:layout_height="130dp"
安卓:text="Menú principal"
安卓:textStyle="bold"
安卓:textSize="30sp"
安卓:background="#0AA842"
安卓:padding="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</安卓x.constraintlayout.widget.ConstraintLayout>
抽屉工具条。xml
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
安卓:orientation="vertical"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent">
<安卓x.appcompat.widget.Toolbar
安卓:id="@+id/toolbar"
安卓:layout_width="match_parent"
安卓:layout_height="?attr/actionBarSize"
安卓:background="#067B30"
安卓:theme="@style/ThemeOverlay.AppCompat.Dark"
/>
<com.google.安卓.material.tabs.TabLayout
安卓:id="@+id/tablayout"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content">
<com.google.安卓.material.tabs.TabItem
安卓:id="@+id/perfil_user"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="Perfil de usuario" />
<com.google.安卓.material.tabs.TabItem
安卓:id="@+id/Debate"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="Debate" />
</com.google.安卓.material.tabs.TabLayout>
<安卓x.viewpager2.widget.ViewPager2
安卓:id="@+id/vp_horizontal_ntb"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
</LinearLayout>
# 1 楼答案
你得到的布局错误是因为
navigation view layout
在你的view pager layouts
前面,所以,我在你的主要活动中添加了一个frame layout
,这将让你在布局之间自由移动而不会出错只需检查您的新代码:
主要活动
主要活动。xml
抽屉菜单。xml
然后我将您的
view pager
及其节点移动到fragment
a1_片段
a1_主布局。xml(新的片段布局)
在这个效果之后,您的菜单中将有一个名为
Home
的项目,它将允许您返回主页面,因为当您从navigation view
单击项目时,主ui中将替换一个新的片段,当您单击Home
项目时,您的第一个ui将返回希望我能帮助你