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. android 之Fragment(官网资料翻译)

    原文地址: http://blog.csdn.net/lilu_leo/article/details/7671533 ***************************  正文分割线 ***** ...

  2. Sicily-1050 深度优先搜索

    一.      题意 给出5个数和4则运算,看能不能算出目标值出来,如果算不出来就算出比目标值小的最大值.深搜:每一步选两个数做运算,然后算出的结果作为下一步的其中一个操作数.每一步选数有C(5,2) ...

  3. 一个异或加密方案--C语言实现

    核心代码: char encrypt( char f , char c) { return f^c; } int OutEncrypt( char *FilePath, char *SecretWor ...

  4. mahout贝叶斯算法开发思路(拓展篇)2

    如果想直接下面算法调用包,可以直接在mahout贝叶斯算法拓展下载,该算法调用的方式如下: $HADOOP_HOME/bin hadoop jar mahout.jar mahout.fansy.ba ...

  5. weblogic的ejb远程调用

    这是一篇对EJB远程调用的简单范例.      1.环境:win7  + weblogic 12c + myeclipse8.5      2.目的:实现在myeclispe中对weblogic中EJ ...

  6. java实现字符串匹配问题之求两个字符串的最大公共子串

    转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/38924981 近期在项目工作中有一个关于文本对照的需求,经过这段时间的学习,总结 ...

  7. 淘特房产CMS系统 7.5

    资源描写叙述: 淘特房产CMS系统採用淘特AspCms开发,全部前台信息生成静态HTM,提供了楼盘.二手房.房产中介.房产经济人.业主社区等管理模块,集成了淘特CMS与动网论坛,Discuz,Oblo ...

  8. C#语言基础之转义字符、变量、常量、类型转换

    1.转义字符: Tab键:/t    反斜杠://   单引号:/’   双引号:/”   回车:/r   换行:/n 警告:/a      退格:/b    换页:/f      空:/0 2.变量 ...

  9. 裸机离奇事件:Freescale usb 有关fault

    裸机离奇事件:Freescale usbucosiiFreescale\KSDK_1.2.0\examples\twrk65f180m\demo_apps\usb\host\cdc\cdc_seria ...

  10. [LeetCode]题解(python):063-Unique Paths II

    题目来源: https://leetcode.com/problems/unique-paths-ii/ 题意分析: 这题的规则和上一题一样.给一个m×n的矩阵0,1矩阵.0代表可以经过,1代表不可以 ...