package com.zyeq.cxsw.listview_3; import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MainActivity extends AppCompatActivity { private int[] ids={R.drawable.img_1,R.drawable.img_2,R.drawable.img_3,R.drawable.img_4,R.drawable.img_5,R.drawable.img_6,R.drawable.img_7,R.drawable.img_8,R.drawable.img_9,R.drawable.img_10}; private ListView listView; private List<Map<String, String>> data = new ArrayList<>(); private int temp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) this.findViewById(R.id.listView); /*加载数据*/ data=loadData(); /*创建适配器*/ MyAdapter adapter=new MyAdapter(); /*设置适配器*/ listView.setAdapter(adapter); } /*使用BasicAdapter*/ class MyAdapter extends BaseAdapter { /*返回数据的条数*/ @Override public int getCount() { return data.size(); } /*返回一个对象*/ @Override public Object getItem(int position) { return null; } /*返回代表当前item项的id*/ @Override public long getItemId(int position) { return position; } /**/ @Override public View getView(int position, View convertView, ViewGroup parent) { temp=position; /*将布局转化为一个View对象*/ /*1.获取布局加载器*/ /*ListView的优化操作:1.复用布局 2.减少findViewById的次数 * */ View view=convertView; ViewHolder viewHolder; if(view==null){ view = getLayoutInflater().inflate(R.layout.list_item, parent, false); /*从布局文件中获取UI控件*/ viewHolder=new ViewHolder(); TextView textView= (TextView) view.findViewById(R.id.name); ImageView imageView= (ImageView) view.findViewById(R.id.logo); viewHolder.setImageView(imageView); viewHolder.setTextView(textView); view.setTag(viewHolder); } viewHolder= (ViewHolder) view.getTag(); viewHolder.getTextView().setText(data.get(position).get("name")); /*为图片设置监听器*/ viewHolder.getImageView().setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(), "id:" + data.get(temp).get("logo"), Toast.LENGTH_SHORT).show(); } }); /*设置字体的颜色*/ if(position==9){ viewHolder.getTextView().setTextColor(Color.RED); } viewHolder.getImageView().setImageResource(Integer.parseInt(data.get(position).get("logo"))); return view; } /*封装控件*/ class ViewHolder{ private TextView textView; private ImageView imageView; public TextView getTextView() { return textView; } public void setTextView(TextView textView) { this.textView = textView; } public ImageView getImageView() { return imageView; } public void setImageView(ImageView imageView) { this.imageView = imageView; } } } /*需要的数据源*/ public List<Map<String, String>> loadData() { List<Map<String, String>> list = new ArrayList<>(); Map<String, String> map = null; for (int i = 1; i <= 1000; i++) { map=new HashMap<>(); map.put("name", "user" + i); map.put("logo",""+ids[i%10]); list.add(map); } return list; } }