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" />
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Flutter의 ListTile에서 높이를 지정하면 레이아웃이 무너지는 문제현재 업무로 1개월 반 정도 Flutter를 사용하고 있습니다. 아주 좋은 팀으로, 최근에는 Flutter 자체에도 열중해 왔습니다. title, subtitle, leading, trailing 등을 설정하는 것만...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.