viewpage可以做简单的导航,到页面菜单,可以使用视图滑动,实现像lanucher左右滑动等等,类似listview,也需要个适配器pageradapter.
使用viewpager只有有四个步骤:
1.在布局文件中加入控件viewpager,这个组件,注意这个组件是用来显示左右滑动的界面的,如果不加载xml布局文件,他是不会显示内容的
2.
加载要显示的页卡 3.
viewpager组件设定一个page适配器,
它是基类提供适配器来填充页面ViewPager内部,你很可能想要使用一个更具体的实现,如FragmentPagerAdapter
FragmentStatePagerAdapter。 4.给viewpager添加OnPageChangeListener事件来实现切换的动画等功能(这一步不是必须的) 通常情况下:
viewpager和frament经常一起使用,谷歌也是这样说的。
简单 介绍下片段frament :注意在导入
android.support.v4 1.frament作为activity界面的一部分组成出现。 2.可以一个activity界面同时出现对个frament,并且一个frament也可以再多个activity中使用 3.在activity运行中可以动态添加,移除,替换frament 4.frament可以响应自己的输入事件,并且有自己的生命周期,不过他的生命周期要被宿主activity的生命周期影响。
frament的生命周期介绍和使用操作见: http://blog.youkuaiyun.com/aomandeshangxiao/article/details/7671533 http://www.cnblogs.com/xinye/archive/2012/08/28/2659712.html 下面是我在网上找到的一个例子,是frament和viewpager一起使用实现qq界面,效果对我们这些新手来说还不错。。 来自:http://www.apkbus.com/android-88510-1-1.html 按照步骤使用viewpager 1.在布局文件引入控件viewpager
复制内容到剪贴板
代码:
<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" /> <!-- android:persistentDrawingCache="animation" 定义绘图的高速缓存的持久性。 绘图缓存可能由一个 ViewGroup 在特定情况下为其所有的子类启用, 例如在一个滚动的过程中。 此属性可以保留在内存中的缓存后其初始的使用。 坚持缓存会消耗更多的内存,但可能会阻止频繁的垃圾回收是反复创建缓存。 默认情况下持续存在设置为滚动 -->
2.加载要显示的页卡
复制内容到剪贴板
代码:
Fragment activityfragment = TestFragment.newInstance("Hello Activity.");//这里使用单例模式,详情见源码 Fragment groupFragment = TestFragment.newInstance("Hello Group.");//TestFrament继承frament的 Fragment friendsFragment=TestFragment.newInstance("Hello Friends."); Fragment chatFragment=TestFragment.newInstance("Hello Chat."); fragmentsList.add(activityfragment); fragmentsList.add(groupFragment); fragmentsList.add(friendsFragment); fragmentsList.add(chatFragment); MyFragmentPagerAdapter adapter=new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentsList));
3.
viewpager组件设定一个page适配器,这里使用了
FragmentPagerAdapter
复制内容到剪贴板
代码:
mPager.setAdapter( adapter);
4.
给viewpager添加OnPageChangeListener事件
复制内容到剪贴板
代码:
public class MyOnPageChangeListener implements OnPageChangeListener { @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); } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } viewpager.setOnPageChangeListener(new MyOnPageChangeListener());
MyOnPageChangeListener监听器主要实现条目切换的动画。
工程代码见:
http://www.apkbus.com/android-88510-1-1.html