Android getView使滚动变得不平滑

Android getView使滚动变得不平滑,android,android-listview,smooth-scrolling,Android,Android Listview,Smooth Scrolling,我正在尝试从web上加载图像(以及一些未从web上加载的文本),并在列表视图中显示它们。问题是,listview变得一团糟。一些单元格切换得很快,这打乱了listView的顺序 @Override public View getView(final int position, View convertView, ViewGroup parent) { View itemView = convertView; if (itemView == null

我正在尝试从web上加载图像(以及一些未从web上加载的文本),并在
列表视图中显示它们。问题是,
listview
变得一团糟。一些单元格切换得很快,这打乱了
listView
的顺序

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View itemView = convertView;
        if (itemView == null)
            itemView = getLayoutInflater().inflate(R.layout.coin_layout, parent, false);
        //find the coin to work with.
        Coin currentCoin = coins.get(position);

        //fill the view
        try {
            //   ImageView coinIcon = (ImageView) itemView.findViewById(R.id.coinIcon);
            //   coinIcon.setTag(currentCoin.getImageLink());

            ViewHolder holder = new ViewHolder();

            holder.position = position;
            holder.coinIcon = (ImageView) itemView.findViewById(R.id.coinIcon);
            holder.coinName = (TextView) itemView.findViewById(R.id.coinName);
            holder.coinPrice = (TextView) itemView.findViewById(R.id.coinPrice);
            holder.coinPercentage = (TextView) itemView.findViewById(R.id.coinPercentage);
            holder.currentCoin = currentCoin;


            //  holder.coinIcon = (ImageView) itemView.findViewById(R.id.coinIcon);

            // Using an AsyncTask to load the slow images in a background thread
            new AsyncTask<ViewHolder, Void, Bitmap>() {
                private ViewHolder v;
                @Override
                protected Bitmap doInBackground(ViewHolder... params) {
                    v = params[0];
                    Log.d("V log", v.currentCoin.getName());

                    Bitmap bmp = null;
                    try{
                        URL ulrn = new URL(v.currentCoin.getImageLink());
                        HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
                        InputStream is = con.getInputStream();
                        bmp = BitmapFactory.decodeStream(is);

                        if (null != bmp)
                            return bmp;
                    }
                    catch(Exception e){}
                    return bmp;
                }

                @Override
                protected void onPostExecute(Bitmap result) {
                    super.onPostExecute(result);
                    if (v.position == position) {
                        Log.d("V Posion ", "" + v.position);
                        // If this item hasn't been recycled already, hide the
                        // progress and set and show the image
                        //   v.progress.setVisibility(View.GONE);
                        v.coinIcon.setVisibility(View.VISIBLE);
                        v.coinIcon.setImageBitmap(result);

                        v.typeFace = Typeface.createFromAsset(getAssets(), "arial.ttf");
                        v.coinName.setText(v.currentCoin.getName());
                        v.coinPrice.setText("" + v.currentCoin.getPrice());
                        v.coinPercentage.setText(v.currentCoin.getPercentage());
                    }
                }
            }.execute(holder);
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        return itemView;
    }
@覆盖
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
视图项视图=转换视图;
如果(itemView==null)
itemView=GetLayoutFlater()。充气(R.layout.coin_布局,父项,false);
//找到要使用的硬币。
Coin currentCoin=coins.get(位置);
//填充视图
试一试{
//ImageView coincon=(ImageView)itemView.findViewById(R.id.coincon);
//setTag(currentCoin.getImageLink());
ViewHolder=新的ViewHolder();
holder.position=位置;
holder.coincon=(ImageView)itemView.findViewById(R.id.coincon);
holder.coinName=(TextView)itemView.findViewById(R.id.coinName);
holder.coinPrice=(TextView)itemView.findViewById(R.id.coinPrice);
holder.coinPercentage=(TextView)itemView.findViewById(R.id.coinPercentage);
holder.currentCoin=currentCoin;
//holder.coincon=(ImageView)itemView.findViewById(R.id.coincon);
//使用AsyncTask在后台线程中加载慢速图像
新建异步任务(){
私人持票人v;
@凌驾
受保护的位图背景(ViewHolder…参数){
v=参数[0];
Log.d(“V Log”,V.currentCoin.getName());
位图bmp=null;
试一试{
URL ulrn=新URL(v.currentCoin.getImageLink());
HttpURLConnection con=(HttpURLConnection)ulrn.openConnection();
InputStream=con.getInputStream();
bmp=位图工厂.decodeStream(is);
如果(null!=bmp)
返回bmp;
}
捕获(例外e){}
返回bmp;
}
@凌驾
受保护的void onPostExecute(位图结果){
super.onPostExecute(结果);
如果(v.position==位置){
对数d(“V位置”,“V位置+V位置”);
//如果此物品尚未回收,请隐藏
//前进,设置并显示图像
//v.progress.setVisibility(视图已消失);
v、 coincon.setVisibility(View.VISIBLE);
v、 coincon.setImageBitmap(结果);
v、 typeFace=typeFace.createFromAsset(getAssets(),“arial.ttf”);
v、 setText(v.currentCoin.getName());
v、 setText(“+v.currentCoin.getPrice());
v、 setText(v.currentCoin.getPercentage());
}
}
}.执行(持有人);
}捕获(NullPointerException e){
e、 printStackTrace();
}
返回项目视图;
}

您不应该在每个getView上创建异步任务。您看到的是在视图被重用或以随机顺序加载后图像完成加载的结果。查看ThreadPoolExecutor服务。

首先,将为每个列表项调用
getView()
,并且每次都在那里分配
ViewHolder()
,以确定是否可以重用已创建的
ViewHolder()
。另外,我将反对
getView()
中的
AsyncTask()
,相反,您可以使用库在listView上延迟加载图像,这是非常流行且定期更新的

public View getView(int position, View convertView, ViewGroup parent) {
        View itemView = convertView;
        ViewHolder holder = null;
    if (itemView == null){
        itemView = getLayoutInflater().inflate(R.layout.coin_layout, parent, false);
        holder = new ViewHolder();
    }
        Coin currentCoin = coins.get(position);
    try {
        holder.position = position;
        //... rest of the code



        //... rest of the code
        return convertView;
    }

}
尝试使用来处理图像加载。