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的升级.切换,应 ...
随机推荐
- HTTP基本协议(查看网页代码)
此示例已实现查看网页的代码来理解HTTP基本协议: (返回的是百度首页的网页代码) import java.io.BufferedReader; import java.io.IOException; ...
- debian安装vld来查看Opcode,PHP调优。
一: 我的环境: Debian 7 (wheezy) x64 PHP 5.4.4-14 (apt-get 而来) Apache/2.2.22 (同上,非源码编译) 二 :安装vld. (# 代表是r ...
- 详细解说Android权限在安卓开发中
android.permission.ACCESS_CHECKIN_PROPERTIES //允许读写访问”properties”表在checkin数据库中,改值可以修改上传 android.perm ...
- BZOJ 1856: [Scoi2010]字符串( 组合数 )
求(0,0)->(n,m)且在直线y=x下方(可以在y=x上)的方案数...同 http://www.cnblogs.com/JSZX11556/p/4908648.html --------- ...
- Stbdroid之ShapeDrawable
Shape可以定义矩形.椭圆形.线条.圆形 <?xml version="1.0" encoding="utf-8"?> <shape xml ...
- 设计模式值六大原则——依赖倒置原则 (DIP)
依赖倒置原则(Dependence Inversion Principle,DIP)的原始定义: 高层模块不应该依赖底层模块,两者都应该依赖其抽象: 抽象不应该依赖细节: 细节应该依赖抽象. 依赖倒置 ...
- codeforces 620E. New Year Tree dfs序+线段树+bitset
题目链接 给一棵树, 每个节点有颜色, 两种操作, 一种是将一个节点的子树全都染色成c, 一种是查询一个节点的子树有多少个不同的颜色, c<=60. 每个节点一个bitset维护就可以. #in ...
- JS使用合并数组
var arr= [4,5,6]; var arr1 = [7,8,9]; var arr2=[1,2,3]; arr.concat(arr1,arr2); //或者使用Arry.prototype. ...
- QComboBox 添加图片(自带addItem函数就有这个功能,从没有注意过)
方法: 使用 QComboxBox::addItem(QIcon, QString); 示例: 点击(此处)折叠或打开 QComboBox *combo_status = new QComboB ...
- hdu4597 Play Game
Play Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total Sub ...