2.4.11 网格视图和图像切换器
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <GridView android:id="@+id/gridView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:numColumns="3" android:gravity="center"> </GridView> <ImageSwitcher android:id="@+id/imageSwitcher1" android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="center_horizontal"> </ImageSwitcher> </LinearLayout>
package com.moon.hello2; import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; import java.util.List; import java.util.Map; import android.R.anim; import android.R.integer; import android.app.Activity; import android.database.DataSetObserver; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Gravity; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Button; import android.widget.DatePicker; import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.GridView; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.RatingBar; import android.widget.SimpleAdapter; import android.widget.RatingBar.OnRatingBarChangeListener; import android.widget.SeekBar; import android.widget.DatePicker.OnDateChangedListener; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TabHost; import android.widget.TextView; import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener; import android.widget.ViewSwitcher.ViewFactory; public class MainActivity extends Activity { private final int[] imgs = new int[]{R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5,R.drawable.a6,R.drawable.a7,R.drawable.a8}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_main); GridView gridView = (GridView)findViewById(R.id.gridView1); List<Map<String, Object>> data = new ArrayList<Map<String,Object>>(); for (int imgId : this.imgs) { Map<String,Object> newMap = new HashMap<String, Object>(); newMap.put("img", imgId); data.add(newMap); } SimpleAdapter simpleAdapter = new SimpleAdapter(this, data, R.layout.cell, new String[]{"img"}, new int[]{R.id.imgView}); gridView.setAdapter(simpleAdapter); final ImageSwitcher imageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher1); imageSwitcher.setInAnimation(this, android.R.anim.fade_in); imageSwitcher.setOutAnimation(this, android.R.anim.fade_out); imageSwitcher.setFactory(new ViewFactory() { @Override public View makeView() { // TODO Auto-generated method stub ImageView imageView = new ImageView(MainActivity.this); imageView.setBackgroundColor(0xff0000); imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); return imageView; } }); gridView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub imageSwitcher.setImageResource(MainActivity.this.imgs[position%imgs.length]); } }); gridView.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long arg3) { // TODO Auto-generated method stub imageSwitcher.setImageResource(MainActivity.this.imgs[position%imgs.length]); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
本篇文章首发自魔芋红茶的博客https://www.cnblogs.com/Moon-Face/
请尊重其他人的劳动成功,转载请注明。