ListView 어댑터 최적화 실례


// - adapter public class listviewAdapterDemo1 extends BaseAdapter {

    private String[] mArrData;
    private ListView listView;
    private AsyncImageLoader asyncImageLoader;
    private final LayoutInflater inflater;

    public listviewAdapterDemo1(String[] mArrData, Context mContext, ListView listView) {
        this.mArrData = mArrData;
        this.listView = listView;
        asyncImageLoader = new AsyncImageLoader();
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return this.mArrData.length;
    }

    @Override
    public Object getItem(int arg0) {
        return mArrData[arg0];
    }

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

    /***
     * 1.
     * 2.view ( 8 ) listview , postion
     *  
     *  
     *   , ,
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewholder;
        Log.d("dd", "Position:" + position+"------");
        if (convertView == null) {
            Log.d("dd", "Position:" + position);
            convertView = inflater.inflate(R.layout.list_item_icon_text, null);
            viewholder = new ViewHolder();
            viewholder.icon = (ImageView) convertView.findViewById(R.id.icon);
            viewholder.text = (TextView) convertView.findViewById(R.id.text);

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

        viewholder.text.setText(mArrData[position]);
        // Load the image and set it on the ImageView
        String imageUrl = "";
        viewholder.icon.setTag(imageUrl);
        Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() {

            public void imageLoaded(Drawable imageDrawable, String imageUrl) {
                ImageView imageViewByTag = (ImageView) listView.findViewWithTag(imageUrl);
                if (imageViewByTag != null) {
                    imageViewByTag.setImageDrawable(imageDrawable);
                }
            }
        });
        if (cachedImage != null)//
            viewholder.icon.setImageDrawable(cachedImage);

        return convertView;
    }

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

listview 의 getview 방법 에서 도 이 방법 을 사용 할 수 있 습 니 다.

public Drawable getDrawable(AsyncImageLoader asyncImageLoader, String imageUrl, final ImageView imageView) {
Drawable drawable = asyncImageLoader.loadDrawable(imageUrl,new ImageCallback() {
@Override public void imageLoaded(Drawable imageDrawable,String imageUrl) {
if (imageDrawable != null)
imageView.setImageDrawable(imageDrawable);
else
imageView.setImageResource(R.drawable.u6_normal);
}
});
return drawable;
}

// ---adapter public class listviewAdapterDemo2 extends BaseAdapter {

    private String[] mArrData;
    private ListView listView;
    private AsyncImageLoader asyncImageLoader;
    private final LayoutInflater inflater;
    private Map<Integer, View> viewMap = new HashMap<Integer, View>();

    public listviewAdapterDemo2(String[] mArrData, Context mContext, ListView listView) {
        this.mArrData = mArrData;
        this.listView = listView;
        asyncImageLoader = new AsyncImageLoader();
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return this.mArrData.length;
    }

    @Override
    public Object getItem(int arg0) {
        return mArrData[arg0];
    }

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

    /**
     * 1.
     * 2. postion view, , postion    * ,
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewholder;
        Log.d("dd", "Position:" + position+"------");
        convertView = this.viewMap.get(position);
        if (convertView == null) {
            Log.d("dd", "Position:" + position);
            convertView = inflater.inflate(R.layout.list_item_icon_text, null);
            viewholder = new ViewHolder();
            viewholder.icon = (ImageView) convertView.findViewById(R.id.icon);
            viewholder.text = (TextView) convertView.findViewById(R.id.text);

            viewholder.text.setText(mArrData[position]);
            // Load the image and set it on the ImageView
            String imageUrl = "";
            viewholder.icon.setTag(imageUrl);
            Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() {

                public void imageLoaded(Drawable imageDrawable, String imageUrl) {
                    ImageView imageViewByTag = (ImageView) listView.findViewWithTag(imageUrl);
                    if (imageViewByTag != null) {
                        imageViewByTag.setImageDrawable(imageDrawable);
                    }
                }
            });
            if (cachedImage != null)//
                viewholder.icon.setImageDrawable(cachedImage);

           
            convertView.setTag(viewholder);
            viewMap.put(position, convertView);
        } else {
            viewholder = (ViewHolder) convertView.getTag();
        }
        return convertView;
    }

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


/** -adpater */
public class AsyncImageLoader {

    private HashMap<String, SoftReference<Drawable>> imageCache; 

    public AsyncImageLoader() { 
        imageCache = new HashMap<String, SoftReference<Drawable>>(); 
    } 

    public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) { 
        //
        if (imageCache.containsKey(imageUrl)) { 
            SoftReference<Drawable> softReference = imageCache.get(imageUrl); 
            Drawable drawable = softReference.get(); 
            if (drawable != null) { 
                return drawable; 
            } 
        } 
        final Handler handler = new Handler() { 
            @Override 
            public void handleMessage(Message message) { 
                imageCallback.imageLoaded((Drawable) message.obj, imageUrl); 
            } 
        }; 
        //
        new Thread() { 
            @Override 
            public void run() { 
                Drawable drawable = loadImageFromUrl(imageUrl); 
                imageCache.put(imageUrl, new SoftReference<Drawable>(drawable)); 
                Message message = handler.obtainMessage(0, drawable); 
                handler.sendMessage(message); 
            } 
        }.start(); 

        return null; 
    } 

    public static Drawable loadImageFromUrl(String url) { 
        Drawable drawable = null;
        // url
        return drawable;
    } 

    public interface ImageCallback { 
        public void imageLoaded(Drawable imageDrawable, String imageUrl); 
    } 


//activity---adapterpublic class ListviewDemoActivity extends Activity {

    private listviewAdapterDemo1 mAdapter;
    private ListView listview;
    private String[] mArrData;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mArrData = new String[1000];
        for (int i = 0; i < 1000; i++) {
            mArrData[i] = "Google IO Adapter" + i;
        }

        listview = (ListView)findViewById(R.id.lv);
        mAdapter = new listviewAdapterDemo1(mArrData,this,listview);
        listview.setAdapter(mAdapter);
    }
}

main.xml 에 삽입

<ListView
        android:id="@+id/lv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" />

좋은 웹페이지 즐겨찾기