如何使用导航抽屉和viewpager/viewpager2在java中制作android应用程序
Posted
技术标签:
【中文标题】如何使用导航抽屉和viewpager/viewpager2在java中制作android应用程序【英文标题】:how to make android app in java using navigation drawer and viewpager/viewpager2 【发布时间】:2021-09-26 03:50:46 【问题描述】:您好,我正在使用 Java 在 android studio 中进行辩论,我打算让该应用程序占用一个导航抽屉和一个 viewpager / viewpager2。我是 androidstudio 中非常新的编程,因此我遵循了几个视频教程将 viewpager / vipager 2 与导航抽屉合并,但到目前为止没有一个对我有用(导航抽屉只对我有用),我尝试的最后一件事是使用 viewpager (因为使用 viepager 2 我在从主服务器调用适配器时遇到问题),但是在编译项目时,模拟器是我留下的黑屏并且它没有显示任何内容。我希望你能帮助我,因为这个项目对我来说非常重要。这是我的代码的一部分:
主要:
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.tabs.TabItem;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import Fragments.generaDebate;
import Fragments.perfil_user;
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;
//variable viewpager
ViewPager pager;
TabLayout tablayout;
TabItem first_item,second_item;
pageAdapter adapter;
@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
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();
//carga viewpager
pager=findViewById(R.id.view);
tablayout=findViewById(R.id.title_menu);
first_item=findViewById(R.id.first_item);
second_item=findViewById(R.id.second_item);
//declarando adapter en el main
FragmentManager fragmentManager;
fragmentManager=getSupportFragmentManager();
adapter=new pageAdapter(fragmentManager, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,tablayout.getTabCount());
pager.setAdapter(adapter);
tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
@Override
public void onTabSelected(TabLayout.Tab tab)
pager.setCurrentItem(tab.getPosition());
@Override
public void onTabUnselected(TabLayout.Tab tab)
@Override
public void onTabReselected(TabLayout.Tab tab)
);
pager.addOnAdapterChangeListener((ViewPager.OnAdapterChangeListener) new TabLayout.TabLayoutOnPageChangeListener(tablayout));
//cargar fragment principal
FragmentTransaction fragmentTransaction;
fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.container, new perfil_user());
fragmentTransaction.commit();
@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.container, new perfil_user());
fragmentTransaction.commit();
else if (item.getItemId() == R.id.exit)
//finish();
loggin login=new loggin();
login.signOut();
//aquí van las otros fragment
drawerLayout.closeDrawer(GravityCompat.START);
return false;
适配器:
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import java.util.ArrayList;
import Fragments.generaDebate;
import Fragments.perfil_user;
public class pageAdapter extends FragmentPagerAdapter
private int tabsNumber;
public pageAdapter(@NonNull FragmentManager fm, int behavior,int tabs)
super(fm, behavior);
@NonNull
@Override
public Fragment getItem(int position)
switch (position)
case 1:
return new perfil_user ();
case 2:
return new generaDebate();
default:
return null;
@Override
public int getCount()
return tabsNumber;
【问题讨论】:
【参考方案1】:导航抽屉是 android 提供的最常见的功能,导航抽屉是一个 UI 面板,用于显示应用的主导航菜单。它也是重要的 UI 元素之一,它提供了更适合用户的操作,例如更改用户配置文件、更改应用程序的设置等。在本文中,已逐步讨论在 android
activity_main.xml
<?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_
android:background="@color/bgColor"
android:layout_
tools:context=".activities.MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_
android:layout_
app:elevation="0dp">
<LinearLayout
android:layout_
android:layout_
android:gravity="center_vertical"
android:background="@color/bgColor"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:orientation="horizontal">
</LinearLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/vp_horizontal_ntb"
android:layout_
android:layout_
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_
android:layout_
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
android:foreground="?attr/selectableItemBackground"
app:menu="@menu/bottom_navigation"
app:elevation="10dp"
app:labelVisibilityMode="labeled"
app:itemIconTint="@color/bottom_navigation_color"
app:itemTextColor="@color/bottom_navigation_color"
app:itemBackground="@color/bottomNavigationBackground"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigationMenu"
android:icon="@android:drawable/ic_menu_sort_by_size"
android:title="Menu" />
<item
android:id="@+id/navigationMyCourses"
android:icon="@drawable/ic_email"
android:title="Courses" />
<item
android:id="@+id/navigationHome"
android:icon="@drawable/ic_home"
android:title="Home" />
<item
android:id="@+id/m_refer"
android:icon="@android:drawable/ic_menu_share"
android:title="Refer" />
<item
android:id="@+id/navigationMyProfile"
android:icon="@drawable/ic_back"
android:title="User" />
</menu>
ViewPagerAdapter.java
public class ViewPagerAdapter extends FragmentStateAdapter
public ViewPagerAdapter(@NonNull @NotNull FragmentActivity fragmentActivity)
super(fragmentActivity);
@NonNull
@NotNull
@Override
public Fragment createFragment(int p)
switch (p)
case 0:
return new ProfileFragment();
case 1:
return new LeaderBoardFragment();
case 2:
return new HomeFragment();
case 3:
return new ReferFragment();
case 4:
return new ProfileFragment();
default:
return new HomeFragment();
@Override
public int getItemCount()
return 5;
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
private void initView()
this.viewPager2 = findViewById(R.id.vp_horizontal_ntb);
viewPager2.setOffscreenPageLimit(100);
this.bottomNavigationView = findViewById(R.id.navigation);
adapter = new ViewPagerAdapter(this);
viewPager2.setAdapter(adapter);
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback()
@Override
public void onPageSelected(int position)
super.onPageSelected(position);
switch (position)
case 0:
bottomNavigationView.getMenu().findItem(R.id.navigationMenu).setChecked(true);
break;
case 1:
bottomNavigationView.getMenu().findItem(R.id.navigationMyCourses).setChecked(true);
break;
case 2:
bottomNavigationView.getMenu().findItem(R.id.navigationHome).setChecked(true);
break;
case 3:
bottomNavigationView.getMenu().findItem(R.id.m_refer).setChecked(true);
break;
case 4:
bottomNavigationView.getMenu().findItem(R.id.navigationMyProfile).setChecked(true);
break;
);
bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener()
@SuppressLint("NonConstantResourceId")
@Override
public boolean onNavigationItemSelected(@NonNull @NotNull MenuItem item)
switch (item.getItemId())
case R.id.navigationHome:
viewPager2.setCurrentItem(2);
break;
case R.id.navigationMyProfile:
viewPager2.setCurrentItem(4);
break;
case R.id.m_refer:
viewPager2.setCurrentItem(3);
break;
case R.id.navigationMyCourses:
viewPager2.setCurrentItem(1);
break;
case R.id.navigationMenu:
viewPager2.setCurrentItem(0);
break;
return false;
);
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) bottomNavigationView.getLayoutParams();
layoutParams.setBehavior(new BottomNavigationBehavior());
bottomNavigationView.setSelectedItemId(R.id.navigationMenu);
快乐编码
【讨论】:
朋友,我如何将它合并到导航抽屉中,我尝试将viewpager 2和底部导航集成到file.xml中,然后将其导入main.xml,最后bottomNavigationView.setOnItemSelectedListener 的一部分也给我带来了问题以上是关于如何使用导航抽屉和viewpager/viewpager2在java中制作android应用程序的主要内容,如果未能解决你的问题,请参考以下文章