阶段二冲刺四
昨天学习了如何制作一个简单的底部导航
跟这视频学习,并没有遇到一些问题,
今天主要是学习了运用viewpager+fragment+bottom技术制作一个完整的底部导航
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:padding="10dp" tools:context=".ui.activity.OrderActivity" android:orientation="vertical"> <LinearLayout android:id="@+id/order_linea1" android:layout_width="match_parent" android:layout_height="0dp" android:orientation="vertical" android:layout_weight="1" > <androidx.viewpager.widget.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> <com.google.android.material.bottomnavigation.BottomNavigationView android:background="#FFFAFA" app:itemIconTint="@color/btnnav_presscolor" app:itemTextColor="@color/btnnav_presscolor" android:id="@+id/btn_menu" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:menu="@menu/btnnav_menu" /> </LinearLayout>
今天制作完成之后,我对viewpage和fragment的理解进一步加深
public class OrderActivity extends AppCompatActivity { private ViewPager mviewPager; private BottomNavigationView mbottomNavigationView; private MyfragmentAdapter myfragmentAdapter; private List<Fragment> fragmentList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_order); mviewPager = findViewById(R.id.vp); mbottomNavigationView = findViewById(R.id.btn_menu); initfragment(); myfragmentAdapter = new MyfragmentAdapter(getSupportFragmentManager(), fragmentList); mviewPager.setAdapter(myfragmentAdapter); mviewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { onPagerSelected(position); } @Override public void onPageScrollStateChanged(int state) { } }); mbottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.menu_game: mviewPager.setCurrentItem(0); break; case R.id.menu_talk: mviewPager.setCurrentItem(1); break; case R.id.menu_mine: mviewPager.setCurrentItem(2); break; default: mviewPager.setCurrentItem(0); break; } return true; } }); } private void onPagerSelected(int position) { switch (position) { case 0: mbottomNavigationView.setSelectedItemId(R.id.menu_game); break; case 1: mbottomNavigationView.setSelectedItemId(R.id.menu_talk); break; case 2: mbottomNavigationView.setSelectedItemId(R.id.menu_mine); break; default: mbottomNavigationView.setSelectedItemId(R.id.menu_game); break; } } private void initfragment() { fragmentList = new ArrayList<>(); GameFragment gameFragment = new GameFragment(); fragmentList.add(gameFragment); TalkFragment talkFragment = new TalkFragment(); fragmentList.add(talkFragment); MIneFragment mIneFragment = new MIneFragment(); fragmentList.add(mIneFragment); } }
并且实现了点击翻页和滑动翻页