android使用ViewSwitcher实现视图切换

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>
运行效果:

下一屏:



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值