我第一次使用ViewHolder
和GridView
,我是android / java的新手
使用ImageView
后,它的效果要好得多。
但我遇到的最大问题是滚动是滞后的。 我有两个问题:
第一个:我能做些什么才能让表现更好?
第二个:我可以使用吗?如何使用BitmapWorkerTask
/ AsyncTask
?
我尝试的是android:scrollingCache="false"
,因为每个人都在写作,但似乎android:scrollingCache="true"
对我的表现有点好。
代码:
布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<ImageView android:id="@+id/ic_launcher"
android:contentDescription="@string/desc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="4"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:fastScrollEnabled="true"
android:persistentDrawingCache="scrolling"
android:scrollingCache="true"
android:animationCache="false"
android:smoothScrollbar="true"
/>
</LinearLayout>
ImageAdapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
// Keep all Images in array about 600
public Integer[] mThumbIds = {
};
// Constructor
public ImageAdapter(Context c) {
mInflater = LayoutInflater.from(c);
mContext = c;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) { // if it's not recycled,
convertView = mInflater.inflate(R.layout.categorycontent, null);
convertView.setLayoutParams(new GridView.LayoutParams(130, 130));
holder = new ViewHolder();
holder.icon = (ImageView )convertView.findViewById(R.id.ic_launcher);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.icon.setAdjustViewBounds(true);
holder.icon.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
holder.icon.setImageResource(mThumbIds[position]);
return convertView;
}
class ViewHolder {
ImageView icon;
} }
我希望你们有一些想法给我:)
NewCodewithErrors
}
class ViewHolder {
ImageView icon;
}
// create a new ImageView for each item referenced by the
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) { // if it's not recycled,
convertView = mInflater.inflate(R.layout.categorycontent, null);
convertView.setLayoutParams(new GridView.LayoutParams(130, 130));
holder = new ViewHolder();
holder.icon = (ImageView )convertView.findViewById(R.id.ic_launcher);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.icon.setAdjustViewBounds(true);
holder.icon.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
holder.icon.setImageResource(mThumbIds[position]);
//Here i have the errors:
//The constructor ImageAdapter.LoadImage() is undefine ImageAdapter.java
//The method execute(Integer...) in the type AsyncTask<Integer,Void,Bitmap> is not
//applicable for the arguments (View) ImageAdapter.java
new LoadImage().execute(convertView);
return convertView;
}
private class LoadImage extends AsyncTask<Integer, Void, Bitmap> {
private ImageView imv;
public LoadImage(ImageView imv) {
this.imv = imv;
}
@Override
protected void onPostExecute(Bitmap result) {
if (result != null && imv != null) {
imv.setVisibility(View.VISIBLE);
imv.setImageBitmap(result);
} else {
imv.setVisibility(View.GONE);
}
}
@Override
protected Bitmap doInBackground(Integer... params) {
Bitmap bitmap = null;
bitmap = BitmapFactory.decodeResource(mContext.getResources(),
params[0]);
return bitmap;
}
}}
答案 0 :(得分:0)
首先在适配器中使用内部类,如..
private class LoadImage extends AsyncTask<Integer, Void, Bitmap> {
private ImageView imv;
public LoadImage(ImageView imv) {
this.imv = imv;
}
@Override
protected void onPostExecute(Bitmap result) {
if (result != null && imv != null) {
imv.setVisibility(View.VISIBLE);
imv.setImageBitmap(result);
} else {
imv.setVisibility(View.GONE);
}
}
@Override
protected Bitmap doInBackground(Integer... params) {
Bitmap bitmap = null;
bitmap = BitmapFactory.decodeResource(mContext.getResources(),
params[0]);
return bitmap;
}
}
并在行返回convertView
之上调用它new LoadImage(holder.icon).execute(mThumbIds[position]);