效果图:
实现步骤:
1.首先在app的builde文件中引用ViewPager2
implementation 'androidx.viewpager2:viewpager2:1.0.0-alpha01'
2.添加首页的布局xml
<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent”>
<androidx.viewpager2.widget.ViewPager2
android:id=”@+id/mainViewPager”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:layout_above=”@+id/navigationView” />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id=”@+id/navigationView”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:background=”#FFFFFF” />
</RelativeLayout>
3.创建home Activity页面
package com.example.tasklayoutdemo.tab; import android.os.Bundle; import android.view.MenuItem; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.viewpager2.widget.ViewPager2; import com.example.tasklayoutdemo.R; import com.google.android.material.bottomnavigation.BottomNavigationView; import com.google.android.material.bottomnavigation.LabelVisibilityMode; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.List; public class HomeActivity extends AppCompatActivity { private ViewPager2 mainViewPager; private BottomNavigationView navigationView; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.home_activity); initView(); initDate(); } private void initView() { mainViewPager = findViewById(R.id.mainViewPager); navigationView = findViewById(R.id.navigationView); } private void initDate() { List<Fragment> fragmentArr = new ArrayList<Fragment>(); fragmentArr.add(new HomeShouYeFragment()); fragmentArr.add(new HomeInfoFragment()); fragmentArr.add(new HomeFindFragment()); fragmentArr.add(new HomeMineFragment()); navigationView.getMenu().add(0, 0, 1, "首页").setIcon(R.drawable.tab_1); navigationView.getMenu().add(0, 1, 1, "消息").setIcon(R.drawable.tab_2); navigationView.getMenu().add(0, 2, 1, "园地").setIcon(R.drawable.tab_3); navigationView.getMenu().add(0, 3, 1, "我的").setIcon(R.drawable.tab_4); /** * 否禁止用户滑动页面 * */ mainViewPager.setUserInputEnabled(false); /** * 设置ViewPager2的滑动监听事件 * isUserInputEnabled = true 时 * */ mainViewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { super.onPageScrolled(position, positionOffset, positionOffsetPixels); } @Override public void onPageSelected(int position) { super.onPageSelected(position); //设置导航栏选中位置 navigationView.getMenu().getItem(position).setChecked(true); } @Override public void onPageScrollStateChanged(int state) { super.onPageScrollStateChanged(state); } }); mainViewPager.setAdapter(new HomePagerAdapter(this, fragmentArr)); navigationView.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED); /** * 设置导航栏菜单项Item选中监听 * */ navigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull @NotNull MenuItem item) { mainViewPager.setCurrentItem(item.getItemId()); return true; } }); } }
4.创建对应的Fragment页面(示例中需创建四个,我贴出来一个代码,其他的一样)
package com.example.tasklayoutdemo.tab; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import com.example.tasklayoutdemo.R; import org.jetbrains.annotations.NotNull; public class HomeInfoFragment extends Fragment { private TextView tv_content; private View view; @Nullable @org.jetbrains.annotations.Nullable @Override public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.shouye_fragment, container, false); return view; } @Override public void onActivityCreated(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); tv_content = view.findViewById(R.id.tv_content); tv_content.setText("我是Fragment,消息...."); } }
5.创建 HomePagerAdapter
package com.example.tasklayoutdemo.tab; import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentActivity; import androidx.viewpager2.adapter.FragmentStateAdapter; import org.jetbrains.annotations.NotNull; import java.util.List; public class HomePagerAdapter extends FragmentStateAdapter { private List<Fragment> fragments; public HomePagerAdapter(@NonNull @NotNull FragmentActivity fragmentActivity, List<Fragment> fragments) { super(fragmentActivity); this.fragments = fragments; } @NonNull @NotNull @Override public Fragment createFragment(int position) { return fragments.get(position); } @Override public int getItemCount() { return fragments.size(); } }
6.添加导航栏图标
完成,运行即可
7.进阶
其中app:itemTextColor=”@drawable/main_bottom”为设置按钮选中与未选中时字体的颜色选择
app:itemIconTint=”@drawable/main_bottom”为设置按钮选中与未选中时图标的颜色选择
main_bottom.xml文件为:
<?xml version=”1.0″ encoding=”utf-8″?>
<selector xmlns:android=”http://schemas.android.com/apk/res/android”>
<item android:color=”@android:color/holo_red_light” android:state_checked=”true” />
<item android:color=”@color/black” android:state_checked=”false” />
</selector>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/119126.html