(转)android中利用 ViewPage 实现滑动屏

最近实现了这样的一个效果:滑动界面出现拖拽效果,可翻动3屏,也可点击按钮翻动页面。
主要利用android.support.v4.view.ViewPager控件来实现。
第一个界面:
滑动屏幕:
换到下一屏:
布局文件:
主界面 main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/guidePages"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/viewGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/pre_one_button"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight = "1"
android:textSize="18sp"
android:textColor="@android:color/black"
android:text="本周"
android:padding="7dp"
android:textStyle="bold"
android:background="@drawable/button_selected"
/>
<Button
android:id="@+id/pre_two_button"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight = "1"
android:textSize="18sp"
android:textColor="@android:color/black"
android:text="前一周"
android:padding="7dp"
android:textStyle="bold"
android:background="@drawable/button_unselected"
/>
<Button
android:id="@+id/pre_three_button"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight = "1"
android:textSize="18sp"
android:textColor="@android:color/black"
android:text="前二周"
android:padding="7dp"
android:textStyle="bold"
android:background="@drawable/button_unselected"
/>
</LinearLayout>
</RelativeLayout>
</FrameLayout>
第一屏界面:page01.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" >
<ListView
android:id="@+id/lv01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="50dp"
android:textColor="@android:color/black"
android:cacheColorHint="#00000000"
android:scrollbars="none"
/>
</LinearLayout>
第二屏 ,第三屏与第一屏布局一样,分别叫page02.xml page03.xml,并且要把ListView的 id 改为 lv02 lv03
列表条目布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/subjectLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="@android:color/white"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/homework_icon"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:id="@+id/homework_name"
android:layout_toRightOf="@id/homework_icon"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="18sp"
android:id="@+id/homework_subject"
android:layout_toRightOf="@id/homework_icon"
android:layout_below="@id/homework_name"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:id="@+id/homework_teacher"
android:layout_toRightOf="@id/homework_icon"
android:layout_below="@id/homework_subject"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:id="@+id/homework_submit_date"
android:layout_toRightOf="@id/homework_icon"
android:layout_below="@id/homework_teacher"
/>
</RelativeLayout>
Java代码:
public class MainActivity extends Activity {
private ViewPager viewPager;
private ArrayList<View> pageViews;
private ViewGroup buttonsLine;
private Button button01;
private Button button02;
private Button button03;
private Button[] buttons;
private ListView lv01;
private ListView lv02;
private ListView lv03;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = getLayoutInflater();
pageViews = new ArrayList<View>();
//每页de界面
View page01=inflater.inflate(R.layout.page01, null);
View page02=inflater.inflate(R.layout.page02, null);
View page03=inflater.inflate(R.layout.page03, null);
pageViews.add(page01); //lee
pageViews.add(page02); //lee
pageViews.add(page03); //lee
lv01 = (ListView) page01.findViewById(R.id.lv01);
lv02 = (ListView) page02.findViewById(R.id.lv02);
lv03 = (ListView) page03.findViewById(R.id.lv03);
lv01.setAdapter(new HomeworkListAdapter(this));
lv02.setAdapter(new HomeworkListAdapter(this));
lv03.setAdapter(new HomeworkListAdapter(this));
//按钮栏
buttons = new Button[pageViews.size()];
buttonsLine = (ViewGroup)inflater.inflate(R.layout.main, null);
button01 = (Button) buttonsLine.findViewById(R.id.pre_one_button);
button02 = (Button) buttonsLine.findViewById(R.id.pre_two_button);
button03 = (Button) buttonsLine.findViewById(R.id.pre_three_button);
buttons[0] = button01;
buttons[1] = button02;
buttons[2] = button03;
button01.setOnClickListener(new GuideButtonClickListener(0));
button02.setOnClickListener(new GuideButtonClickListener(1));
button03.setOnClickListener(new GuideButtonClickListener(2));
viewPager = (ViewPager)buttonsLine.findViewById(R.id.guidePages);
setContentView(buttonsLine);
viewPager.setAdapter(new GuidePageAdapter());
viewPager.setOnPageChangeListener(new GuidePageChangeListener());
}
//列表适配
public class HomeworkListAdapter extends BaseAdapter {
private Context mContext = null;
private LayoutInflater mInflater = null;
public HomeworkListAdapter(Context c) {
mContext = c;
mInflater = LayoutInflater.from(this.mContext);
}
@Override
public int getCount() {
return 8;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.homework_item, null);
//初始化组件
holder.icon =(ImageView) convertView. findViewById(R.id.homework_icon);
holder.name = (TextView) convertView.findViewById(R.id.homework_name);
holder.subject = (TextView) convertView.findViewById(R.id.homework_subject);
holder.teacher = (TextView) convertView.findViewById(R.id.homework_teacher);
holder.date = (TextView) convertView.findViewById(R.id.homework_submit_date);
convertView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
v.setBackgroundResource(R.drawable.item_seleted);
break;
case MotionEvent.ACTION_UP:
v.setBackgroundResource(R.drawable.item_unseleted);
break;
case MotionEvent.ACTION_CANCEL:
v.setBackgroundResource(R.drawable.item_unseleted);
break;
}
return true;
}
});
convertView.setTag(holder);
} else {
holder=(ViewHolder) convertView.getTag();
}
holder.icon.setBackgroundResource(R.drawable.ic_launcher);
holder.name.setText(".getName()");
holder.subject.setText(".getSubjectName()");
holder.teacher.setText(".getTeacherName()");
holder.date.setText(".getSubmitDate()");
return convertView;
}
class ViewHolder {
ImageView icon;
TextView name;
TextView subject;
TextView teacher;
TextView date;
}
}
class GuidePageAdapter extends PagerAdapter {
@Override
public int getCount() {
return pageViews.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return super.getItemPosition(object);
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
// TODO Auto-generated method stub
((ViewPager) arg0).removeView(pageViews.get(arg1));
}
@Override
public Object instantiateItem(View arg0, int arg1) {
// TODO Auto-generated method stub
((ViewPager) arg0).addView(pageViews.get(arg1));
return pageViews.get(arg1);
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
// TODO Auto-generated method stub
}
@Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null;
}
@Override
public void startUpdate(View arg0) {
// TODO Auto-generated method stub
}
@Override
public void finishUpdate(View arg0) {
// TODO Auto-generated method stub
}
}
class GuidePageChangeListener implements OnPageChangeListener {
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageSelected(int arg0) {
for (int i = 0; i < buttons.length; i++) {
buttons[arg0]
.setBackgroundResource(R.drawable.button_selected);
if (arg0 != i) {
buttons[i]
.setBackgroundResource(R.drawable.button_unselected);
}
}
}
}
class GuideButtonClickListener implements OnClickListener {
private int index = 0;
public GuideButtonClickListener(int i) {
index = i;
}
@Override
public void onClick(View v) {
viewPager.setCurrentItem(index, true);
}
}
}
(转)android中利用 ViewPage 实现滑动屏的更多相关文章
- Android中利用Handler实现消息的分发机制(三)
在第二篇文章<Android中利用Handler实现消息的分发机制(一)>中,我们讲到主线程的Looper是Android系统在启动App的时候,已经帮我们创建好了,而假设在子线程中须要去 ...
- Android中利用ant进行多渠道循环批量打包
公司负责Android开发的小伙伴学习能力稍微偏弱,交代给他的自动化打包的任务,弄了好久依然没有成效.无奈只好亲自出手. 没有想到过程很顺利,我完全按照如下文章的步骤进行: 主要参考: Android ...
- Android 中利用ViewFlipper 滑动屏幕切换页面,ListView展示数据
首先新建一个Android项目,命名为ViewFlipperTest 如图:项目机构,本项目主要操作图中红色箭头标注的文件 1.HgroupAdapter.java文件代码↓主要实现listview数 ...
- android中按电源键锁屏然后解锁导致Activity调用onDestory以及如何防止锁屏
今天在android项目中按电源键锁屏,然后解锁,发现子Activity关闭了,回到了主页,这个问题困扰了我很久,最后打log发现,在按电源键的时候,调用了子Activity的onDestroy()方 ...
- android中实现view可以滑动的六种方法续篇(二)
承接上一篇,上一篇中讲解了实现滑动的第五种方法,如果你还没读过,可点击下面链接: http://www.cnblogs.com/fuly550871915/p/4985482.html 这篇文章现在来 ...
- Android中利用Camera与Matrix实现3D效果详解
本文行文目录: 一.Camera与Matrix初步认识 二.Camera与Matrix旋转效果拆分介绍 三.Camera与Matrix实现立体3D切换效果 [csdn地址:http://blog.cs ...
- Android中利用C++处理Bitmap对象
相信有些Android&图像算法开发者和我一样,遇到过这样的状况:要对Bitmap对象做一些密集计算(例如逐像素的滤波),但是在java层写循环代码来逐像素操作明显是不现实的,因为Java代码 ...
- Android中利用jsoup解析html页面
学习jsoup :jsoup学习网站 Android 中使用: 添加依赖 implementation 'org.jsoup:jsoup:1.10.1' 直接上代码: package com.load ...
- Android中监听ListView滑动到底部
Android中的应用就是ListView中向下滑动加载更多的功能,不要再onScroll方法中进行判断,那样当滑动到底部的时候,触摸屏幕就会又去加载更多,效果很差,可以自行测试一下: listvie ...
随机推荐
- myeclipse 2016 激活,myeclipse 2016 激活
myeclipse 2016 激活: 找了好久,myeclipse 2016 终于激活了.myeclipse版本是下载的 myeclipse-2016-ci-0-offline-installer- ...
- Data Base MongoVue 破解治标不治本
MongoVue 破解治标不治本 ---------解决燃眉之急 注册表中查找B1159E65-821C3-21C5-CE21-34A484D54444中的子项4FF78130 ,删除其下的三个子项 ...
- rqnoj-106-最大加权矩形-dp
和我之前做的那个切西瓜的题目相比就是小巫见大巫了.. 运用最长字段和的原理把O(n^4)转化成O(n^3) #include<stdio.h> #include<string.h&g ...
- jvm 内存整理 -----学习
分为:方法区 ,堆 ,栈 ,本地栈 ,程序计数器 1.程序计数器 保存当前线程执行的字节码行号指示器,解释器工作时,都是通过改变计数器的值来获取下一条程序指令,循环.异常.跳转.分支. ...
- U3D NGUI改变GameObject Activity闪烁的问题
不是关闭再激活GameObject会闪烁,而是再激活时,NGUI渲染步骤不一致导致的闪烁. 并且文字激活后渲染要慢一帧,如果延迟一帧处理,又会导致精灵图片快一帧,图片重叠.这个测试结果不一定准确,先记 ...
- DbProviderFactories.GetFactoryClasses
using System.Data.Common; private void Method1() { DataTable table = DbProviderFactories.GetFactoryC ...
- VIM移动
VIM移动 断断续续的使用VIM也一年了,会的始终都是那么几个命令,效率极低 前几个星期把Windows换成了Linux Mint,基本上也稳定了下来 就今晚,我已经下定决心开始新的VIM之旅,顺 ...
- HDU 1851 (巴什博奕 SG定理) A Simple Game
这是由n个巴什博奕的游戏合成的组合游戏. 对于一个有m个石子,每次至多取l个的巴什博奕,这个状态的SG函数值为m % (l + 1). 然后根据SG定理,合成游戏的SG函数就是各个子游戏SG函数值的异 ...
- HDU 1574 RP问题
如果说难的话,难就难在对阶段的划分. 这又是一道对值域空间进行分段的题目. 因为rp有正有负,所以将整个数组向右平移10000个单位长度 l和r分别是rp可能的最小值 因为b是“门槛”,所以如果 发生 ...
- [转] POJ字符串分类
POJ 1002 - 487-3279(基础)http://acm.pku.edu.cn/JudgeOnline/problem?id=1002题意:略解法:二叉查找数,map,快排... POJ 1 ...