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

相关文章

Java实现字节字符转bcd编码

《Java实现字节字符转bcd编码》BCD是一种将十进制数字编码为二进制的表示方式,常用于数字显示和存储,本文将介绍如何在Java中实现字节字符转BCD码的过程,需要的小伙伴可以了解下... 目录前言BCD码是什么Java实现字节转bcd编码方法补充总结前言BCD码(Binary-Coded Decima

SpringBoot全局域名替换的实现

《SpringBoot全局域名替换的实现》本文主要介绍了SpringBoot全局域名替换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录 项目结构⚙️ 配置文件application.yml️ 配置类AppProperties.Ja

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

C#实现一键批量合并PDF文档

《C#实现一键批量合并PDF文档》这篇文章主要为大家详细介绍了如何使用C#实现一键批量合并PDF文档功能,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言效果展示功能实现1、添加文件2、文件分组(书签)3、定义页码范围4、自定义显示5、定义页面尺寸6、PDF批量合并7、其他方法

SpringBoot实现不同接口指定上传文件大小的具体步骤

《SpringBoot实现不同接口指定上传文件大小的具体步骤》:本文主要介绍在SpringBoot中通过自定义注解、AOP拦截和配置文件实现不同接口上传文件大小限制的方法,强调需设置全局阈值远大于... 目录一  springboot实现不同接口指定文件大小1.1 思路说明1.2 工程启动说明二 具体实施2

Python实现精确小数计算的完全指南

《Python实现精确小数计算的完全指南》在金融计算、科学实验和工程领域,浮点数精度问题一直是开发者面临的重大挑战,本文将深入解析Python精确小数计算技术体系,感兴趣的小伙伴可以了解一下... 目录引言:小数精度问题的核心挑战一、浮点数精度问题分析1.1 浮点数精度陷阱1.2 浮点数误差来源二、基础解决

Java实现在Word文档中添加文本水印和图片水印的操作指南

《Java实现在Word文档中添加文本水印和图片水印的操作指南》在当今数字时代,文档的自动化处理与安全防护变得尤为重要,无论是为了保护版权、推广品牌,还是为了在文档中加入特定的标识,为Word文档添加... 目录引言Spire.Doc for Java:高效Word文档处理的利器代码实战:使用Java为Wo

Java实现远程执行Shell指令

《Java实现远程执行Shell指令》文章介绍使用JSch在SpringBoot项目中实现远程Shell操作,涵盖环境配置、依赖引入及工具类编写,详解分号和双与号执行多指令的区别... 目录软硬件环境说明编写执行Shell指令的工具类总结jsch(Java Secure Channel)是SSH2的一个纯J