热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Android_ViewPager+Fragment实现页面滑动和底部导航栏

1、Xml中底部导航栏由一个RadioGroup组成,其上是ViewPager。   

1、Xml中底部导航栏由一个RadioGroup组成,其上是ViewPager。

 

 

 


xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:orientation="horizontal"
android:background="#FFFFFF"
tools:cOntext=".MainActivity">
android:id="@+id/main_toplinear"
android:layout_
android:layout_
android:background="@drawable/riple_btn_nocorner"
android:gravity="center">
android:layout_
android:layout_
android:layout_margin="5dp"
android:src="@drawable/icon">
android:layout_
android:layout_
android:text="滴滴嗒小账本"
android:textColor="#9E6E1E"
android:textSize="25dp">

android:layout_
android:layout_
android:layout_below="@+id/main_toplinear"
android:background="#232">
android:layout_
android:layout_
android:layout_above="@+id/main_radio_group"
android:background="#232">
android:id="@+id/main_radio_group"
android:layout_
android:layout_
android:layout_alignParentBottom="true"
android:background="#EDDEF7"
android:gravity="center"
android:orientation="horizontal">
android:layout_
android:layout_
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"
>
android:id="@+id/main_rd_inout"
android:layout_
android:layout_
android:layout_weight="1"
android:gravity="center"
android:textSize="25dp"
android:textColor="#9E6E1E"
android:text="收支"
android:background="@drawable/radio"
android:button="@null">
android:id="@+id/main_rd_my"
android:layout_
android:layout_
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:textSize="25dp"
android:textColor="#9E6E1E"
android:background="@drawable/radio"
android:text="我的"
>

android:id="@+id/main_viewpager"
android:layout_
android:layout_
android:layout_below="@+id/main_toplinear"
android:layout_above="@id/main_radio_group">

  

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;
}
}
}
}

  



推荐阅读
author-avatar
东东84321
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有