1、Xml中底部导航栏由一个RadioGroup组成,其上是VIEwPager。
<?xml version="1.0" enCoding="utf-8"?><relativeLayout 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:orIEntation="horizontal" androID:background="#FFFFFF" tools:context=".MainActivity"> <linearLayout androID:ID="@+ID/main_toplinear" androID:layout_wIDth="match_parent" androID:layout_height="70dp" androID:background="@drawable/riple_btn_nocorner" androID:gravity="center"> <ImageVIEw androID:layout_wIDth="60dp" androID:layout_height="60dp" androID:layout_margin="5dp" androID:src="@drawable/icon"></ImageVIEw> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="滴滴嗒小账本" androID:textcolor="#9E6E1E" androID:textSize="25dp"></TextVIEw> </linearLayout> <VIEw androID:layout_wIDth="match_parent" androID:layout_height="0.5dp" androID:layout_below="@+ID/main_toplinear" androID:background="#232"></VIEw> <VIEw androID:layout_wIDth="match_parent" androID:layout_height="0.5dp" androID:layout_above="@+ID/main_radio_group" androID:background="#232"></VIEw> <RadioGroup androID:ID="@+ID/main_radio_group" androID:layout_wIDth="match_parent" androID:layout_height="70dp" androID:layout_alignParentBottom="true" androID:background="#EDDEF7" androID:gravity="center" androID:orIEntation="horizontal"> <Radiobutton androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" androID:layout_weight="1" androID:button="@null" androID:text="记录" androID:gravity="center" androID:textSize="25dp" androID:textcolor="#9E6E1E" androID:background="@drawable/radio" androID:ID="@+ID/main_rd_cord" ></Radiobutton> <Radiobutton androID:ID="@+ID/main_rd_inout" androID:layout_wIDth="5dp" androID:layout_height="match_parent" androID:layout_weight="1" androID:gravity="center" androID:textSize="25dp" androID:textcolor="#9E6E1E" androID:text="收支" androID:background="@drawable/radio" androID:button="@null"></Radiobutton> <Radiobutton androID:ID="@+ID/main_rd_my" androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" androID:layout_weight="1" androID:button="@null" androID:gravity="center" androID:textSize="25dp" androID:textcolor="#9E6E1E" androID:background="@drawable/radio" androID:text="我的" ></Radiobutton> </RadioGroup> <androIDx.vIEwpager.Widget.VIEwPager androID:ID="@+ID/main_vIEwpager" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:layout_below="@+ID/main_toplinear" androID:layout_above="@ID/main_radio_group"></androIDx.vIEwpager.Widget.VIEwPager></relativeLayout>
2、编写几个页面的Fragment。
3、编写FragmentPagerAdapter,重写getItem和getCount方法。
package com.example.dIDIDa_corder;import androIDx.annotation.NonNull;import androIDx.fragment.app.Fragment;import androIDx.fragment.app.FragmentManager;import androIDx.fragment.app.FragmentPagerAdapter;public class MyFragPageAdapter extends FragmentPagerAdapter { private int PAGE_NUM = 3; private final int PAGE_CORD = 0; private final int PAGE_INOUT = 1; private final int PAGE_MY = 2; private CordFragment cordFragment = null; private MyItemFragment myItemFragment = null; private InOutFragment inOutFragment = null; public MyFragPageAdapter(@NonNull FragmentManager fm) { super(fm); cordFragment = new CordFragment(); myItemFragment = new MyItemFragment(); inOutFragment = new InOutFragment(); } @NonNull @OverrIDe public Fragment getItem(int position) { switch (position) { case PAGE_CORD: { return cordFragment; } case PAGE_INOUT: { return inOutFragment; } case PAGE_MY: { return myItemFragment; } default: return null; } } @OverrIDe public int getCount() { return PAGE_NUM; }}
4、在MainActivity中实现VIEwPager.OnPagechangelistener和RadioGroup.OnCheckedchangelistener接口。
package com.example.dIDIDa_corder;import androIDx.appcompat.app.AppCompatActivity;import androIDx.vIEwpager.Widget.VIEwPager;import androID.os.Bundle;import androID.util.Log;import androID.vIEw.VIEw;import androID.Widget.Adapter;import androID.Widget.Radiobutton;import androID.Widget.RadioGroup;import androID.Widget.TextVIEw;public class MainActivity extends AppCompatActivity implements VIEwPager.OnPagechangelistener, RadioGroup.OnCheckedchangelistener{ private MyFragPageAdapter myFragPageAdapter; private VIEwPager vIEwPager; private TextVIEw textVIEw; private RadioGroup radioGroup; private Radiobutton radiobutton_crod; private Radiobutton radiobutton_inout; private Radiobutton radiobutton_myitem; private int PAGE_NUM = 3; private final int PAGE_CORD = 0; private final int PAGE_INOUT = 1; private final int PAGE_MY = 2; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); myFragPageAdapter=new MyFragPageAdapter(getSupportFragmentManager()); bindVIEws(); radiobutton_crod.setChecked(true); } private voID bindVIEws(){ radiobutton_crod=findVIEwByID(R.ID.main_rd_cord); radiobutton_inout=findVIEwByID(R.ID.main_rd_inout); radiobutton_myitem=findVIEwByID(R.ID.main_rd_my); radioGroup=findVIEwByID(R.ID.main_radio_group); radioGroup.setonCheckedchangelistener(this); vIEwPager= findVIEwByID(R.ID.main_vIEwpager); vIEwPager.setAdapter(myFragPageAdapter); vIEwPager.addOnPagechangelistener(this); vIEwPager.setCurrentItem(0); } @OverrIDe public voID onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @OverrIDe public voID onPageSelected(int position) { } @OverrIDe public voID onPageScrollStateChanged(int state) { if (state==2){ switch (vIEwPager.getCurrentItem()){ case PAGE_CORD: radiobutton_crod.setChecked(true); break; case PAGE_INOUT: radiobutton_inout.setChecked(true); break; case PAGE_MY: radiobutton_myitem.setChecked(true); break; } } } @OverrIDe public voID onCheckedChanged(RadioGroup group, int checkedID) { switch (checkedID){ case R.ID.main_rd_cord:{ vIEwPager.setCurrentItem(PAGE_CORD); break; } case R.ID.main_rd_inout:{ vIEwPager.setCurrentItem(PAGE_INOUT); break; } case R.ID.main_rd_my:{ vIEwPager.setCurrentItem(PAGE_MY); break; } } }}
总结
以上是内存溢出为你收集整理的Android_ViewPager+Fragment实现页面滑动和底部导航栏全部内容,希望文章能够帮你解决Android_ViewPager+Fragment实现页面滑动和底部导航栏所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)