Android项目ViewPager+Fragment的基本使用[通俗易懂]

Android项目ViewPager+Fragment的基本使用[通俗易懂]Android项目ViewPager+Fragment的简单使用

大家好,又见面了,我是你们的朋友全栈君。

利用ViewPager+Fragment简单实现页面的切换

Android项目ViewPager+Fragment的基本使用[通俗易懂]Android项目ViewPager+Fragment的基本使用[通俗易懂]

项目的大概组成:

Android项目ViewPager+Fragment的基本使用[通俗易懂]Android项目ViewPager+Fragment的基本使用[通俗易懂]

以下是代码的实现,首先在activity_main.xml新建菜单栏和ViewPager控件:

<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"
    tools:context="com.itman.viewpagerdemo.MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tv_item_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:text="菜单一" />

        <TextView
            android:id="@+id/tv_item_two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:text="菜单二" />

        <TextView
            android:id="@+id/tv_item_three"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:text="菜单三" />
    </LinearLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/myViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>

接下来就新建三个Fragment页面做好准备,Fragment的布局文件:

<RelativeLayout 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" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="我是菜单一"
        android:textSize="30sp" />

</RelativeLayout>

Fragment的Java文件:

package com.itman.viewpagerdemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class OneFragment extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,  Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, null);
return view;
}

}

三个fragment页面都一样的,就不全部贴出来了,接下来就准备添加Fragment的适配器TabFragmentPagerAdapter:

package com.itman.viewpagerdemo;

import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class TabFragmentPagerAdapter extends FragmentPagerAdapter {
private FragmentManager mfragmentManager;
private List<Fragment> mlist;

public TabFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {
super(fm); 
this.mlist = list;
}

@Override
public Fragment getItem(int arg0) {
return mlist.get(arg0);//显示第几个页面
}

@Override
public int getCount() {
return mlist.size();//有几个页面
}
}

准备工作完成,接下来是MainActivit.Java的代码实现:

package com.itman.viewpagerdemo;

import java.util.ArrayList;
import java.util.List;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity implements OnClickListener {

private TextView tv_item_one;
private TextView tv_item_two;
private TextView tv_item_three;
private ViewPager myViewPager;
private List<Fragment> list;
private TabFragmentPagerAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
InitView();

// 设置菜单栏的点击事件
tv_item_one.setOnClickListener(this);
tv_item_two.setOnClickListener(this);
tv_item_three.setOnClickListener(this);
myViewPager.setOnPageChangeListener(new MyPagerChangeListener());

//把Fragment添加到List集合里面
list = new ArrayList<>();
list.add(new OneFragment());
list.add(new TwoFragment());
list.add(new ThreeFragment());
adapter = new TabFragmentPagerAdapter(getSupportFragmentManager(), list);
myViewPager.setAdapter(adapter);
myViewPager.setCurrentItem(0);  //初始化显示第一个页面
tv_item_one.setBackgroundColor(Color.RED);//被选中就为红色
}

/**
* 初始化控件
*/
private void InitView() {
tv_item_one = (TextView) findViewById(R.id.tv_item_one);
tv_item_two = (TextView) findViewById(R.id.tv_item_two);
tv_item_three = (TextView) findViewById(R.id.tv_item_three);
myViewPager = (ViewPager) findViewById(R.id.myViewPager);
}

/**
* 点击事件
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_item_one:
myViewPager.setCurrentItem(0);
tv_item_one.setBackgroundColor(Color.RED);
tv_item_two.setBackgroundColor(Color.WHITE);
tv_item_three.setBackgroundColor(Color.WHITE);
break;
case R.id.tv_item_two:
myViewPager.setCurrentItem(1);
tv_item_one.setBackgroundColor(Color.WHITE);
tv_item_two.setBackgroundColor(Color.RED);
tv_item_three.setBackgroundColor(Color.WHITE);
break;
case R.id.tv_item_three:
myViewPager.setCurrentItem(2);
tv_item_one.setBackgroundColor(Color.WHITE);
tv_item_two.setBackgroundColor(Color.WHITE);
tv_item_three.setBackgroundColor(Color.RED);
break;
}
}

/**
* 设置一个ViewPager的侦听事件,当左右滑动ViewPager时菜单栏被选中状态跟着改变
*
*/
public class MyPagerChangeListener implements OnPageChangeListener {

@Override
public void onPageScrollStateChanged(int arg0) {
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}

@Override
public void onPageSelected(int arg0) {
switch (arg0) {
case 0:
tv_item_one.setBackgroundColor(Color.RED);
tv_item_two.setBackgroundColor(Color.WHITE);
tv_item_three.setBackgroundColor(Color.WHITE);
break;
case 1:
tv_item_one.setBackgroundColor(Color.WHITE);
tv_item_two.setBackgroundColor(Color.RED);
tv_item_three.setBackgroundColor(Color.WHITE);
break;
case 2:
tv_item_one.setBackgroundColor(Color.WHITE);
tv_item_two.setBackgroundColor(Color.WHITE);
tv_item_three.setBackgroundColor(Color.RED);
break;
}
}
}

}

代码的注释很详细,也不是什么很难实现功能,有了基本实现的样例,大家就可以随意改动,变成自己喜欢的样式了。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/163228.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • HDLBits答案(12)_Verilog移位寄存器「建议收藏」

    HDLBits答案(12)_Verilog移位寄存器「建议收藏」Verilog移位寄存器HDLBits链接前言今天更新一节寄存器相关内容,其中涉及CRC校验的内容是用线性反馈移位寄存器搭建而成的。题库题目描述1:构建一个4bit的移位寄存器(右移),含异步复位、同步加载和使能areset:让寄存器复位为0load:加载4bit数据到移位寄存器中,不移位ena:使能右移q:移位寄存器中的内容Solution1:moduletop_module(inputclk,inputareset,//asyncacti

    2022年7月16日
  • 字符串正则匹配leetcode_正则在线检验

    字符串正则匹配leetcode_正则在线检验原题链接给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 ‘.’ 和 ‘*’ 的正则表达式匹配。‘.’ 匹配任意单个字符‘*’ 匹配零个或多个前面的那一个元素所谓匹配,是要涵盖 整个 字符串 s的,而不是部分字符串。示例 1:输入:s = “aa” p = “a”输出:false解释:”a” 无法匹配 “aa” 整个字符串。示例 2:输入:s = “aa” p = “a*”输出:true解释:因为 ‘*’ 代表可以匹配零个或多个前面的那一个元素, 在这里前面的元素就是

    2022年8月8日
  • IDEA超级实用的几个快捷键[通俗易懂]

    最近开始使用IDEA,突然发现的比较的实用的几个快捷键 这些快捷键用的好的话真的可以提升很多效率 还有一些比较简单的快捷键,相信大家都会 Ctrl+X:剪切 Ctrl+C:复制 Ctrl+V:粘贴 C

    2022年3月13日
  • 全面详解c语言使用cJSON解析JSON字符[通俗易懂]

    全面详解c语言使用cJSON解析JSON字符[通俗易懂]为什么选择cJSON来解析JSON字符串?因为简洁又简单,而且效率又快,cJSON工程文件也非常简单,仅一个.c文件和一个.h文件!如果要自己写的话就是重造轮子了,况且效率上也不一定会比cJSON更好!且文件体积大小不到30k,源代码思路也非常清晰,也非常适合研究。点我下载cJSON当我们下载好cJSON只需要把.c和.h文件包含文件拷贝到我们工程目录下,并将头文件和实现文件…

    2022年6月30日
  • pycharm激活码注册码密钥2021破解方法

    pycharm激活码注册码密钥2021破解方法,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月14日
  • substring() 方法用于提取字符串中介于两个指定下标之间的字符。

    substring() 方法用于提取字符串中介于两个指定下标之间的字符。

    2021年10月30日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号