(转)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 ...
随机推荐
- 用SQLData读写数据库自定义类型
如何读写自定义类型?SQLData是个很直观的解决办法 在oracle使用手册上找到了很好的资料 点击打开链接 http://docs.oracle.com/cd/B10501_01/java.920 ...
- linux下c程序调用reboot函数实现直接重启【转】
转自:http://www.blog.chinaunix.net/uid-20564848-id-73878.html linux下c程序调用reboot函数实现直接重启 当然你也可以直接调用syst ...
- ARC的内存管理
在objective-c中,内存的引用计数一直是一个让人比较头疼的问题.尤其是当引用计数涉及到arc.blocks等等的时候.似乎ARC的出现只是让我们解放了双手,由于底层实现依然依赖引用计数 ...
- php实现新闻页面
首页 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8& ...
- 详解javascript中的call, apply
一些学js的同学一看到call, apply, 就蒙了, 感觉不好懂, 看的头大. 今天我们就一起来研究一下这2个东东.彻底弄清楚它们的用法. 定义: call, apply是函数的方法, 只有函数才 ...
- 漫游Kafka设计篇之性能优化
Kafka在提高效率方面做了很大努力.Kafka的一个主要使用场景是处理网站活动日志,吞吐量是非常大的,每个页面都会产生好多次写操作.读方面,假设每个消息只被消费一次,读的量的也是很大的,Kafka也 ...
- spring、springmvc、mybatis整合笔记
这段时间上一个项目刚做完,下一个项目还没开始,趁这个时候来认真总结一下上个项目使用的ssm开发框架.由于,项目中关于使用ssm这部分的代码和配置是我们项目的整体架构师一个独立完成的,我们只负责业务部分 ...
- BZOJ 3406 乳草的入侵
BFS. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm&g ...
- Android 源代码自动编译packages/apps
/*************************************************************************** * Android 源代码自动编译packag ...
- PHP中括号“{}”的3个作用
1,将多个独立语句合并为一个复合语句,例如“if .... else ....”中推荐如此使用. 2,在变量的间接引用中进行定界,避免歧义.例如“${$my_var[8]}”与“${$my_var}[ ...