真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Android通過ViewHolder優(yōu)化適配器的實現(xiàn)方法(必看)

Adapter類的定義:

讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:域名注冊、虛擬空間、營銷軟件、網(wǎng)站建設(shè)、新賓網(wǎng)站維護、網(wǎng)站推廣。

Adapter對象是AdapterView和底層數(shù)據(jù)見的橋梁。Adapter用于訪問數(shù)據(jù)項,并且負責為數(shù)據(jù)項生成視圖

AdapterView是一個抽象類,用于那些需要通過Adapter填充自身的視圖,其常見子類是ListView。顯示AdapterView時會調(diào)用Adapter的getView()方法創(chuàng)建并添加每個子條目的視圖。Adapter的getView()方法就是用來創(chuàng)建這些視圖的,Adapter并不會為每行數(shù)據(jù)都創(chuàng)建一個新視圖,而是提供了回收舊視圖的方法。運行機制簡單說就是當getView()方法被調(diào)用是,如果convertView參數(shù)不為null,就使用convertView,不用新建視圖,通過convertView.findViewById()方法獲取每個UI控件的引用,然后使用與當前項的位置綁定的數(shù)據(jù)來填充視圖

為了優(yōu)化,使用ViewHolder模式,ViewHolder是一個靜態(tài)類,可以用于保存每行的視圖以避免每次調(diào)用getView時都會調(diào)用findViewById()

Android 通過ViewHolder優(yōu)化適配器的實現(xiàn)方法(必看)

public class Hack25Activity extends ListActivity {
  private static final int MODEL_COUNT = 30;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setListAdapter(new ModelAdapter(this, 0, buildModels()));
  }

  private List buildModels() {
    final ArrayList ret = new ArrayList(MODEL_COUNT);
    for (int i = 0; i < MODEL_COUNT; i++) {
      final Model model = new Model();
      model.setImage(R.mipmap.ic_launcher);
      model.setText1("Name " + i);
      model.setText2("Description " + i);
      ret.add(model);
    }
    return ret;
  }
}

Model.java

public class Model {

 private String mText1;
 private String mText2;
 private int mImageResId;

 public String getText1() {
  return mText1;
 }

 public void setText1(String text1) {
  mText1 = text1;
 }

 public String getText2() {
  return mText2;
 }

 public void setText2(String text2) {
  mText2 = text2;
 }

 public int getImage() {
  return mImageResId;
 }

 public void setImage(int imageResId) {
  mImageResId = imageResId;
 }
}

ModelAdapter.java

public class ModelAdapter extends ArrayAdapter {

 private LayoutInflater mInflater;

 public ModelAdapter(Context context, int textViewResourceId,
   List objects) {
  super(context, textViewResourceId, objects);
  mInflater = LayoutInflater.from(context);
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  final ViewHolder viewHolder;

  if (convertView == null) {
   convertView = mInflater.inflate(R.layout.row_layout, parent,
     false);

   viewHolder = new ViewHolder();
   viewHolder.imageView = (ImageView) convertView
     .findViewById(R.id.image);
   viewHolder.text1 = (TextView) convertView
     .findViewById(R.id.text1);
   viewHolder.text2 = (TextView) convertView
     .findViewById(R.id.text2);

   convertView.setTag(viewHolder);

  } else {
   viewHolder = (ViewHolder) convertView.getTag();
  }

  Model model = getItem(position);
  viewHolder.imageView.setImageResource(model.getImage());
  viewHolder.text1.setText(model.getText1());
  viewHolder.text2.setText(model.getText2());

  return convertView;
 }

 private static class ViewHolder {
  public ImageView imageView;
  public TextView text1;
  public TextView text2;
 }
}

row_layout.xml

<?xml version="1.0" encoding="utf-8"?>



  

  

    

    
  


以上代碼就基本實現(xiàn)了功能

不過每次都自定義ViewHolder比較繁瑣,在網(wǎng)上找了一個工具類,共享一下

ViewHolder.java

public class ViewHolder {
  public static  T get(View view, int id) {
    SparseArray viewHolder = (SparseArray) view.getTag();
    if (viewHolder == null) { 
      viewHolder = new SparseArray(); 
      view.setTag(viewHolder); 
    } 
    View childView = viewHolder.get(id); 
    if (childView == null) { 
      childView = view.findViewById(id); 
      viewHolder.put(id, childView); 
    } 
    return (T) childView; 
  } 
} 

使用方法:

  @Override 
  public View getView(int position, View convertView, ViewGroup parent) { 

    if (convertView == null) { 
      convertView = LayoutInflater.from(context) 
       .inflate(R.layout.banana_phone, parent, false); 
    } 

    ImageView bananaView = ViewHolder.get(convertView, R.id.banana); 
    TextView phoneView = ViewHolder.get(convertView, R.id.phone); 

    BananaPhone bananaPhone = getItem(position); 
    phoneView.setText(bananaPhone.getPhone()); 
    bananaView.setImageResource(bananaPhone.getBanana()); 

    return convertView; 
  } 


以上這篇Android 通過ViewHolder優(yōu)化適配器的實現(xiàn)方法(必看)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持創(chuàng)新互聯(lián)。


本文名稱:Android通過ViewHolder優(yōu)化適配器的實現(xiàn)方法(必看)
本文地址:http://weahome.cn/article/jpejgc.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部