标签:
完成这个例子的步骤:
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来写死。
标签:
原文地址:http://www.cnblogs.com/exmyth/p/4544404.html