您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。