初识ViewPager【Android】

viewpager可以实现gallery的图片展示效果
java代码:

package com.succ7.viewpagertest;

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

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private ViewPager viewpager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		viewpager = (ViewPager) findViewById(R.id.viewpager);

		/*List<ImageView> lists = new ArrayList<ImageView>();
		ImageView imageView1 = new ImageView(this);
		imageView1.setBackgroundResource(R.drawable.g1);

		ImageView imageView2 = new ImageView(this);
		imageView2.setBackgroundResource(R.drawable.g2);

		ImageView imageView3 = new ImageView(this);
		imageView3.setBackgroundResource(R.drawable.g3);

		ImageView imageView4 = new ImageView(this);
		imageView4.setBackgroundResource(R.drawable.g4);*/
		/*********************换成textview可以吗******************************/
		List<TextView> lists = new ArrayList<TextView>();
		TextView imageView1 = new TextView(this);
		imageView1.setBackgroundResource(R.drawable.g1);

		TextView imageView2 = new TextView(this);
		imageView2.setBackgroundResource(R.drawable.g2);

		TextView imageView3 = new TextView(this);
		imageView3.setBackgroundResource(R.drawable.g3);

		TextView imageView4 = new TextView(this);
		imageView4.setBackgroundResource(R.drawable.g4);

		lists.add(imageView1);
		lists.add(imageView2);
		lists.add(imageView3);
		lists.add(imageView4);

		ViewPagerAdapter adapter = new ViewPagerAdapter(lists);
		viewpager.setAdapter(adapter);
		viewpager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				System.out.println("onPageSelected");
				Toast.makeText(MainActivity.this, arg0+"", 0).show();
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				System.out.println("onPageScrolled");
				Toast.makeText(MainActivity.this, arg0+"|"+arg1+"|"+arg2, 0).show();
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				System.out.println("onPageScrollStateChanged");
				Toast.makeText(MainActivity.this, arg0+"", 0).show();
			}
		});

	}

	class ViewPagerAdapter extends PagerAdapter {
		private List<TextView> lists;

		public ViewPagerAdapter(List<TextView> lists) {
			this.lists = lists;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			((ViewPager) container).removeView(lists.get(position));
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			((ViewPager) container).addView(lists.get(position));
			return lists.get(position);
		}

		@Override
		public int getCount() {
			return lists.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

	}

}

Xml布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>


使用
android.support.v4.view.ViewPager;

用ImageView 和 TextView都是可以的,只要设置背景图片


imageview 的
setBackgroundResource(resId)设置的是imageview的大小
setImageResource(resId)设置的是图片的实际的大小,相当于
布局文件中的 android:src = "resid"
注意:图片的大小未必是imageview的大小



布局文件:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值