ViewPager控件的Demo
1、主视图
<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"
> <include layout="@layout/head"/>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="@+id/vp"
/> </LinearLayout>
2、主视图头部
<?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="wrap_content"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:orientation="horizontal" > <TextView
android:id="@+id/tvChat"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="聊天" /> <TextView
android:id="@+id/tvFriend"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="朋友" /> <TextView
android:id="@+id/tvContact"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="通讯" />
</LinearLayout>
<ImageView
android:id="@+id/iv_scroll"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:background="@drawable/ivbg"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#000000"
/>
</LinearLayout>
3、fragment的视图
<?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="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我在聊天"
/> </LinearLayout>
<?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="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我的朋友"
/> </LinearLayout>
<?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="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我的通讯"
/> </LinearLayout>
4、MainActivity
package com.zyhui.viewpagerdemo; import java.util.ArrayList;
import java.util.List; import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout; public class MainActivity extends FragmentActivity {
private List<Fragment> fragList;
private ViewPager vp;
private ImageView iv_scroll;
private int screenWidth;
private LinearLayout.LayoutParams lp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); iv_scroll = (ImageView) findViewById(R.id.iv_scroll); //获取屏幕宽
screenWidth = getScreenWH()[0];
//设置iv_scroll的宽和高
lp = (LinearLayout.LayoutParams)iv_scroll.getLayoutParams();//为什么要这样呢?
lp.width = screenWidth/3; initFragment();
initViewPager(); setVpEvent();
} private void initViewPager() {
vp = (ViewPager) findViewById(R.id.vp);
vp.setAdapter(new MyAdapter(getSupportFragmentManager()));
vp.setCurrentItem(0);
} private void initFragment() {
fragList = new ArrayList<Fragment>();
fragList.add(new FragmentChat());
fragList.add(new FragmentFriend());
fragList.add(new FragmentContact());
} private class MyAdapter extends FragmentPagerAdapter{ public MyAdapter(FragmentManager fm) {
super(fm); } @Override
public Fragment getItem(int position) {
return fragList.get(position);
} @Override
public int getCount() {
return fragList.size();
} } private void setVpEvent() {
vp.setOnPageChangeListener(new OnPageChangeListener() {
private int currentIndex = 0;//这个初始值应当为0,而不要为-1,否则默认是显示不了iv_scroll的图片的,因为leftMargin的值会为负值
//选中页面时候调用的方法
//index表示当前选中的页
@Override
public void onPageSelected(int index) {
currentIndex = index;
} //页面滑动时候调用的方法
//position是向右滑动到下一个页面时position==index,向左滑动position-index=1
//offset:向右滑动到下一个页面时,它的值从0.00到0.99;向做滑动时,从0.99到0.00
//offsetPixels:向右滑动到下一个页面时,它从0到屏幕的宽度;向左滑动时是从屏幕宽度到0
@Override
public void onPageScrolled(int position, float offset, int offsetPixels) {
//经测试,感觉不用这个判断也可以,直接使用lp.leftMargin = (screenWidth/3)*currentIndex就行了,这样右移感觉不到卡
if(position == currentIndex){//向右滑动
lp.leftMargin = (int) ((screenWidth/3)*currentIndex + offset * (screenWidth/3));
//lp.leftMargin = (screenWidth/3)*currentIndex;
Log.i("zyh", currentIndex + "========>" + offset+"R");
//Log.i("zyh", "右移");//感觉没有左移的输出
iv_scroll.setLayoutParams(lp);
}else if(position - currentIndex == 1){//向左滑动
lp.leftMargin = (int) ((screenWidth/3) * currentIndex - (1 - offset) * (screenWidth/3));
//lp.leftMargin = (screenWidth/3)*currentIndex;
Log.i("zyh", currentIndex + "========>" + offset+"L");
//Log.i("zyh", "左移");//感觉没有左移的输出
iv_scroll.setLayoutParams(lp);
} } //滑动状态改变时调用的方法
//state有3个值:
//0表示什么多不干
//1表示正在滑动
//2表示
@Override
public void onPageScrollStateChanged(int state) { }
});
} /**
* @desc 获取屏幕的宽高
* @return
*/
private int[] getScreenWH() {
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
return new int[]{outMetrics.widthPixels,outMetrics.heightPixels};
} }
5、fragment的java代码
package com.zyhui.viewpagerdemo; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class FragmentChat extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { return inflater.inflate(R.layout.frag_chat, null);
}
}
其他的类似
ViewPager控件的Demo的更多相关文章
- 安卓开发_深入学习ViewPager控件
一.概述 ViewPager是android扩展包v4包(android.support.v4.view.ViewPager)中的类,这个类可以让用户左右切换当前的view. ViewPager特点: ...
- Android高手进阶教程(二十八)之---Android ViewPager控件的使用(基于ViewPager的横向相册)!!!
分类: Android高手进阶 Android基础教程 2012-09-14 18:10 29759人阅读 评论(35) 收藏 举报 android相册layoutobjectclassloade ...
- WPF常用控件应用demo
WPF常用控件应用demo 一.Demo 1.Demo截图如下: 2.demo实现过程 总体布局:因放大缩小窗体,控件很根据空间是否足够改变布局,故用WrapPanel布局. <ScrollVi ...
- Android实现图片轮显效果——自定义ViewPager控件
一.问题概述 使用ViewPager控件实现可横向翻页.水平切换图片等效果,但ViewPager需要手动滑动才能切换页面,图片轮显效果的效果本质上就是在ViewPager控件的基础上让它能自动的进行切 ...
- 模仿ViewPager控件
自定义控件是开发中经常使用的技术.系统中自带的ViewPager实现的功能有时候不能满足开发的需要,如ViewPager没有滑动图片时的动画切换效果.通过对 ViewPager的模仿和部分功能的加强, ...
- 【WPF】推荐一款拖拉缩放控件的DEMO
引言 在CodeProject上有个实现了控件拖拉缩放的DEMO,界面很漂亮,里面对Thumb和Adorner运用得很精彩.我觉得,使用WPF的开发者都可以去学习一下.下面放出链接. WPF Diag ...
- 【Android】带底部指示的自定义ViewPager控件
在项目中经常需要使用轮转广告的效果,在android-v4版本中提供的ViewPager是一个很好的工具,而一般我们使用Viewpager的时候,都会选择在底部有一排指示物指示当前显示的是哪一个pag ...
- Andoird实现类似iphone AssistiveTouch的控件的demo
类似Iphone Assistive Touch的控件的实现 网上也有些这方面的控件,不过貌似不怎么好用,或者是论坛需要积分下载,恰好自己在项目中有用到这种控件,就打算自己写一个,也成功实现了这种功能 ...
- 文档驱动 —— 表单组件(五):基于Ant Design Vue 的表单控件的demo,再也不需要写代码了。
源码 https://github.com/naturefwvue/nf-vue3-ant 特点 只需要更改meta,既可以切换表单 可以统一修改样式,统一升级,以最小的代价,应对UI的升级.切换,应 ...
随机推荐
- 【Leetcode】Triangle
给定一个由数字组成的三角形,从顶至底找出路径最小和. Given a triangle, find the minimum path sum from top to bottom. Each step ...
- jQuery的AJAX方法简介及与其他文件$符号冲突的解决办法
一.重要的jQuery AJAX方法简介 $.load(url) 从服务器载入数据 $.get(url,callback) 从服务器请求数据,并执行回调函数 $.post(url,data,callb ...
- VS2010 .net4.0 登录QQ 获取QQ空间日志 右键选中直接打开日志 免积分 源码下载
代码有一部分是原来写的 最近翻代码 看到了 就改了一下 CSDN上传源码 上传了几次都没 成功 郁闷 不知道怎么回事 上传不了 想要的留 邮箱 或加群77877965 下载地址在下面 演示地址 ...
- C语言之自守数
自守数 自守数是指一个数的平方的尾数等于该数自身的自然数,如25*25=625,76*76=5776.要求求出一定范围内的所有自守数. 题目分析: 刚拿到这个题目的时候认为解题关键在于,测试该数平方数 ...
- SQL——表结构或数据的复制
一.复制表结构及数据到新表 create table new_tb select * from old_tb 二.只复制表结构到新表 create table new_tb select * from ...
- BZOJ 1607: [Usaco2008 Dec]Patting Heads 轻拍牛头
1607: [Usaco2008 Dec]Patting Heads 轻拍牛头 Description 今天是贝茜的生日,为了庆祝自己的生日,贝茜邀你来玩一个游戏. 贝茜让N(1≤N≤10 ...
- pragma comment
pragma指令简介 在编写程序的时候,我们经常要用到#pragma指令来设定编译器的状态或者是指示编译器完成一些特定的动作. 下面介绍了一下该指令的一些常用参数,希望对大家有所帮助! 一. mess ...
- Swap file ".Podfile.swp" already exists!
解决Swap file ".ceshi.c.swp" already exists!问题 关于swp文件:使用vi,经常可以看到swp这个文件,那这个文件是怎么产生的呢,当你打开一 ...
- HtmlAgilityPack 抓取页面的乱码处理
HtmlAgilityPack 抓取页面的乱码处理 用来解析 HTML 确实方便.不过直接读取网页时会出现乱码. 实际上,它是能正确读到有关字符集的信息,怎么会在输出时,没有取到正确内容. 因此,读两 ...
- BitNami一键安装Redmine
1. 简单介绍 对于一个新手,假设严格依照官方文档来安装redmine,我想会"疯"掉的.有没有一种简便的方法.有滴,那就是BitNami. BitNami提供redmine的一键 ...