Gallery的使用



xml布局

1、整体activity_main.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
	<Gallery 
	    android:id="@+id/gallery"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:spacing="5dp"
	    />
	<ImageView 
	    android:id="@+id/large_pic"
	    android:layout_width="200dp"
	    android:layout_height="200dp"
	    android:layout_centerHorizontal="true"
	    android:layout_alignParentBottom="true"/>

</RelativeLayout>

2、item布局gallery_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <ImageView 
        android:id="@+id/pic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

3、在drawable添加图片

tx1.jpg


tx2.jpg


tx3.jpg


tx4.jpg


tx5.jpg

4、java代码

MyView.java

package com.example.assembly;

import android.view.View;
import android.widget.ImageView;

public class MyView {
	ImageView iv;
	public MyView(View v) {
		iv = (ImageView) v.findViewById(R.id.pic);
	}

}

MyAdapter.java

package com.example.assembly;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class MyAdapter extends BaseAdapter {
	private String[] name;
	private int[] pic;
	private Context context;

	public MyAdapter(String[] name, int[] pic, Context context) {
		super();
		this.name = name;
		this.pic = pic;
		this.context = context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return name.length;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return name[position];
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		MyView myView;
		if (convertView == null) {
			// 加载对象
			LayoutInflater layoutInflater = LayoutInflater.from(context);
			// 实例化加载对象item的view对象
			convertView = layoutInflater.inflate(R.layout.gallery_item, parent, false);
			// 实例化内部静态类
			myView = new MyView(convertView);
			// 设值
			convertView.setTag(myView);
		} else {
			myView = (MyView) convertView.getTag();
		}
		// 传值
		myView.iv.setImageResource(pic[position]);
		return convertView;
	}
}

MainActivity.java

package com.example.assembly;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.ImageView;

public class MainActivity extends Activity {
	private String[] name = { "工藤新一", "毛利兰", "毛利小五郎", "怪盗基德", "灰原哀" };
	private int[] id = { R.drawable.tx1, R.drawable.tx2, R.drawable.tx3, R.drawable.tx4, R.drawable.tx5 };
	private Gallery gallery;
	private ImageView iv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		gallery = (Gallery) findViewById(R.id.gallery);
		iv = (ImageView) findViewById(R.id.large_pic);
		MyAdapter simple=new MyAdapter(name, id, MainActivity.this);
		gallery.setAdapter(simple);
		gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				iv.setImageResource(id[arg2]);

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
	}
}

效果图:





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值