189 8069 5689

怎么在Android应用中利用ViewHolder优化Adapter

怎么在Android应用中利用ViewHolder优化Adapter?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

创新互联公司致力于互联网网站建设与网站营销,提供网站制作、成都做网站、网站开发、seo优化、网站排名、互联网营销、小程序定制开发、公众号商城、等建站开发,创新互联公司网站建设策划专家,为不同类型的客户提供良好的互联网应用定制解决方案,帮助客户在新的全球化互联网环境中保持优势。

具体方法如下:

public class MarkerItemAdapter extends BaseAdapter
{
  private Context mContext = null;
  private List mMarkerData = null;

  public MarkerItemAdapter(Context context, List markerItems)
  {
    mContext = context;
    mMarkerData = markerItems;
  }

  public void setMarkerData(List markerItems)
  {
    mMarkerData = markerItems;
  }

  @Override
  public int getCount()
  {
    int count = 0;
    if (null != mMarkerData)
    {
      count = mMarkerData.size();
    }
    return count;
  }

  @Override
  public MarkerItem getItem(int position)
  {
    MarkerItem item = null;

    if (null != mMarkerData)
    {
      item = mMarkerData.get(position);
    }

    return item;
  }

  @Override
  public long getItemId(int position)
  {
    return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent)
  {
    ViewHolder viewHolder = null;
    if (null == convertView)
    {
      viewHolder = new ViewHolder();
      LayoutInflater mInflater = LayoutInflater.from(mContext);
      convertView = mInflater.inflate(R.layout.item_marker_item, null);

      viewHolder.name = (TextView) convertView.findViewById(R.id.name);
      viewHolder.description = (TextView) convertView
          .findViewById(R.id.description);
      viewHolder.createTime = (TextView) convertView
          .findViewById(R.id.createTime);

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

    // set item values to the viewHolder:

    MarkerItem markerItem = getItem(position);
    if (null != markerItem)
    {
      viewHolder.name.setText(markerItem.getName());
      viewHolder.description.setText(markerItem.getDescription());
      viewHolder.createTime.setText(markerItem.getCreateDate());
    }

    return convertView;
  }

  private static class ViewHolder
  {
    TextView name;
    TextView description;
    TextView createTime;
  }

}

其中MarkerItem是自定义的类,其中包含name,description,createTime等字段,并且有相应的get和set方法。

ViewHolder是一个内部类,其中包含了单个项目布局中的各个控件。

单个项目的布局,即R.layout.item_marker_item如下:

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


  

  

  

官方的API Demos中也有这个例子:

package com.example.android.apis.view中的List14:

/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.apis.view;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.ImageView;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;
import com.example.android.apis.R;

/**
 * Demonstrates how to write an efficient list adapter. The adapter used in this example binds
 * to an ImageView and to a TextView for each row in the list.
 *
 * To work efficiently the adapter implemented here uses two techniques:
 * - It reuses the convertView passed to getView() to avoid inflating View when it is not necessary
 * - It uses the ViewHolder pattern to avoid calling findViewById() when it is not necessary
 *
 * The ViewHolder pattern consists in storing a data structure in the tag of the view returned by
 * getView(). This data structures contains references to the views we want to bind data to, thus
 * avoiding calls to findViewById() every time getView() is invoked.
 */
public class List14 extends ListActivity {

  private static class EfficientAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Bitmap mIcon1;
    private Bitmap mIcon2;

    public EfficientAdapter(Context context) {
      // Cache the LayoutInflate to avoid asking for a new one each time.
      mInflater = LayoutInflater.from(context);

      // Icons bound to the rows.
      mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_1);
      mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_2);
    }

    /**
     * The number of items in the list is determined by the number of speeches
     * in our array.
     *
     * @see android.widget.ListAdapter#getCount()
     */
    public int getCount() {
      return DATA.length;
    }

    /**
     * Since the data comes from an array, just returning the index is
     * sufficent to get at the data. If we were using a more complex data
     * structure, we would return whatever object represents one row in the
     * list.
     *
     * @see android.widget.ListAdapter#getItem(int)
     */
    public Object getItem(int position) {
      return position;
    }

    /**
     * Use the array index as a unique id.
     *
     * @see android.widget.ListAdapter#getItemId(int)
     */
    public long getItemId(int position) {
      return position;
    }

    /**
     * Make a view to hold each row.
     *
     * @see android.widget.ListAdapter#getView(int, android.view.View,
     *   android.view.ViewGroup)
     */
    public View getView(int position, View convertView, ViewGroup parent) {
      // A ViewHolder keeps references to children views to avoid unneccessary calls
      // to findViewById() on each row.
      ViewHolder holder;

      // When convertView is not null, we can reuse it directly, there is no need
      // to reinflate it. We only inflate a new View when the convertView supplied
      // by ListView is null.
      if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item_icon_text, null);

        // Creates a ViewHolder and store references to the two children views
        // we want to bind data to.
        holder = new ViewHolder();
        holder.text = (TextView) convertView.findViewById(R.id.text);
        holder.icon = (ImageView) convertView.findViewById(R.id.icon);

        convertView.setTag(holder);
      } else {
        // Get the ViewHolder back to get fast access to the TextView
        // and the ImageView.
        holder = (ViewHolder) convertView.getTag();
      }

      // Bind the data efficiently with the holder.
      holder.text.setText(DATA[position]);
      holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);

      return convertView;
    }

    static class ViewHolder {
      TextView text;
      ImageView icon;
    }
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setListAdapter(new EfficientAdapter(this));
  }

  private static final String[] DATA = Cheeses.sCheeseStrings;
}

其中布局:

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




  

  

看完上述内容,你们掌握怎么在Android应用中利用ViewHolder优化Adapter的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


文章名称:怎么在Android应用中利用ViewHolder优化Adapter
网址分享:http://cdxtjz.com/article/iijhsi.html

其他资讯