对于ListView的优化从下面两个方面考虑:
1:convertView的复用 2:对封装控件的ViewHolder的使用
对于ListView这个适配器控件来说,它里面的每一子项布局Item的显示都需要Adapter调用一次getView()方法
在这个方法里面的参数convertView,这个参数可以对之前加载好的布局进行缓从,以便之后可以复用
convertView:就是指的是滚出屏幕的呢个item的view(在这之前被进行了缓从),当从屏幕的下方相应的生成一个Item的时候,就会将这个convertView从缓从里面拿出来进行复用
ViewHolder:将每一个控件的实例存放在ViewHodel里面,再将viewHolder对象存储在View中,第二次重用convertView的时候,,需要重新取出viewHolder
1 package com.example.todaylistview; 2 import android.graphics.Bitmap; 3 import android.support.v4.widget.SwipeRefreshLayout; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.Adapter; 9 import android.widget.BaseAdapter; 10 import android.widget.ImageView; 11 import android.widget.ListView; 12 import android.widget.TextView; 13 import android.widget.Toast; 14 import com.android.volley.RequestQueue; 15 import com.android.volley.Response; 16 import com.android.volley.VolleyError; 17 import com.android.volley.toolbox.ImageRequest; 18 import com.android.volley.toolbox.JsonArrayRequest; 19 import com.android.volley.toolbox.Volley; 20 import org.json.JSONArray; 21 import org.json.JSONException; 22 public class MainActivity extends AppCompatActivity { 23 private JSONArray data; 24 private ListView listView; 25 private MyAdapter myAdapter; 26 private RequestQueue requestQueue ; 27 private SwipeRefreshLayout swipeRefreshLayout; 28 protected void onCreate(Bundle savedInstanceState) { 29 super.onCreate(savedInstanceState); 30 setContentView(R.layout.activity_main); 31 requestQueue= Volley.newRequestQueue(this); 32 swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh); 33 /*设置进度条的颜色*/ 34 swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent,R.color.colorPrimary,R.color.colorPrimaryDark); 35 listView = (ListView) findViewById(R.id.listView); 36 /*加载数据*/ 37 loadData(); 38 /*为下拉刷新添加监听器*/ 39 swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 40 public void onRefresh() { 41 /*下拉刷新执行的操作*/ 42 flushData(); 43 /*取消下拉的进度条*/ 44 swipeRefreshLayout.setRefreshing(false); 45 } 46 }); 47 } 48 /*1:首次加载数据*/ 49 public void loadData(){ 50 /*1:数据来源于数据库*/ 51 String url="http://10.2.43.96/android/queryServlet"; 52 JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() { 53 /*执行成功是的响应结果*/ 54 public void onResponse(JSONArray jsonArray) { 55 data = jsonArray; 56 myAdapter=new MyAdapter(); 57 listView.setAdapter(myAdapter); 58 } 59 }, new Response.ErrorListener() { 60 /*执行失败*/ 61 public void onErrorResponse(VolleyError volleyError) { 62 Toast.makeText(getApplicationContext(), "网络超时", Toast.LENGTH_SHORT).show(); 63 } 64 }); 65 requestQueue.add(jsonArrayRequest); 66 } 67 /*刷新数据*/ 68 public void flushData(){ 69 String url="http://10.2.43.96/android/queryServlet"; 70 JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() { 71 public void onResponse(JSONArray jsonArray) { 72 data = jsonArray; 73 /**通知适配器控件更新视图*/ 74 myAdapter.notifyDataSetChanged(); 75 } 76 }, new Response.ErrorListener() { 77 public void onErrorResponse(VolleyError volleyError) { 78 Toast.makeText(getApplication(),"网络异常",Toast.LENGTH_SHORT).show(); 79 } 80 }); 81 requestQueue.add(jsonArrayRequest); 82 } 83 84 /*创建适配jsonArray类型数据的适配器*/ 85 class MyAdapter extends BaseAdapter{ 86 /*1:显示数据的条数*/ 87 public int getCount() { 88 return data.length(); 89 } 90 public Object getItem(int position) { 91 return null; 92 } 93 public long getItemId(int position) { 94 return position; 95 } 96 /*2:ListView中每一个item显示都需要Adapter调用getView方法*/ 97 public View getView(int position, View convertView, ViewGroup parent) { 98 View view = convertView; 99 ViewHolder viewHolder = null; 100 if(view==null){ 101 /* 1:使用布局加载器为子项加载我们传入的布局*/ 102 view = getLayoutInflater().inflate(R.layout.my_layout,parent,false); 103 /*2:使用view获取布局控件*/ 104 viewHolder = new ViewHolder(); 105 viewHolder.setImageView((ImageView) view.findViewById(R.id.logo)); 106 viewHolder.setTextView((TextView) view.findViewById(R.id.name)); 107 /*3:将viewHolder加入到View中*/ 108 view.setTag(viewHolder); 109 } 110 /*重新获取viewHolder*/ 111 viewHolder = (ViewHolder) view.getTag(); 112 try { 113 /*1:设置用户名*/ 114 viewHolder.getTextView().setText(data.getJSONObject(position).getString("name")); 115 /*获取图片的名称*/ 116 String path= data.getJSONObject(position).getString("image"); 117 /*下载图片*/ 118 LoadImage(viewHolder.getImageView(), path); 119 } catch (JSONException e) { 120 e.printStackTrace(); 121 } 122 return view; 123 } 124 } 125 /*创建封装控件的ViewHolder类 减少findViewById的次数*/ 126 class ViewHolder{ 127 private ImageView imageView; 128 private TextView textView; 129 public ImageView getImageView() { 130 return imageView; 131 } 132 public void setImageView(ImageView imageView) { 133 this.imageView = imageView; 134 } 135 public TextView getTextView() { 136 return textView; 137 } 138 public void setTextView(TextView textView) { 139 this.textView = textView; 140 } 141 } 142 /*下载图片的方法*/ 143 public void LoadImage( final ImageView imageView,String path){ 144 /*从服务器中获取图片数据*/ 145 String url = "http://10.2.43.96/android/image/";//访问服务器的路径 146 ImageRequest imageRequest = new ImageRequest(url+path, new Response.Listener<Bitmap>() { 147 public void onResponse(Bitmap bitmap) { 148 if(bitmap!=null){ 149 imageView.setImageBitmap(bitmap); 150 } 151 } 152 }, 0, 0, null, new Response.ErrorListener() { 153 public void onErrorResponse(VolleyError volleyError) { 154 155 } 156 }); 157 requestQueue.add(imageRequest); 158 } 159 }