Android ViewFlow的一个例子
完成这个例子的步骤:
1.下载ViewFlow的源码,然后将类ViewFlow放在自己的工程的src的某个包下。
2.下载的源码里有2个工程view flow,viewflow-example。将view flow工程里的attr
3.布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/home_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/home_headerLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="9" >
<Button
android:id="@+id/memo_type_edit_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_margin="4dip"
android:background="@drawable/button_bg_select_type"
android:padding="3dip" />
<Button
android:id="@+id/memo_type_btn"
android:layout_width="100dip"
android:layout_height="35dip"
android:layout_centerInParent="true"
android:layout_margin="4dip"
android:background="@drawable/button_bg_down"
android:gravity="center"
android:singleLine="true"
android:text="工作(10)"
android:textColor="@color/white"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/new_memo_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="4dip"
android:background="@drawable/button_bg_add" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/kitty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
<FrameLayout
xmlns:app="http://schemas.android.com/apk/res/com.bst.memo.activity"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.lx.viewflow.ViewFlow <!-- 这个类就是拷贝下载的ViewFlow类 -->
android:id="@+id/home_viewflow"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="25dip" />
<com.lx.viewflow.CircleFlowIndicator <!-- 这个类也是下载的工程viewflow中的 -->
android:id="@+id/viewflowindic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
app:fadeOut="1000"
app:inactiveType="fill"
android:paddingTop="10dip" />
</FrameLayout>
<ImageView
android:id="@+id/setting"
android:layout_width="24dip"
android:layout_height="24dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="10dip"
android:layout_marginRight="10dip"
android:src="@drawable/i" />
</RelativeLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/adlayout"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:layout_gravity="center"
android:background="@drawable/adbg" >
</LinearLayout>
</LinearLayout>
</LinearLayout>
4.为ViewFlow写一个适配器:
public class ViewFlowAdapter extends BaseAdapter implements OnItemClickListener {
private LayoutInflater mInflater;
private MemoActivity memoActivity; //主Activity
private int selectedPosition = 0;
private List<List<MemoBean>> list =new ArrayList<List<MemoBean>>(); //要显示在ViewFlow中的数据
public GalleryAdapter(MemoActivity activity, List<MemoBean> memoBeanList) {
memoActivity = activity;
mInflater = LayoutInflater.from(activity);
initList(memoBeanList);
}
public void setSelectedPosition(int position) {
this.selectedPosition = position;
}
public void updateData(List<MemoBean> memoBeanList) {
if(list!=null){
list.clear();
}
initList(memoBeanList);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
//初始化ViewFlow的各个页的数据
private void initList(List<MemoBean> memoBeanList){
if(memoBeanList==null){
return;
}
int totalCount = memoBeanList.size();
int pages =0;
if(totalCount%Config.PAGE_SIZE==0){
pages = totalCount/Config.PAGE_SIZE; //Config.PAGE_SIZE控制每页显示多少个
}else{
pages = totalCount/Config.PAGE_SIZE+1;
}
for(int i=1;i<=pages;i++){
list.add(getCurrentPageList(i,memoBeanList));
}
}
//获取当前页的数据
private List<MemoBean> getCurrentPageList(int page,List<MemoBean> memoBeanList) {
List<MemoBean> list = new ArrayList<MemoBean>();
if(memoBeanList!=null && memoBeanList.size()>0){
int totalCount = memoBeanList.size();
int start = 0;
int end = 0;
start = (page - 1) * Config.PAGE_SIZE;
end = start + Config.PAGE_SIZE;
if (end > totalCount) {
end = totalCount;
}
for (int i = start; i < end; i++) {
list.add(memoBeanList.get(i));
}
}
return list;
}
//释放ViewFlow占的资源
public void releaseViewFlow(int currentPosition, ViewFlow viewFlow) {
List<View> views = viewFlow.mLoadedViews;
int size = views.size();
for (int i = 0; i < size; i++) {
if (i != currentPosition){ //我的ViewFlow的每一页对应的布局是一个LinearLayout,LinearLayout里面有个GridView
LinearLayout layout = (LinearLayout) views.get(i);
GridView gridView = (GridView) layout.getChildAt(0);
GridViewAdapter adapter = (GridViewAdapter) gridView.getAdapter();
adapter.clear(); //释放GridView占用的资源
System.gc();
}
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
List<MemoBean> memos = list.get(position);
//viewflow_page这个布局对应的就是每页该怎么展示的布局,一个包含GridView的LinearLayout,
convertView = mInflater.inflate(R.layout.viewflow_page, null);
GridView gridView = (GridView) convertView.findViewById(R.id.gridview);
gridView.setOnItemClickListener(this);
GridViewAdapter adapter = (GridViewAdapter) gridView.getAdapter();
if (adapter == null) {
adapter = new GallaryItemAdapter(memoActivity, memos);
gridView.setAdapter(adapter);
} else {
adapter.setMemoList(memos);
adapter.notifyDataSetChanged();
}
}
return convertView;
}
//点击每页里面的GridView的Item的处理
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
memoActivity.finish();
Intent intent = new Intent();
intent.setClass(memoActivity, EditActivity.class);
MemoBean memoBean = (MemoBean) ((GridView) parent)
.getItemAtPosition(position);
MemoTypeBean typeBean = new MemoTypeBean(memoActivity);
typeBean = typeBean.getBean(memoBean.getMemoTypeId());
Bundle bundle = new Bundle();
bundle.putSerializable("memo_bean", memoBean);
bundle.putSerializable("type_id", MemoActivity.selectedTypeId);
int start = selectedPosition* Config.PAGE_SIZE+position;
bundle.putInt("current_position", start);
intent.putExtras(bundle);
memoActivity.startActivity(intent);
}
public void clear(){
list.clear();
list=null;
}
}
5.主Activity MemoActivity的初始化ViewFlow代码:
mViewFlow = (ViewFlow) findViewById(R.id.home_viewflow);
List<MemoBean> memoList = myMemoBean.getMemoListByType(selectedTypeId);
ViewFlowAdapter adapter = new GalleryAdapter(this, memoList);
mViewFlow.setAdapter(adapter);
if(memoList!=null && memoList.size()>0){
mViewFlow.setSelection(0);
}
6.效果图

注意点:
由于在ViewFlow中的onMeasure方法里,有下面这段代码:
if (widthMode != MeasureSpec.EXACTLY && !isInEditMode()) {
throw new IllegalStateException(
"ViewFlow can only be used in EXACTLY mode.");
}
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY && !isInEditMode()) {
throw new IllegalStateException(
"ViewFlow can only be used in EXACTLY mode.");
}
所以布局时,ViewFlow的layout_width,layout_height是属于确定的模式。按layout_weight来写,或按dip来写死。
Android ViewFlow的一个例子的更多相关文章
- Android怎么让一个service开机自动启动
1.首先开机启动后系统会发出一个Standard Broadcast Action,名字叫android.intent.action.BOOT_COMPLETED,这个Action只会发出一次. 2. ...
- android JNI处理图片的例子
android JNI处理图片的例子 原地址:http://blog.csdn.net/xjwangliang/article/details/7065670 <pre class=" ...
- 二维码合成,将苹果和安卓(ios和android)合成一个二维码,让用户扫描一个二维码就可以分别下载苹果和安卓的应用
因为公司推广的原因,没有合适的将苹果和安卓(ios和android)合成一个二维码的工具. 因为这个不难,主要是根据浏览器的UA进行判断,所以就自己开发了一个网站 网站名称叫:好推二维码 https ...
- Android 如何判断一个应用在运行(转)
Android 如何判断一个应用在运行 在一个应用中,或一个Service .Receiver中判断一个应用是否正在运行,以便进行一些相关的处理. 这个时候我们需要得到一个ActivityManag ...
- spring笔记--使用springAPI以及自定义类 实现AOP的一个例子
Spring的另一个重要思想是AOP,面向切面的编程,它提供了一种机制,可以在执行业务前后执行另外的代码,Servlet中的Filter就是一种AOP思想的体现,下面通过一个例子来感受一下. 假设我们 ...
- android studio 导入一个已有的android studio project作为lib使用
android studio 导入一个已有的android studio project作为lib使用 新项目来了. 需要搭建框架. android studio对我来说还是很陌生,之前一个项目在同事 ...
- Android Studio新建一个HelloWorld 程序(App)
Android Studio新建一个HelloWorld程序(App) 新建 或者直接启动程序(注:如果已有程序,此方法会直接打开最近一次关闭从程序) 更改App名 选择App运行平台 选择模板 更改 ...
- ReCap 360 photo照片建模技术的又一个例子
这是我做的又一个利用Autodesk ReCap 360 照片建模技术做的一个例子.你可以下载模型自己把玩,或者下载原始照片自己试一试. 拍摄工具: 小米手机 照片数量:约120张 后期处理工具: p ...
- 从一个例子中体会React的基本面
[起初的准备工作] npm init npm install --save react react-dom npm install --save-dev html-webpack-plugin web ...
随机推荐
- c#中去掉字符串空格方法
(1)Trim方法 string tt=" aaa "; tt=tt.Trim() 去字符串首尾空格的函数 tt=tt.TrimEnd() 去掉字符串尾空格 tt= ...
- scn转换为十进制
- 64位操作系统下IIS报“试图加载格式不正确的程序”错误
缘由:在64位操作系统下IIS发布32位的项目,报“项目依赖的dll无法读取,试图加载格式不正确的程序”错误. 原因:程序集之间的通讯要么全是64位环境下的,要么全是32位环境下的.不能混编访问.不然 ...
- 学习springMVC实例1——配置和跳转到HelloWorld
本文让大家迅速掌握springMVC的使用方法,以最简单的方式理解此框架 一.用eclipse新建一个web项目,命名为springMVC1,tomcat的端口号为9090 二.在WEB-INF目录下 ...
- [C++] namespace相关语法
本段测试代码包括如下内容: (1) 如何访问namespace中声明的名称:(2) namespace导致的相关冲突:(3) namespace可嵌套:(4) 可以在namespace中使用using ...
- .NET,你真的 知道了吗
搞清自己是干什么的 有人问你是做什么的,回答是:"我是做,NET开发的",有的人也会问:"那.NER.是什么?"刚开始我认为是一个开打工具,后认为是一个平台,一 ...
- EclipsePHP Studio 常用设置笔记
工作需要,学习PHP使用EclipsePHP Studio开发工具, 习惯整理下常用的使用设置,分享一下吧: 1.窗口-首选项-常规-工作空间,把文本文件编码改为utf8,以后再新建文件就默认是utf ...
- tomcat正常启动,但IP不能访问web。ping IP地址,一直超时。 用ipconfig命令修复TCP/IP的配置信息
今天遇到一个好奇葩的问题 好吧是昨天遇到的一直没找到解决办法(`へ´) tomcat正常启动,但是通过IP不能访问web 用IP地址就是不行 (:′⌒`) 打不开 localhost就可以 ...
- LRU 算法简单实现
在学习很多服务器软件中,当内存不够,而需要淘汰内存的时候,一般会使用LRU算法,便产生了浓厚的兴趣.在学习操作系统的过程中发现LRU在系统中用寄存器和栈来实现.所以我就尝试着学习用栈来解决LRU的问题 ...
- Keil_uvision_4基本使用教程
Keil C51 V9.00 即09年发布的最新版本uVision 4,版本外观改变比较大,可以使用以前的注册文件.如果全新安装,在VISTA或者WIN 7系统下,请使用管理员方式运行,然后注册即可无 ...