您的位置:首页 > 移动开发 > Android开发

android使用ViewSwitcher实现视图切换

2014-10-28 21:32 405 查看
activity类:
package com.zzj.ui.viewanimatordemo;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewSwitcher;
import android.widget.ViewSwitcher.ViewFactory;

import com.zzj.ui.R;

public class MainActivity extends Activity {
private ViewSwitcher switcher;
private LayoutInflater inflater;
private List<DataItem> dataItems = new ArrayList<DataItem>();
private int currentScreenNo = -1;// 当前屏数
private int screenCount = 0;// 总屏数
private final int perScreenCount = 12;// 每一屏显示数量

private BaseAdapter adapter = new GridViewAdapter();

int[] images = { R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewanimator_activity);
inflater = LayoutInflater.from(this);

switcher = (ViewSwitcher) findViewById(R.id.viewSwitcher1);
// 设置每一屏显示的视图,这里设置为GridView
switcher.setFactory(new ViewFactory() {

@Override
public View makeView() {
return inflater.inflate(R.layout.viewanimator_gridview, null);
}
});

for (int i = 0; i < images.length; i++) {
DataItem dataItem = new DataItem();
dataItem.lable = "lable" + i;
dataItem.drawable = getResources().getDrawable(images[i]);
dataItems.add(dataItem);
}

// 计算总屏数
if (dataItems.size() % perScreenCount == 0) {
screenCount = dataItems.size() / perScreenCount;
} else {
screenCount = dataItems.size() / perScreenCount + 1;
}

Log.d("screenCount", String.valueOf(screenCount));

next(null);// 页面加载时显示第一页
}

// 下一屏
public void next(View view) {
if (currentScreenNo < screenCount - 1) {
currentScreenNo++;
// 设置视图切换动画
switcher.setInAnimation(this, R.anim.slide_in_right);// 自定义动画效果
switcher.setOutAnimation(this, R.anim.slide_out_left);// 自定义动画效果
// 获取下一屏GridView(通过ViewFactory设置),并且设置数据适配器
((GridView) switcher.getNextView()).setAdapter(adapter);
switcher.showNext();
}
}

// 上一屏
public void prev(View view) {
if (currentScreenNo > 0) {
currentScreenNo--;
// 设置视图切换动画
switcher.setInAnimation(this, android.R.anim.slide_in_left);// android系统自带的动画效果
switcher.setOutAnimation(this, android.R.anim.slide_out_right);// android系统自带的动画效果
// 获取下一屏GridView(通过ViewFactory设置),并且设置数据适配器
((GridView) switcher.getNextView()).setAdapter(adapter);
switcher.showNext();
}
}

static class DataItem {
String lable;
Drawable drawable;
}

class GridViewAdapter extends BaseAdapter {

/**
* 当前屏的数量
*/
@Override
public int getCount() {
if (currentScreenNo < screenCount - 1) {
return perScreenCount;
}
return dataItems.size() - (screenCount - 1) * perScreenCount;
}

/**
* 数据
*/
@Override
public Object getItem(int position) {
int index = currentScreenNo * perScreenCount + position;
return dataItems.get(index);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.lable_icon, null);
}
ImageView imageView = (ImageView) convertView
.findViewById(R.id.imageView1);
DataItem dataItem = (DataItem) getItem(position);
imageView.setImageDrawable(dataItem.drawable);
TextView textView = (TextView) convertView
.findViewById(R.id.textView1);
textView.setText(dataItem.lable);
return convertView;
}

}

}

主布局文件viewanimator_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ViewSwitcher
android:id="@+id/viewSwitcher1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ViewSwitcher>

<Button
android:id="@+id/button_prev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:onClick="prev"
android:text="<" />

<Button
android:id="@+id/button_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="next"
android:text=">" />

</RelativeLayout>


布局文件viewanimator_gridview.xml:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3" />
布局文件lable_icon.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>


自定义动画效果res/anim/slide_in_right.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p"
android:toXDelta="0"
android:duration="@android:integer/config_mediumAnimTime"/>

</set>
自定义动画效果res/anim/slide_out_left.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0"
android:toXDelta="-100%p"
android:duration="@android:integer/config_mediumAnimTime"/>

</set>
运行效果:



下一屏:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐