ListView是自己手动优化(优化:将滑出的item调到滑入复用,改变其中数据显示,这样永远只是创建手机屏幕容纳item加一个视图)
ListView 优化的优点:1.提升了列表的加载速度
ListView 优化的优点:1.提升了列表的加载速度
2.降低系统内存消耗
下面代码如下
xml代码
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.g160828_android08_listview.MainActivity"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/lv_main_list" ></ListView> </android.support.constraint.ConstraintLayout>
Java代码
package com.example.g160828_android08_listview; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ImageView; import android.widget.ListView; import android.widget.SimpleAdapter; 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 String titles[] = {"鸟", "猫", "鸡", "牛", "狗", "龙", "鸭", "象", "鱼", "狐狸"}; private int images[] = {R.drawable.bird, R.drawable.cat, R.drawable.chicken, R.drawable.cow, R.drawable.dog, R.drawable.dragon, R.drawable.duck, R.drawable.elephant, R.drawable.fish, R.drawable.fox}; private ListView lv_main_list; private Button btn_item_listview_download; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //拿到控件 lv_main_list = (ListView) findViewById(R.id.lv_main_list); btn_item_listview_download = (Button) findViewById(R.id.btn_item_listview_download); // List<Map<String,Object>> list=new ArrayList<>(); // for (int i = 0; i < titles.length; i++) { // Map<String,Object>map=new HashMap<>(); // map.put("title",titles[i]); // map.put("image",images[i]); // list.add(map); // } //适配器 // SimpleAdapter adapter=new SimpleAdapter(this,list,R.layout.item_listview,new String[]{"title","image"},new int[]{ R.id.tv_item_listview_title,R.id.iv_item_listview_ico}); //设置适配器 lv_main_list.setAdapter(new MyAdapter()); // lv_main_list 的点击事件 lv_main_list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Toast.makeText(MainActivity.this, "点击了" + titles[i % titles.length], Toast.LENGTH_SHORT).show(); } }); } class MyAdapter extends BaseAdapter { @Override //得到 public int getCount() { return titles.length; } @Override public Object getItem(int i) { return titles[i]; } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { if (view == null) { //xml文件装成view view = getLayoutInflater().inflate(R.layout.item_listview, null); ItemTag itemTag = new ItemTag(); //获取控件(必须使用v.findview) itemTag.imageView = view.findViewById(R.id.iv_item_listview_ico); itemTag.textView = view.findViewById(R.id.tv_item_listview_title); itemTag.button = view.findViewById(R.id.btn_item_listview_download); view.setTag(itemTag); } //获取控件(必须使用v.findview) ImageView iv_item_listview_ico = view.findViewById(R.id.iv_item_listview_ico); TextView tv_item_listview_title = view.findViewById(R.id.tv_item_listview_title); Button btn_item_listview_download = view.findViewById(R.id.btn_item_listview_download); ItemTag it= (ItemTag) view.getTag(); //给控件设置 it.imageView.setImageResource(images[i % titles.length]); it.textView.setText(titles[i % titles.length]); return view; } } }