您当前位置:主页 > 程序开发 >
 
android学习之--网格视图(GridView)和图像切换器(ImageSwitcher)
日期:2019-01-31 编辑:admin

android学习之--网格视图(GridView)和图像切换器(ImageSwitcher)


GridView用于在界面上按行、列分布显示多个组件。GridView和ListView有共同父类:AbsListView。GridView与ListView的区别在于:ListView只在一个方向上分布,GridView在两个方向上分布。所以使用GridView时一般都指定numColumns大于1,否则该属性默认值为1,就意味着改GridView只有一列,那就变成了ListView


GridView的xml属性

vcD4KPHA+YW5kcm9pZDpzdHJ0Y2hNb2RlILXEJiMyMDU0MDvOqqO6PC9wPgo8cD4gICA8aW1nIHNyYz0="http://www.2cto.com/uploadfile/Collfiles/20140812/2014081208380875.png" encoding="UTF-8"?>
(3)MainActivity.java

package com.example.viewimage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.GridView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.AAAAAAAdapter;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity {
	
	//定义组件
	private GridView gredView = null;
	private ImageSwitcher imageSwt =null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//获取布局组件
		gredView = (GridView) findViewById(R.id.grid);
		imageSwt = (ImageSwitcher) findViewById(R.id.imageSwitch);
		//图片
		final int images[] = new int[]{
				R.drawable.name01,R.drawable.name02,R.drawable.name03,R.drawable.name04,
				R.drawable.name05,R.drawable.name06,R.drawable.name07,R.drawable.name08,
				R.drawable.name09,R.drawable.name10,R.drawable.name11,R.drawable.name12,
				R.drawable.name13,R.drawable.name14,R.drawable.name15,R.drawable.name16
		};
		//创建list集合,存储图片
		List> list = new ArrayList>();
		for(int i=0;i map = new HashMap();
			map.put("img", images[i]);
			list.add(map);
		}
		//设置图片更换效果
		//淡入效果
		imageSwt.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
		//淡出效果
		imageSwt.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
		//设置图片转换效果
		imageSwt.setFactory(new ViewFactory() {
			
			@Override
			public View makeView() {
				ImageView imageView = new ImageView(MainActivity.this);
				imageView.setLayoutParams(new ImageSwitcher.LayoutParams(300, 300));
				imageView.setScaleType(ScaleType.FIT_CENTER);
				return imageView;
			}
		});
		//设置适配器
		AAAAAAAdapter AAAAAAAdapter = new AAAAAAAdapter(this, list, R.layout.img, new String[]{"img"}, new int[]{R.id.img});
		//适配器
		gredView.setAdapter(AAAAAAAdapter);
		//选中事件监听
		gredView.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView parent, View view,
					int position, long id) {
				Log.i("onItemSelected", "onItemSelected");
				imageSwt.setImageResource(images[position]);
			}

			@Override
			public void onNothingSelected(AdapterView parent) {
				// TODO Auto-generated method stub
				
			}
		});
		//点击事件监听
		gredView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView parent, View view,
					int position, long id) {
				Log.i("onItemClick", "onItemClick");
				imageSwt.setImageResource(images[position]);
				
			}
		});
	}


}

运行效果如下:


http://www.bkjia.com/Androidjc/863358.htmlwww.bkjia.comtruehttp://www.bkjia.com/Androidjc/863358.htmlTechArticleandroid学习之--网格视图(GridView)和图像切换器(ImageSwitcher) GridView用于在界面上按行、列分布显示多个组件。GridView和ListView有共同父类:AbsL...

本文源自: AG环亚娱乐

上一篇:详解Android定位,详解android 下一篇:没有了
 
Copyright © 2005-2016 http://www.tecsquared.com AG环亚娱乐_AG直营网_环亚娱乐AG最佳线路_腾讯体育版权所有 AG环亚娱乐_AG直营网_环亚娱乐AG最佳线路_腾讯体育