最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android中利用ViewHolder優(yōu)化自定義Adapter的寫法(必看)

 更新時間:2017年04月18日 10:07:11   投稿:jingxian  
下面小編就為大家?guī)硪黄狝ndroid中利用ViewHolder優(yōu)化自定義Adapter的寫法(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

最近寫Adapter寫得多了,慢慢就熟悉了。

用ViewHolder,主要是進(jìn)行一些性能優(yōu)化,減少一些不必要的重復(fù)操作。(WXD同學(xué)教我的。)

具體不分析了,直接上一份代碼吧:

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

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

  public void setMarkerData(List<MarkerItem> 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等字段,并且有相應(yīng)的get和set方法。

ViewHolder是一個內(nèi)部類,其中包含了單個項目布局中的各個控件。

單個項目的布局,即R.layout.item_marker_item如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" 
  android:padding="5dp">

  <TextView
    android:id="@+id/name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Name"
    android:textSize="20sp"
    android:textStyle="bold" />

  <TextView
    android:id="@+id/description"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Description"
    android:textSize="18sp" />

  <TextView
    android:id="@+id/createTime"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="CreateTime"
    android:textSize="16sp" />

</LinearLayout>

官方的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"?>
<!-- Copyright (C) 2007 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.
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <ImageView android:id="@+id/icon"
    android:layout_width="48dip"
    android:layout_height="48dip" />

  <TextView android:id="@+id/text"
    android:layout_gravity="center_vertical"
    android:layout_width="0dip"
    android:layout_weight="1.0"
    android:layout_height="wrap_content" />

</LinearLayout>

以上這篇Android中利用ViewHolder優(yōu)化自定義Adapter的寫法(必看)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android實現(xiàn)定制返回按鈕動畫效果的方法

    Android實現(xiàn)定制返回按鈕動畫效果的方法

    這篇文章主要介紹了Android實現(xiàn)定制返回按鈕動畫效果的方法,涉及Android控件及動畫的相關(guān)操作技巧,需要的朋友可以參考下
    2016-02-02
  • Android實現(xiàn)倒計時效果

    Android實現(xiàn)倒計時效果

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)倒計時效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • Android入門計算器編寫代碼

    Android入門計算器編寫代碼

    這篇文章主要為大家詳細(xì)介紹了Android入門計算器編寫代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • Android?自定義來電秀實現(xiàn)總結(jié)

    Android?自定義來電秀實現(xiàn)總結(jié)

    這篇文章主要為大家介紹了Android?自定義來電秀實現(xiàn)總結(jié)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Flutter深色模式適配的實現(xiàn)

    Flutter深色模式適配的實現(xiàn)

    這篇文章主要介紹了Flutter深色模式適配的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • Android編程之頁面切換測試實例

    Android編程之頁面切換測試實例

    這篇文章主要介紹了Android編程之頁面切換測試,實例分析了Android實現(xiàn)頁面點(diǎn)擊切換的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • Android Studio打包.so庫到apk中實例詳解

    Android Studio打包.so庫到apk中實例詳解

    這篇文章主要介紹了Android Studio打包.so庫到apk中實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • Android組合控件自定義標(biāo)題欄

    Android組合控件自定義標(biāo)題欄

    這篇文章主要為大家詳細(xì)介紹了Android組合控件自定義標(biāo)題欄,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Android中AutoCompleteTextView與MultiAutoCompleteTextView的用法

    Android中AutoCompleteTextView與MultiAutoCompleteTextView的用法

    這篇文章主要介紹了Android中AutoCompleteTextView與MultiAutoCompleteTextView的用法,需要的朋友可以參考下
    2014-07-07
  • android讀取raw文件示例

    android讀取raw文件示例

    這篇文章主要介紹了android讀取raw文件示例,需要的朋友可以參考下
    2014-02-02

最新評論

大丰市| 普兰店市| 清镇市| 宁远县| 麻江县| 桃江县| 兰坪| 四子王旗| 微山县| 东城区| 高州市| 永善县| 清徐县| 龙游县| 衢州市| 鄂温| 乳源| 靖州| 吉首市| 塘沽区| 沁源县| 迁安市| 长治县| 林周县| 拉萨市| 灵寿县| 旌德县| 陕西省| 沅江市| 正蓝旗| 扎囊县| 怀安县| 区。| 修水县| 林口县| 当阳市| 澎湖县| 江西省| 阿坝县| 广河县| 霞浦县|