ViewPager+fragment实现切换页面(一)

2024-09-08 08:48

本文主要是介绍ViewPager+fragment实现切换页面(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

如今的很多应用中都是下面有一排按钮,点击可以切换页面,滑动也可以切换页面。下面就来简单的实现这个功能。

思路
  • 首先肯定是会用到viewpager这个控件,为了能够向下兼容,最好用v4包下的viewpager,Activity要继承FragmentActivity
  • 其次用一个集合来存储所有的fragment页面
  • 在设置viewpager的适配器时,把存储fragment页面的list集合传入adapter中,adapter继承的是FragmentPagerAdapter
  • 给viewpager设置监听事件,这样滑动换页的时候做相应的逻辑处理
  • 点击按钮想要得到对应的fragment页面,只需要设置viewpager.setCurrentItem(item);

  • 遗忘的一个概念 object[] obj = new object[]任何对象都可以创建数组

效果图

这里写图片描述

这里写图片描述

下面上代码
主界面布局代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:background="#ededed"><RelativeLayout android:layout_width="match_parent"android:layout_height="50dp"android:background="#ffffff"><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp"android:text="个人信息"android:textColor="#ed6d48"android:layout_marginTop="10dp"android:layout_marginBottom="10dp"android:layout_centerInParent="true"/></RelativeLayout><LinearLayout
        android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="10dp"android:background="@drawable/linearlayout01"android:orientation="horizontal"android:weightSum="5" ><LinearLayout
            android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="15dp"android:layout_marginLeft="20dp"android:layout_marginTop="15dp"android:layout_weight="2"android:gravity="center"android:orientation="vertical" ><TextView
                android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="账户总额"android:textSize="12sp" /><LinearLayout
                android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:orientation="horizontal" ><TextView
                    android:id="@+id/tv_shouldmoney"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="0.00"android:textColor="#ed6d48"android:textSize="20sp" /><TextView
                    android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="元"android:textColor="#ed6d48"android:textSize="16sp" /></LinearLayout></LinearLayout><LinearLayout
            android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="15dp"android:layout_marginLeft="30dp"android:layout_marginTop="15dp"android:layout_weight="3"android:gravity="center_vertical"android:orientation="vertical" ><LinearLayout
                android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal" ><TextView
                    android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="共消费:" /><TextView
                    android:id="@+id/tv_totalMoneyNum"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="0"android:textColor="#ed6d48" /><TextView
                    android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="笔" /></LinearLayout><LinearLayout
                android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal" ><TextView
                    android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="合计金额:" /><TextView
                    android:id="@+id/tv_totalMoney"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="0.00"android:textColor="#ed6d48" /><TextView
                    android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="元" /></LinearLayout><LinearLayout
                android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal" ><TextView
                    android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="信用额度:" /><TextView
                    android:id="@+id/tv_credit"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="5000.00"android:textColor="#ed6d48" /><TextView
                    android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="元" /></LinearLayout></LinearLayout></LinearLayout><include layout="@layout/tab"/><android.support.v4.view.ViewPager
        android:id="@+id/viewPager"android:layout_width="match_parent"android:layout_height="match_parent" /></LinearLayout>
tab布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="44dp"android:orientation="horizontal" android:background="#ededed"><Button android:id="@+id/btn_one"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:text="第一页"android:background="@drawable/linearlayout01"android:layout_marginLeft="5dp"android:layout_marginRight="5dp"/><Button android:id="@+id/btn_two"android:layout_width="0dp"android:layout_marginLeft="5dp"android:layout_marginRight="5dp"android:layout_weight="1"android:layout_height="wrap_content"android:background="@drawable/linearlayout01"android:text="第二页"/><Button android:id="@+id/btn_three"android:layout_marginLeft="5dp"android:layout_marginRight="5dp"android:background="@drawable/linearlayout01"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:text="第三页"/></LinearLayout>
一些资源的xml代码,比如linearLayout01.xml,linearLayout01s.xml,color.xml
linearLayout01.xml代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" ><solid android:color="#ffffff" /> <stroke android:width="0.1dip" android:color="#ed6d48" />  <corners android:radius="5dp"/></shape>
linearLayout01s.xml代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" ><solid android:color="#ffffff" /> <stroke android:width="1dip" android:color="#ed6d48" />  <corners android:radius="5dp"/></shape>
color.xml代码
<?xml version="1.0" encoding="utf-8"?>
<resources><color name="textcolor">#ed6d48</color><color name="loan_title">#ed6d48</color><color name="main_color_white">#ffffff</color><color name="background">#ededed</color><color name="loan_butBackground">#ed6d48</color><color name="black">#000000</color></resources>
MainActivity代码
 package com.example.myviewpager;import java.util.ArrayList;
import java.util.List;import com.example.adapter.MyAdapter;
import com.example.fragment.Fragment1;
import com.example.fragment.Fragment2;
import com.example.fragment.Fragment3;import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;public class MainActivity extends FragmentActivity implements OnClickListener{private ViewPager mViewPager;private Button btnOne;private Button btnTwo;private Button btnThree;private Button[] btns = new Button[3];//存放fragment的集合private List<Fragment> fragmentList = new ArrayList<Fragment>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initData();initView();}private void initData() {//把fragment添加进集合fragmentList.add(new Fragment1());fragmentList.add(new Fragment2());fragmentList.add(new Fragment3());}private void initView() {btnOne = (Button) this.findViewById(R.id.btn_one);btnTwo = (Button) this.findViewById(R.id.btn_two);btnThree = (Button) this.findViewById(R.id.btn_three);//默认第一个按钮为选中状态btnOne.setBackgroundResource(R.drawable.linearlayout01s);btnOne.setTextColor(getResources().getColor(R.color.textcolor));//创建一个数组来存btn,object[] obj = new object[]这个概念才有btns[0] = btnOne;btns[1] = btnTwo;btns[2] = btnThree;btnOne.setOnClickListener(this);btnTwo.setOnClickListener(this);btnThree.setOnClickListener(this);mViewPager = (ViewPager) this.findViewById(R.id.viewPager);//设置viewpager的数据适配器mViewPager.setAdapter(new MyAdapter(getSupportFragmentManager(), fragmentList));//监听viewpager的滑动事件,来控制按钮的变换mViewPager.setOnPageChangeListener(new OnPageChangeListener() {@Overridepublic void onPageSelected(int position) {//把3个按钮全部设置成原始状态resetButton();btns[position].setBackgroundResource(R.drawable.linearlayout01s);btns[position].setTextColor(getResources().getColor(R.color.textcolor));}@Overridepublic void onPageScrolled(int arg0, float arg1, int arg2) {}@Overridepublic void onPageScrollStateChanged(int arg0) {}});}//重置button的背景和字体颜色protected void resetButton() {for(int i=0;i<btns.length;i++){btns[i].setBackgroundResource(R.drawable.linearlayout01);btns[i].setTextColor(getResources().getColor(R.color.black));}}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_one:resetButton();mViewPager.setCurrentItem(0);btns[0].setBackgroundResource(R.drawable.linearlayout01s);btns[0].setTextColor(getResources().getColor(R.color.textcolor));break;case R.id.btn_two:resetButton();mViewPager.setCurrentItem(1);btns[1].setBackgroundResource(R.drawable.linearlayout01s);btns[1].setTextColor(getResources().getColor(R.color.textcolor));break;case R.id.btn_three:resetButton();mViewPager.setCurrentItem(2);btns[2].setBackgroundResource(R.drawable.linearlayout01s);btns[2].setTextColor(getResources().getColor(R.color.textcolor));break;default:break;}}}
MyAdapter中的代码
package com.example.adapter;import java.util.List;import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;public class MyAdapter extends FragmentPagerAdapter {//接受fragment页的集合数据private List<Fragment> list;public MyAdapter(FragmentManager fm,List<Fragment> list) {super(fm);this.list = list;}@Overridepublic Fragment getItem(int position) {return list.get(position);}@Overridepublic int getCount() {return list.size();}}
其中的一个Fragment页面的代码,其余的都一样
package com.example.fragment;import com.example.myviewpager.R;import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;public class Fragment1 extends Fragment{@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragment01, null);return view;}}

下面附上完整demo的链接,里面有很详细的注释。
完整代码下载地址: http://download.csdn.net/detail/u013467495/8494827

这篇关于ViewPager+fragment实现切换页面(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1147656

相关文章

openCV中KNN算法的实现

《openCV中KNN算法的实现》KNN算法是一种简单且常用的分类算法,本文主要介绍了openCV中KNN算法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录KNN算法流程使用OpenCV实现KNNOpenCV 是一个开源的跨平台计算机视觉库,它提供了各

OpenCV图像形态学的实现

《OpenCV图像形态学的实现》本文主要介绍了OpenCV图像形态学的实现,包括腐蚀、膨胀、开运算、闭运算、梯度运算、顶帽运算和黑帽运算,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起... 目录一、图像形态学简介二、腐蚀(Erosion)1. 原理2. OpenCV 实现三、膨胀China编程(

通过Spring层面进行事务回滚的实现

《通过Spring层面进行事务回滚的实现》本文主要介绍了通过Spring层面进行事务回滚的实现,包括声明式事务和编程式事务,具有一定的参考价值,感兴趣的可以了解一下... 目录声明式事务回滚:1. 基础注解配置2. 指定回滚异常类型3. ​不回滚特殊场景编程式事务回滚:1. ​使用 TransactionT

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

使用Python实现全能手机虚拟键盘的示例代码

《使用Python实现全能手机虚拟键盘的示例代码》在数字化办公时代,你是否遇到过这样的场景:会议室投影电脑突然键盘失灵、躺在沙发上想远程控制书房电脑、或者需要给长辈远程协助操作?今天我要分享的Pyth... 目录一、项目概述:不止于键盘的远程控制方案1.1 创新价值1.2 技术栈全景二、需求实现步骤一、需求

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

SpringBatch数据写入实现

《SpringBatch数据写入实现》SpringBatch通过ItemWriter接口及其丰富的实现,提供了强大的数据写入能力,本文主要介绍了SpringBatch数据写入实现,具有一定的参考价值,... 目录python引言一、ItemWriter核心概念二、数据库写入实现三、文件写入实现四、多目标写入

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

SpringSecurity JWT基于令牌的无状态认证实现

《SpringSecurityJWT基于令牌的无状态认证实现》SpringSecurity中实现基于JWT的无状态认证是一种常见的做法,本文就来介绍一下SpringSecurityJWT基于令牌的无... 目录引言一、JWT基本原理与结构二、Spring Security JWT依赖配置三、JWT令牌生成与

Pytest多环境切换的常见方法介绍

《Pytest多环境切换的常见方法介绍》Pytest作为自动化测试的主力框架,如何实现本地、测试、预发、生产环境的灵活切换,本文总结了通过pytest框架实现自由环境切换的几种方法,大家可以根据需要进... 目录1.pytest-base-url2.hooks函数3.yml和fixture结论你是否也遇到过