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的更多相关文章

  1. 安卓开发_深入学习ViewPager控件

    一.概述 ViewPager是android扩展包v4包(android.support.v4.view.ViewPager)中的类,这个类可以让用户左右切换当前的view. ViewPager特点: ...

  2. Android高手进阶教程(二十八)之---Android ViewPager控件的使用(基于ViewPager的横向相册)!!!

      分类: Android高手进阶 Android基础教程 2012-09-14 18:10 29759人阅读 评论(35) 收藏 举报 android相册layoutobjectclassloade ...

  3. WPF常用控件应用demo

    WPF常用控件应用demo 一.Demo 1.Demo截图如下: 2.demo实现过程 总体布局:因放大缩小窗体,控件很根据空间是否足够改变布局,故用WrapPanel布局. <ScrollVi ...

  4. Android实现图片轮显效果——自定义ViewPager控件

    一.问题概述 使用ViewPager控件实现可横向翻页.水平切换图片等效果,但ViewPager需要手动滑动才能切换页面,图片轮显效果的效果本质上就是在ViewPager控件的基础上让它能自动的进行切 ...

  5. 模仿ViewPager控件

    自定义控件是开发中经常使用的技术.系统中自带的ViewPager实现的功能有时候不能满足开发的需要,如ViewPager没有滑动图片时的动画切换效果.通过对 ViewPager的模仿和部分功能的加强, ...

  6. 【WPF】推荐一款拖拉缩放控件的DEMO

    引言 在CodeProject上有个实现了控件拖拉缩放的DEMO,界面很漂亮,里面对Thumb和Adorner运用得很精彩.我觉得,使用WPF的开发者都可以去学习一下.下面放出链接. WPF Diag ...

  7. 【Android】带底部指示的自定义ViewPager控件

    在项目中经常需要使用轮转广告的效果,在android-v4版本中提供的ViewPager是一个很好的工具,而一般我们使用Viewpager的时候,都会选择在底部有一排指示物指示当前显示的是哪一个pag ...

  8. Andoird实现类似iphone AssistiveTouch的控件的demo

    类似Iphone Assistive Touch的控件的实现 网上也有些这方面的控件,不过貌似不怎么好用,或者是论坛需要积分下载,恰好自己在项目中有用到这种控件,就打算自己写一个,也成功实现了这种功能 ...

  9. 文档驱动 —— 表单组件(五):基于Ant Design Vue 的表单控件的demo,再也不需要写代码了。

    源码 https://github.com/naturefwvue/nf-vue3-ant 特点 只需要更改meta,既可以切换表单 可以统一修改样式,统一升级,以最小的代价,应对UI的升级.切换,应 ...

随机推荐

  1. Convert Sorted List to Binary Search Tree ------C++ 递归创建平衡二叉查找树

    有序链表 0->1->2->3->4->5 转换为一个二叉排序树.我们在此创建一个平衡二叉排序树 1.先找链表到中间的节点 2.中间节点的val创建一个新的树节点Tree ...

  2. POJ1062 昂贵的聘礼 【DFS】

    昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37475   Accepted: 10816 Descripti ...

  3. EnumMap源代码阅读器

    EnumMap是一个用于存放键值为enum类型的map.全部的键值必须来自一个单一的enum类型.EnumMap内部用数组表示效率更高. EnumMap维持键值的自然顺序(即枚举类型常量声明的顺序), ...

  4. 工具篇-TraceView

    --- layout: post title: 工具篇-TraceView  description: 让我们远离卡顿和黑屏 2015-10-09 category: blog --- ## 让我们远 ...

  5. 【转】install intel wireless 3165 driver for ubuntu 14.04.3

    [转]install intel wireless 3165 driver for ubuntu 14.04.3 Ubuntu 14.04.3 with 3.19 kernel can’t drive ...

  6. linux串口编程(c)

    //linux c: 串口设置//串口操作无非以下几个://1 打开                       //2 设置串口属性//3 read write //struct termios能够 ...

  7. Two Sum-n方优化与C++map的使用

    LeetCode第一题,刚拿到题目时虽然明知道n方的遍历算法会超时,但还是不信邪的提交了一次,然而编程不存在运气,TLE不可避免.但是之后的思维方式比较直接,我并没有立刻想到O(n)的方法,想了一种先 ...

  8. Java中有关日期的一些常见运算与应用(Date,DateFormat,SimpleDateFormat)

    import java.text.DateFormat; import java.text.DateFormat; import java.text.SimpleDateFormat; import ...

  9. [转载]MySql常用命令总结

    原文:http://www.php100.com/html/webkaifa/database/Mysql/2009/0910/3288.html1:使用SHOW语句找出在服务器上当前存在什么数据库: ...

  10. C# List 转Datatable

    最近在做Excel导出,看到了这个方法,虽不是自己写的,但值得收藏,但是忘记从那摘抄的,没写原文作者看到望见谅! #region 导出Excel /// <summary> /// lis ...