具有自定义网格视图的 AsyncTask

Posted

技术标签:

【中文标题】具有自定义网格视图的 AsyncTask【英文标题】:AsyncTask with custom grid view 【发布时间】:2016-11-20 05:20:45 【问题描述】:

如何在自定义网格视图中使用异步任务? 我正在为一个迷你项目开发一个 android 应用程序。我有一系列图像,我使用了自定义网格视图。滚动不流畅 我用谷歌搜索了一些说要使用 AsyncTask 但我不明白如何使用 网格视图中的异步任务我的意思是我应该在异步任务中保留什么

public class ImageAdapter extends BaseAdapter 
private Context mContext;

public ImageAdapter(Context c) 
    mContext = c;


public int getCount() 
    return mThumbIds.length;


public Object getItem(int position) 
    return null;


public long getItemId(int position) 
    return 0;


@Override



// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) 
    View grid;


    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewHolder mVHolder;
    if (convertView == null) 
        // if it's not recycled, initialize some attributes
        convertView = inflater.inflate(R.layout.grid_single, parent, false);
        mVHolder=new ViewHolder();
        mVHolder.mImageView=(ImageView)convertView.findViewById(R.id.grid_image);
        convertView.setTag(mVHolder);

     else 
        mVHolder=(ViewHolder)convertView.getTag();
    
    mVHolder.mImageView.setImageResource(mThumbIds[position]);

    return convertView;




// references to our images

private Integer[] mThumbIds = 
       R.drawable.pk001,R.drawable.pk002,R.drawable.pk003,R.drawable.pk004,R.drawable.pk005,R.drawable.pk006
        ,R.drawable.pk007,R.drawable.pk008,R.drawable.pk009,R.drawable.pk010,R.drawable.pk011,R.drawable.pk012
        ,R.drawable.pk013,R.drawable.pk014,R.drawable.pk015,R.drawable.pk016,R.drawable.pk017,R.drawable.pk018
        ,R.drawable.pk019,R.drawable.pk020,R.drawable.p000,R.drawable.pk021,R.drawable.pk022,R.drawable.p000
        ,R.drawable.pk023,R.drawable.pk024,R.drawable.p000,R.drawable.pk025,R.drawable.pk026,R.drawable.p000
        ,R.drawable.pk027,R.drawable.pk028,R.drawable.p000,R.drawable.pk029,R.drawable.pk030,R.drawable.pk031
        ,R.drawable.pk034,R.drawable.pk035,R.drawable.pk036,R.drawable.p000,R.drawable.pk037,R.drawable.pk038
        ,R.drawable.p000,R.drawable.pk039,R.drawable.pk040,R.drawable.p000,R.drawable.pk041,R.drawable.pk042
        ,R.drawable.pk043,R.drawable.pk044,R.drawable.pk045,R.drawable.pk046,R.drawable.pk047,R.drawable.p000,R.drawable.pk048
        ,R.drawable.pk049,R.drawable.p000,R.drawable.pk050,R.drawable.pk051,R.drawable.p000,R.drawable.pk052
        ,R.drawable.pk053,R.drawable.p000,R.drawable.pk054,R.drawable.pk055,R.drawable.p000,R.drawable.pk056
        ,R.drawable.pk057,R.drawable.p000,R.drawable.pk058,R.drawable.pk059,R.drawable.p000,R.drawable.pk060
        ,R.drawable.pk061,R.drawable.pk062,R.drawable.pk063,R.drawable.pk064,R.drawable.pk065,R.drawable.pk066
        ,R.drawable.pk067,R.drawable.pk068,R.drawable.pk069,R.drawable.pk070,R.drawable.pk071,R.drawable.pk072
        ,R.drawable.pk073,R.drawable.p000,R.drawable.pk074,R.drawable.pk075,R.drawable.pk076,R.drawable.pk077
        ,R.drawable.pk078,R.drawable.p000,R.drawable.pk079,R.drawable.pk080,R.drawable.p000,R.drawable.pk081
        ,R.drawable.pk082,R.drawable.p000,R.drawable.pk083,R.drawable.p000,R.drawable.pk084,R.drawable.pk085
        ,R.drawable.p000,R.drawable.pk086,R.drawable.pk087,R.drawable.p000,R.drawable.pk088,R.drawable.pk089
        ,R.drawable.p000,R.drawable.pk090,R.drawable.pk091,R.drawable.p000,R.drawable.pk092,R.drawable.pk093,R.drawable.pk094
        ,R.drawable.p000,R.drawable.pk095,R.drawable.p000,R.drawable.pk096,R.drawable.pk097,R.drawable.p000,R.drawable.pk098
        ,R.drawable.pk099,R.drawable.p000,R.drawable.pk100,R.drawable.pk101,R.drawable.p000,R.drawable.pk102,R.drawable.pk103
        ,R.drawable.p000,R.drawable.pk104,R.drawable.pk105,R.drawable.p000,R.drawable.p000,R.drawable.pk106,R.drawable.p000
        ,R.drawable.p000,R.drawable.pk107,R.drawable.p000,R.drawable.p000,R.drawable.pk108,R.drawable.p000,R.drawable.pk109
        ,R.drawable.pk110,R.drawable.p000,R.drawable.pk111,R.drawable.pk112,R.drawable.p000,R.drawable.p000,R.drawable.pk113
        ,R.drawable.p000,R.drawable.p000,R.drawable.pk114,R.drawable.p000,R.drawable.p000,R.drawable.pk115,R.drawable.p000
        ,R.drawable.pk116,R.drawable.pk117,R.drawable.p000,R.drawable.pk118,R.drawable.pk119,R.drawable.p000,R.drawable.pk120
        ,R.drawable.pk121,R.drawable.p000,R.drawable.p000,R.drawable.pk122,R.drawable.p000,R.drawable.p000,R.drawable.pk123,R.drawable.p000
        ,R.drawable.p000,R.drawable.pk124,R.drawable.p000,R.drawable.p000,R.drawable.pk125,R.drawable.p000,R.drawable.p000,R.drawable.pk126
        ,R.drawable.p000,R.drawable.p000,R.drawable.pk127,R.drawable.p000,R.drawable.p000,R.drawable.pk128,R.drawable.p000
        ,R.drawable.pk129,R.drawable.pk130,R.drawable.p000,R.drawable.p000,R.drawable.pk131,R.drawable.p000,R.drawable.p000
        ,R.drawable.pk132,R.drawable.p000,R.drawable.p000,R.drawable.p000,R.drawable.pk134,R.drawable.p000,R.drawable.pk133
        ,R.drawable.pk135,R.drawable.p000,R.drawable.p000,R.drawable.pk136,R.drawable.p000,R.drawable.pk137,R.drawable.p000
        ,R.drawable.pk138,R.drawable.pk139,R.drawable.p000,R.drawable.pk140,R.drawable.pk141,R.drawable.p000,R.drawable.p000
        ,R.drawable.pk142,R.drawable.p000,R.drawable.p000,R.drawable.pk143,R.drawable.p000,R.drawable.p000,R.drawable.pk144
        ,R.drawable.p000,R.drawable.p000,R.drawable.pk145,R.drawable.p000,R.drawable.p000,R.drawable.pk146,R.drawable.p000
        ,R.drawable.pk147,R.drawable.pk148,R.drawable.p000,R.drawable.pk149,R.drawable.p000,R.drawable.pk150,R.drawable.p000
        ,R.drawable.p000,R.drawable.pk151

;
 

class ViewHolder 
    ImageView mImageView;

GridView gridview = (GridView)getView().findViewById(R.id.gridview);

        gridview.setAdapter(new ImageAdapter(getContext()));

【问题讨论】:

尝试使用这个有用的例子***.com/questions/9671546/… 使用RecyclerView啊,我讨厌你不能在 cmets 中使用删除线。 【参考方案1】:

这种情况不需要使用异步任务

【讨论】:

以上是关于具有自定义网格视图的 AsyncTask的主要内容,如果未能解决你的问题,请参考以下文章

颤振中的自定义高度网格视图

正确实现自定义网格列表视图控件

drupal - 如何在视图模块中为每种网格格式添加自定义 CSS?

在 yii2 中自定义网格视图

用于 iPad App 的 iOS 自定义网格视图以显示下载的图像

自定义视图的getWidth()和getHeight()给出错误的值[重复]