有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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) 个答案

  1. # 1 楼答案

    你得到的布局错误是因为navigation view layout在你的view pager layouts前面,所以,我在你的主要活动中添加了一个frame layout,这将让你在布局之间自由移动而不会出错

    只需检查您的新代码:

    主要活动

    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;
    
    
    @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
    
        getSupportFragmentManager().beginTransaction().replace(R.id.main_container_,new a1_fragment()).addToBackStack(null)
                .commit();
    
        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();
    
    
       /* FragmentManager fragmentManager;
        FragmentTransaction fragmentTransaction;
        fragmentManager=getSupportFragmentManager();
        fragmentTransaction=fragmentManager.beginTransaction().setReorderingAllowed(true);
        fragmentTransaction.add(R.id.container, new perfil_user(),null);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();*/
    
    
    }
    
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        if(item.getItemId() == R.id.home_item){
            getSupportFragmentManager().beginTransaction().replace(R.id.main_container_,new a1_fragment()).addToBackStack(null)
                    .commit();
        }
        if (item.getItemId() == R.id.user) {
            FragmentManager fragmentManager;
            FragmentTransaction fragmentTransaction;
            fragmentManager = getSupportFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.main_container_, 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.main_container_, 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;
    }
    
    }
    

    主要活动。xml

    <?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    
    <FrameLayout
        android:id="@+id/main_container_"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:menu="@menu/drawer_menu"
        app:headerLayout="@layout/drawer_header"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        >
    
    </com.google.android.material.navigation.NavigationView>
    
    
    
    </androidx.drawerlayout.widget.DrawerLayout>
    

    抽屉菜单。xml

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:title="Selecciona una opción">
    <menu>
    
        <item
            android:id="@+id/home_item"
            android:title="Home"/>
    
        <item
            android:id="@+id/user"
            android:icon="@drawable/user"
            android:title="Perfil de usuario">
    
        </item>
        <item
            android:id="@+id/friendslist"
            android:icon="@drawable/group"
            android:title="Lista de amigos">
    
        </item>
        <item
            android:id="@+id/debate"
            android:icon="@drawable/stars"
            android:title="Debates">
    
        </item>
        <item
            android:id="@+id/search"
            android:icon="@drawable/search"
            android:title="Buscar">
    
        </item>
        <item
            android:id="@+id/exit"
            android:icon="@drawable/exit"
            android:title="Salir">
    
        </item>
    
    </menu>
    
    </item>
    </menu>
    

    然后我将您的view pager及其节点移动到fragment

    a1_片段

    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    import androidx.fragment.app.Fragment;
    import androidx.viewpager2.widget.ViewPager2;
    
    import com.google.android.material.tabs.TabItem;
    import com.google.android.material.tabs.TabLayout;
    
    public class a1_fragment extends Fragment {
    
    ViewPager2 viewPager2;
    ViewPagerAdapter adapter;
    TabLayout tabLayout;
    TabItem firstItem, secondItem;
    
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup 
    container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.a1_main_layout,null);
    
        viewPager2 = view.findViewById(R.id.vp_horizontal_ntb);
        tabLayout = view.findViewById(R.id.tablayout);
        firstItem = view.findViewById(R.id.perfil_user);
        secondItem = view.findViewById(R.id.Debate);
        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) {
    
            }
        });
        return view ;
    }
    }
    

    a1_主布局。xml(新的片段布局)

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <include
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/drawer_toolbar"
        >
    
    </include>
    <include
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/content_main"
        >
    
    </include>
    <include
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/test"
        >
    
    </include>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    在这个效果之后,您的菜单中将有一个名为Home的项目,它将允许您返回主页面,因为当您从navigation view单击项目时,主ui中将替换一个新的片段,当您单击Home项目时,您的第一个ui将返回

    希望我能帮助你