您好,登錄后才能下訂單哦!
package com.example.testgallary; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements OnItemClickListener{ private Gallery mGallery; private MyAdapter adapter; private List<Map<String, Object>> data = null; private List<Map<String, Object>> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mGallery = (Gallery) findViewById(R.id.gallery1); data = getData();// 獲取數據 adapter = new MyAdapter(this); mGallery.setAdapter(adapter); mGallery.setOnItemClickListener(this); } private List<Map<String, Object>> getData(){ list = new ArrayList<Map<String, Object>>(); Map<String, Object> map; for(int i=0; i<str.length; i++){ map = new HashMap<String, Object>(); map.put("title", str[i]); list.add(map); } return list; } // ViewHolder靜態類 static class ViewHolder { public TextView title; } public class MyAdapter extends BaseAdapter { private LayoutInflater mInflater = null; private MyAdapter(Context context) { // 根據context上下文加載布局 this.mInflater = LayoutInflater.from(context); } @Override public int getCount() { // 在此適配器中所代表的數據集中的條目數 return data.size(); } @Override public Object getItem(int position) { // 獲取數據集中與指定索引對應的數據項 return position; } @Override public long getItemId(int position) { // 獲取在列表中與指定索引對應的行id return position; } // 獲取一個在數據集中指定索引的視圖來顯示數據 @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; // 如果緩存convertView為空,則需要創建View if (convertView == null) { holder = new ViewHolder(); // 根據自定義的Item布局加載布局 convertView = mInflater.inflate(R.layout.list_item, null); holder.title = (TextView) convertView.findViewById(R.id.title); holder.title.setTextSize(32); // 將設置好的布局保存到緩存中,并將其設置在Tag里,以便后面方便取出Tag convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.title.setText((String) data.get(position).get("title")); holder.title.setTextColor(Color.BLACK);// return convertView; } } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(MainActivity.this, data.get(arg2).get("title").toString(), Toast.LENGTH_SHORT).show(); } String[] str = {"李","王","孫","杜","趙","吳","陸","楊","侯","屈","宋","周","張","陳","馮", "朱","蔣","沈","韓","秦","許","何","呂","孔","諸葛","夏侯"}; }
//activity_main layout布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <Gallery android:id="@+id/gallery1" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>
//list_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20sp" android:layout_marginRight="20sp" android:text="" /> </LinearLayout>
http://670176656.blog.51cto.com/addblog.php
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。