strings.xml
-------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">com.viewpager!</string>
<string name="app_name">com.viewpager</string>
<string name="tab_1">动态</string>
<string name="tab_2">群组</string>
<string name="tab_3">好友</string>
<string name="tab_4">会话</string>
</resources>
color.xml
-------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="lightwhite">#ddd</color>
<color name="white">#fff</color>
</resources>
activity_main.xml
-------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:umadsdk="http://schemas.android.com/apk/res/com.LoveBus"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/top_theme_blue"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dip" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="36dip"
android:layout_height="36dip"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dip"
android:src="@drawable/headimg" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dip"
android:text="UserName"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="10dip"
android:src="@drawable/status_online" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dip"
android:paddingTop="10dip" >
<TextView
android:id="@+id/tv_tab_activity"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:gravity="center"
android:text="@string/tab_1"
android:textColor="@color/white"
android:textSize="18dip" />
<TextView
android:id="@+id/tv_tab_groups"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:gravity="center"
android:text="@string/tab_2"
android:textColor="@color/lightwhite"
android:textSize="18dip" />
<TextView
android:id="@+id/tv_tab_friends"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:gravity="center"
android:text="@string/tab_3"
android:textColor="@color/lightwhite"
android:textSize="18dip" />
<TextView
android:id="@+id/tv_tab_chat"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:gravity="center"
android:text="@string/tab_4"
android:textColor="@color/lightwhite"
android:textSize="18dip" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="vertical"
android:paddingBottom="3dip" >
<ImageView
android:id="@+id/iv_bottom_line"
android:layout_width="40dip"
android:layout_height="2dip"
android:layout_marginLeft="20dip"
android:scaleType="matrix"
android:src="#fff" />
</LinearLayout>
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vPager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.0"
android:background="#000000"
android:flipInterval="30"
android:persistentDrawingCache="animation" />
</LinearLayout>
|
|
page1.xmla
-------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
page2.xml
-------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FF8684" >
</LinearLayout>
page3.xml
-------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#1586FF" >
</LinearLayout>
MainActivity.java
-------------------------------------------------------------------------------------------------------------------------------
package com.viewpager;
import java.util.ArrayList;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
/**
*
* 项目名称:com.viewpager
* 类 名 称:MainActivity
* 类 描 述:ViewPager+Fragment整合
* 创 建 人:fy
* 创建时间:2014-3-17 下午5:04:55
* Copyright (c) 方勇-版权所有
*/
public class MainActivity extends FragmentActivity implements View.OnClickListener, OnPageChangeListener {
private static final String TAG = "fy";
/* 多页面滑动切换 */
private ViewPager mPager;
/* 多页面 */
private ArrayList<Fragment> fragmentsList;
/* 导航菜单页下的白色下划线 */
private ImageView ivBottomLine;
/* 文本,动态、群组、好友、会话 */
private TextView tvTabActivity, tvTabGroups, tvTabFriends, tvTabChat;
/* 页面所在位置 */
private int currIndex = 0;
/* 白色下划线宽度 */
private int bottomLineWidth;
/* 动画效果-->偏移量 */
private int offset = 0;
/* 动画效果--> */
private int position_one;
private int position_two;
private int position_three;
private Resources resources;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
resources = getResources();
// InitTextView();
findViews();
setListeners();
InitWidth();
InitViewPager();
}
private void findViews() {
tvTabActivity = (TextView) findViewById(R.id.tv_tab_activity);
tvTabGroups = (TextView) findViewById(R.id.tv_tab_groups);
tvTabFriends = (TextView) findViewById(R.id.tv_tab_friends);
tvTabChat = (TextView) findViewById(R.id.tv_tab_chat);
mPager = (ViewPager) findViewById(R.id.vPager);
ivBottomLine = (ImageView) findViewById(R.id.iv_bottom_line);
}
private void setListeners() {
tvTabActivity.setOnClickListener(this);
tvTabGroups.setOnClickListener(this);
tvTabFriends.setOnClickListener(this);
tvTabChat.setOnClickListener(this);
mPager.setOnPageChangeListener(this);
}
private void InitViewPager() {
fragmentsList = new ArrayList<Fragment>();
Fragment activityfragment = MyFragment.newInstance("Hello Activity.");
Fragment groupFragment = MyFragment.newInstance("Hello Group.");
Fragment friendsFragment = MyFragment.newInstance("Hello Friends.");
Fragment chatFragment = MyFragment.newInstance("Hello Chat.");
fragmentsList.add(activityfragment);
fragmentsList.add(groupFragment);
fragmentsList.add(friendsFragment);
fragmentsList.add(chatFragment);
//加载适配器数据
mPager.setAdapter(new MyAdapter(getSupportFragmentManager(), fragmentsList));
//默认选中第一个页面(动态页面)
mPager.setCurrentItem(0);
}
private void InitWidth() {
// 白色下划线宽度
bottomLineWidth = ivBottomLine.getLayoutParams().width;
Log.d(TAG, "cursor imageview width=" + bottomLineWidth);
/*A structure describing general information about a display, such as its * size, density, and font scaling.*/
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
//屏幕宽度
int screenW = dm.widthPixels;
//偏移量
offset = (int) ((screenW / 4.0 - bottomLineWidth) / 2);
Log.i("MainActivity", "offset=" + offset);
/*Change in X coordinate to apply at the start of the animation*/
position_one = (int) (screenW / 4.0);
position_two = position_one * 2;
position_three = position_one * 3;
}
@Override
public void onClick(View v) {
mPager.setCurrentItem(v.getId());
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int arg0) {
Animation animation = null;
switch (arg0) {
case 0://动态
/*动画效果*/
if (currIndex == 1) {
animation = new TranslateAnimation(position_one, 0, 0, 0);
tvTabGroups.setTextColor(resources.getColor(R.color.lightwhite));
} else if (currIndex == 2) {
animation = new TranslateAnimation(position_two, 0, 0, 0);
tvTabFriends.setTextColor(resources.getColor(R.color.lightwhite));
} else if (currIndex == 3) {
animation = new TranslateAnimation(position_three, 0, 0, 0);
tvTabChat.setTextColor(resources.getColor(R.color.lightwhite));
}
tvTabActivity.setTextColor(resources.getColor(R.color.white));
break;
case 1://群组
/*动画效果*/
if (currIndex == 0) {
animation = new TranslateAnimation(offset, position_one, 0, 0);
tvTabActivity.setTextColor(resources.getColor(R.color.lightwhite));
} else if (currIndex == 2) {
animation = new TranslateAnimation(position_two, position_one, 0, 0);
tvTabFriends.setTextColor(resources.getColor(R.color.lightwhite));
} else if (currIndex == 3) {
animation = new TranslateAnimation(position_three, position_one, 0, 0);
tvTabChat.setTextColor(resources.getColor(R.color.lightwhite));
}
tvTabGroups.setTextColor(resources.getColor(R.color.white));
break;
case 2://好友
/*动画效果*/
if (currIndex == 0) {
animation = new TranslateAnimation(offset, position_two, 0, 0);
tvTabActivity.setTextColor(resources.getColor(R.color.lightwhite));
} else if (currIndex == 1) {
animation = new TranslateAnimation(position_one, position_two, 0, 0);
tvTabGroups.setTextColor(resources.getColor(R.color.lightwhite));
} else if (currIndex == 3) {
animation = new TranslateAnimation(position_three, position_two, 0, 0);
tvTabChat.setTextColor(resources.getColor(R.color.lightwhite));
}
tvTabFriends.setTextColor(resources.getColor(R.color.white));
break;
case 3://会话
/*动画效果*/
if (currIndex == 0) {
animation = new TranslateAnimation(offset, position_three, 0, 0);
tvTabActivity.setTextColor(resources.getColor(R.color.lightwhite));
} else if (currIndex == 1) {
animation = new TranslateAnimation(position_one, position_three, 0, 0);
tvTabGroups.setTextColor(resources.getColor(R.color.lightwhite));
} else if (currIndex == 2) {
animation = new TranslateAnimation(position_two, position_three, 0, 0);
tvTabFriends.setTextColor(resources.getColor(R.color.lightwhite));
}
tvTabChat.setTextColor(resources.getColor(R.color.white));
break;
}
//当前白色下划线位置
currIndex = arg0;
//保留在终止位
animation.setFillAfter(true);
//设置动画持续时间
animation.setDuration(300);
//执行动画
ivBottomLine.startAnimation(animation);
}
/*
* 1、该 PagerAdapter 的实现将只保留当前页面,当页面离开视线后,就会被消除,释放其资源;
* 这么实现的好处就是当拥有大量的页面时,不必在内存中占用大量的内存
* 2、而FragmentPagerAdapter的每一个生成的 Fragment 都将保存在内存之中,
* 因此适用于那些相对静态的页,数量也比较少的那种
*/
public static class MyAdapter extends FragmentStatePagerAdapter {
private ArrayList<Fragment> fragmentsList;
public MyAdapter(FragmentManager fm) {
super(fm);
}
public MyAdapter(FragmentManager fm, ArrayList<Fragment> fragments) {
super(fm);
this.fragmentsList = fragments;
}
@Override
public int getCount() {
return fragmentsList.size();
}
@Override
public Fragment getItem(int position) {
return fragmentsList.get(position);
}
}
}
MyFragment.java
-------------------------------------------------------------------------------------------------------------------------------
package com.viewpager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
*
* 项目名称:com.viewpager
* 类 名 称:MyFragment
* 类 描 述:页面模板
* 创 建 人:fy
* 创建时间:2014-3-17 下午4:36:36
* Copyright (c) 方勇-版权所有
*/
public class MyFragment extends Fragment {
private static final String TAG = "fy";
private String hello;// = "hello android";
private String defaultHello = "default value";
static MyFragment newInstance(String s) {
MyFragment newFragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putString("hello", s);
newFragment.setArguments(bundle);
return newFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "fy-----onCreate");
Bundle args = getArguments();
hello = args != null ? args.getString("hello") : defaultHello;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
Log.d(TAG, "fy-----onCreateView");
View view = inflater.inflate(R.layout.page1, container, false);
TextView viewhello = (TextView) view.findViewById(R.id.tv_hello);
viewhello.setText(hello);
return view;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "TestFragment-----onDestroy");
}
}
效果
-------------------------------------------------------------------------------------------------------------------------------
<!--EndFragment-->
<!--EndFragment-->
相关推荐
本资源是提供给各位刚了解ViewPage使用的小伙伴。不过这个资源我按照了开发的模式,抽取了一些基类来写这个简单的功能,就当练习。希望小伙伴能看懂ViewPage的使用。代码在basedemo这个模块里面写的。而代码用...
ViewPage1.31 for 360se 2.0+版,可以通过调整配置文件使插件图标显示在工具栏,也可以显示在状态栏,调整参数“Type=TOOLBAR_BUTTON;在工具栏显示”“Type=STATUSBUTTON;在状态栏显示”,使用注释符号“;”来开启...
viewpage嵌套listview,效果:滑动listview删除,滑动非listview区域viewpage翻页
ViewPage1.21 for 360se 通用版,可以通过调整配置文件使插件图标显示在工具栏,也可以显示在状态栏,调整参数“Type=TOOLBAR_BUTTON;在工具栏显示”“Type=STATUSBUTTON;在状态栏显示”,使用注释符号“;”来开启...
ViewPage自动轮播图片
Android viewpage分页源码
Tab+Viewpage+Fragment实现导航源代码,是一个很不错的Android源码,有兴趣的伙伴们抽时间可以看一下把
ViewPage(无限循环)
一个ViewPage可动态添加页面的例子,更灵活的开发方式。
简单的重新viewpage类,可以控制关闭viewpage的侧滑事件。
相信大家做ViewPage循环滑动只有2页的时候都会出现页面不显示的问题。这个demo解决了2页图片不显示问题;有两个demo;第一个ViewPagerDemo(2张图片循环显示 通过Bitmap和PhotoView来解决,图片可捏合放大缩小);第...
ViewPage 是一款浏览器辅助软件,支持 Internet Explorer / Maxthon / MyIE2 / TheWorld / TT 等大多数流行的浏览器,它提供了丰富、强大的功能来满足您分析 HTML 页面和提取页面资源的需要。它不仅是 WEB 程序员的...
可以在ViewPage里左右循环查看图片,自定义的标签可以控制显示哪个Viewpage里的数据!
OverlayCardViewPager 层叠卡片效果的ViewPage
android viewpage例子,并且底栏有可滑动按钮,和viewpage滑动互相控制,项目源码,直接解压
Android移动应用开发之Viewpage2+fragment实现微信滑动界面的效果
采用viewpage,实现app开始的页面导航功能,自动播放图片
ViewPage实现图片集的左右循环滑动
吸顶结合tabLayout+viewpage+fragment
ViewPage 左右滑动实例。直接导入eclipse可以使用。 适合初学者了解ViewPage, 适用于第一次进入应用程序的介绍界面。