[Android]BaseAdapter-Gallery循环@List/Grid/SpinnerView改进

 

1. MainActivity.java

 package home.lee.listviewadapterdemo; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Gallery; import android.widget.GridView; import android.widget.ImageView; import android.widget.ListView; import android.widget.Spinner; public class MainActivity extends Activity { protected static final String TAG = "MainAc"; //声明UI组件和资源变量等 private Gallery gallery; private MyBaseAdapter adapter; private ImageView imageView; protected int[] imgs = {android.R.drawable.alert_dark_frame, android.R.drawable.alert_light_frame, android.R.drawable.arrow_down_float, android.R.drawable.arrow_up_float, android.R.drawable.bottom_bar, android.R.drawable.btn_default, android.R.drawable.button_onoff_indicator_on, android.R.drawable.ic_dialog_dialer, android.R.drawable.editbox_background_normal, android.R.drawable.ic_menu_crop}; private ListView lv2; private MyListViewAdapter listViewAdapter; private GridView gv2; private Spinner sp2;; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //基于BaseAdapter setContentView(R.layout.gallery); setGalleryView(); //基于ListView、GridView及Spinner和BaseAdapter实现 // setContentView(R.layout.main); // setLGSView(); } private void setLGSView() { lv2 = (ListView)findViewById(R.id.listview2); listViewAdapter = new MyListViewAdapter(this); //lv2.setSelector(android.R.drawable.ic_menu_my_calendar); 点击的时候,背景图片变换 //也可以在xml中设置listSelector属性指定相应图片资源 lv2.setFastScrollEnabled(true);//列表快速滑块功能,当快速滑动时,在右侧会显示小方块,当停止滑动1秒左右,滑块会消失 //也可在xml中,设置fastScrollEnabled(true) lv2.setAdapter(listViewAdapter); gv2 = (GridView)findViewById(R.id.gridview2); gv2.setAdapter(listViewAdapter); gv2.setNumColumns(3); sp2 = (Spinner)findViewById(R.id.spinner2); sp2.setAdapter(listViewAdapter); } private void setGalleryView() { //初始化UI组件 imageView = (ImageView)findViewById(R.id.imageView1); gallery = (Gallery)findViewById(R.id.gallery1); adapter = new MyBaseAdapter(this); gallery.setAdapter(adapter); gallery.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { int newArg2 = arg2 % 10; Log.i(TAG, "onItemClick" + "arg2->" + arg2 + "arg3->" + arg3 + "newArg2->" + newArg2); imageView.setImageResource(imgs[newArg2]); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, 1, 2, R.string.exit); menu.add(0, 2, 1, R.string.about); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()){ case 1: finish(); break; case 2: //Activity about content break; default: break; } return super.onOptionsItemSelected(item); } }

 

2. MyBaseAdapter.java

 package home.lee.listviewadapterdemo; import android.content.Context; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; public class MyBaseAdapter extends BaseAdapter { //自定义图片Adapter以外部类形式存在于MainActivity中,也可以内部类形式以便访问MainActivity中的各个变量,特别是imgs数组 private static final String TAG = "Ray BaseAdapter"; private Context context; private ImageView imageView; private int[] imgs = {android.R.drawable.alert_dark_frame, android.R.drawable.alert_light_frame, android.R.drawable.arrow_down_float, android.R.drawable.arrow_up_float, android.R.drawable.bottom_bar, android.R.drawable.btn_default, android.R.drawable.button_onoff_indicator_on, android.R.drawable.ic_dialog_dialer, android.R.drawable.editbox_background_normal, android.R.drawable.ic_menu_crop}; public MyBaseAdapter(Context context) { super(); this.context = context; } @Override public int getCount() { Log.i(TAG, "MAX_VALUE--->" + Integer.MAX_VALUE); return Integer.MAX_VALUE;//最大整形数,实现图库的循环 // return imgs.length; } @Override public Object getItem(int arg0) { Log.i(TAG, "getItem--->" + arg0); return arg0; } @Override public long getItemId(int position) { int newPosition = position % 10; Log.i(TAG, "newPosition--->" + newPosition); return newPosition; } @Override public View getView(int position, View convertView, ViewGroup parent) { //针对每一个数据(即每一个图片ID)创建一个ImageView实例, imageView = new ImageView(context);//针对外面传递过来的Context变量 int newPosition = position % 10; imageView.setImageResource(imgs[newPosition]);//根据position获得图片资源 Log.i(TAG, "" + String.valueOf(imgs[newPosition])); imageView.setLayoutParams(new Gallery.LayoutParams(80, 80));//设置Gallery中每幅图的大小 imageView.setScaleType(ImageView.ScaleType.FIT_XY); return imageView; } }

 

3. MyListViewAdapter.java

 package home.lee.listviewadapterdemo; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; public class MyListViewAdapter extends BaseAdapter { private Context context;//为myInflater而采用context private LayoutInflater myInflater; public MyListViewAdapter(Context context) { super(); this.context = context; } @Override public int getCount() {//用来说明需要绘制的条目数 return 100;//数组的长度,即个数,可以根据实际调整 } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) {//用来说明需要绘制的条目数个条目就调用一次这个方法 ViewHolder holder = new ViewHolder(); if (convertView == null) { /** * 这个方法getView(),是用来逐一绘制每一条item * * setTag()会把View与ViewHolder绑定,形成一一对应的关系,拖动listview的时候会重新绘制每一条item * 但是那些已经取得绑定的View,会调用getTag()方法,就不会重新绘制,而是拿到内存中已经取得的资源,提高了效率 * * @param position position就是位置从0开始 * @param convertView convertView是Spinner,ListView中每一项要显示的view * @param parent parent就是父窗体了,也就是Spinner,ListView,GridView了 * @return 通常return 的view也就是convertView * * 在程序中动态加载以上布局: * LayoutInflater flater = LayoutInflater.from(this); * View view = flater.inflate(R.layout.example, null); */ myInflater = LayoutInflater.from(context); convertView = myInflater.inflate(R.layout.item_list, null); holder.icon = (ImageView)convertView.findViewById(R.id.itemimageview2); holder.text = (TextView)convertView.findViewById(R.id.itemtextview2); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } if (position <= 1) { holder.icon.setImageResource(android.R.drawable.btn_dropdown); holder.text.setText("你好"); } else if (position <= 3 && position >= 2) { holder.icon.setImageResource(android.R.drawable.edit_text); holder.text.setText("Hello"); } else { holder.icon.setImageResource(android.R.drawable.spinner_dropdown_background); holder.text.setText("안녕하세요"); } return convertView; } private class ViewHolder { TextView text; ImageView icon; } }

运行结果:
1. 可以实现循环图库;
2. 可以实现ListView,GridView,SpinnerView的简单显示以及个别属性的引用方法。

posted @ 2014-06-06 08:58  It's_Lee  阅读(217)  评论(0编辑  收藏  举报