ViewFlipper的使用,显示图片左右滑动效果

1.简介

          ViewFlipper extends ViewAnimator,Simple ViewAnimator that will animate between two or more views that have been added to it. Only one child is shown at a time. If requested, can automatically flip between each child at a regular interval。

           简单的ViewAnimator 可以在两个或两个以上的视图间实现动画效果,但是一次只能显示一个子类。如果设置的话,子类可以按照一定规律来显示。

 

2. 具体使用

 边上代码边解释

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent" android:background="#ffffff">
<!-- 滑动翻页显示 -->
	<ViewFlipper
		android:orientation="vertical" android:id="@+id/ViewFlipper"
		android:layout_width="fill_parent" android:layout_height="75dip"
		android:background="@drawable/gradient_dark_purple">

		<!-- (0) Loading -->
		<LinearLayout android:orientation="vertical"
			android:layout_width="fill_parent" android:layout_height="fill_parent"
			android:layout_marginLeft="15dip" android:gravity="left|center_vertical">
			<com.teleca.jamendo.widget.ProgressBar
				android:id="@+id/ProgressBar" android:layout_width="wrap_content"
				android:layout_height="wrap_content">
			</com.teleca.jamendo.widget.ProgressBar>
		</LinearLayout>

		<!-- (1) Gallery -->
		<LinearLayout android:orientation="vertical"
			android:layout_width="fill_parent" android:layout_height="fill_parent"
			android:gravity="center">
			<Gallery android:id="@+id/Gallery" android:layout_width="fill_parent"
				android:layout_height="wrap_content" android:spacing="0px" />
		</LinearLayout>

		<!-- (2) Failure -->
		<LinearLayout android:orientation="vertical"
			android:layout_width="fill_parent" android:layout_height="fill_parent"
			android:layout_marginLeft="15dip" android:gravity="left|center_vertical">
			<com.teleca.jamendo.widget.FailureBar
				android:id="@+id/FailureBar" android:layout_width="wrap_content"
				android:layout_height="wrap_content">
			</com.teleca.jamendo.widget.FailureBar>
		</LinearLayout>
	</ViewFlipper>
</LinearLayout>

 在viewflipper中有三个线性布局,分别为初始加载和加载后以及加载失败。当我们在activity中这个viewflipper实例化时,我们打印

mViewFlipper.getDisplayedChild()

 

会得到3。说明三个linnerlayout为她的子类。同时为了怕给主线程增加负担,我们采用异步实现。

  

private class NewsTask extends AsyncTask<Void, WSError, Album[]> {

		@Override
		public void onPreExecute() {
			mViewFlipper.setDisplayedChild(0);
			
			System.out.println("子类数目----"+mViewFlipper.getChildCount()+" "+mViewFlipper.getDisplayedChild());
			
			mProgressBar.setText(R.string.loading_news);
			super.onPreExecute();
		}

		@Override
		public Album[] doInBackground(Void... params) {
			JamendoGet2Api server = new JamendoGet2ApiImpl();
			Album[] albums = null;
			try {
				albums = server.getPopularAlbumsWeek();
			} catch (JSONException e) {
				e.printStackTrace();
			} catch (WSError e){
				publishProgress(e);
			}
			System.out.println("doInBackground"+mViewFlipper.getDisplayedChild());
			return albums;
		}

		@Override
		public void onPostExecute(Album[] albums) {

			if(albums != null && albums.length > 0){
				mViewFlipper.setDisplayedChild(1);
				ImageAdapter albumsAdapter = new ImageAdapter(HomeActivity.this);
				albumsAdapter.setList(albums);
				mGallery.setAdapter(albumsAdapter);
				mGallery.setOnItemClickListener(mGalleryListener);
				mGallery.setSelection(albums.length/2, true); // animate to center

			} else {
				mViewFlipper.setDisplayedChild(2);
				mFailureBar.setOnRetryListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						new NewsTask().execute((Void)null);
					}

				});
				mFailureBar.setText(R.string.connection_fail);
			}
System.out.println("onPostExecute"+mViewFlipper.getDisplayedChild());
			super.onPostExecute(albums);
		}

 

这个类实现了我们的操作。

 在oncreate()中使用

new NewsTask().execute((Void)null);

 

调用异步操作。

接下来,我们只讲解子类1,即加载成功后的显示效果。

如图:



 在以上代码中,if(albums != null && albums.length > 0){
mViewFlipper.setDisplayedChild(1);//设置显示第一面即成功加载后的效果
ImageAdapter albumsAdapter = new ImageAdapter(HomeActivity.this);//自定义了一个adpter,来组织显示样式
    albumsAdapter.setList(albums);//将数据放入其中,那么albums 自然是个bean了。里面定义了albums 的各种属性,如发行时间,专辑名等。
    mGallery.setAdapter(albumsAdapter);//给画布设置适配器,使画布显示出效果来。
mGallery.setOnItemClickListener(mGalleryListener);//添加监听,实现对指定专辑的使用
    mGallery.setSelection(albums.length/2, true);} //设置默认显示区域

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值