调用函数在后台更新我的 UI

Calling a function to update my UI in background

我发现如果包含此代码 productPrice.setText(MyLeanCloudApp.getInstance().userTotalCharge.getPriceRangeString(product, context));,我的 gridview 将无法平滑滚动。

数组适配器class

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View rowView = inflater.inflate(R.layout.searchcvc, parent, false);
    TextView productPrice = rowView.findViewById(R.id.SearchCVC_productPrice);

    // issue in here
    productPrice.setText(MyLeanCloudApp.getInstance().userTotalCharge.getPriceRangeString(product, context));

    return rowView;
}

我尝试使用 Handler

Handler h = new Handler();
h.post(new Runnable() {
    @Override
    public void run() {
        productPrice.setText(MyLeanCloudApp.getInstance().userTotalCharge.getPriceRangeString(product, context));
    }
});

但是,还是不能流畅的滚动。有什么建议吗?

最好的办法是使用 AsyncTask。

在后台您将加载图像,在 post 执行中您将用图像膨胀不同的视图,我在我的项目中使用它并且滚动非常流畅因为这是在我滚动时在后台进行的,它将开始填充视图

看看,我正在使用它来顺利填充图像

private class CargarImg extends AsyncTask<Void, Void, Void> {
    private final int mPosition;
    private final MyViewHolder mHolder;
    private String mStringTexto;
    private Drawable mDrawableIcono;

    public CargarImg(int position, MyViewHolder holder) {
            mPosition = position;
            mHolder = holder;
        }

        @Override
        protected Void doInBackground(Void... voids) {
            Bitmap mBitmap;
            try{
                mStringTexto = json.getNombre(mArrayData.get(mPosition));
                mDrawableIcono= json.getIcono(mArrayData.get(mPosition));
                mBitmap = ThumbnailUtils.extractThumbnail(((BitmapDrawable)mDrawableIcono).getBitmap(),150,150);
                mDrawableIcono = new BitmapDrawable(mContext.getResources(), mBitmap);
            }catch (Exception e){

                somethingHappened(mContext, "can't reach the photo");
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

            mHolder.build(mStringTexto,mDrawableIcono);
            mHolder.imageView.setBackgroundColor(cargarColor(json.getTipo(mArrayData.get(mPosition))));


        }
    }

如您所见,我在滚动时正在加载图像,因此,由于我没有一次加载所有图像,因此不会冻结 UI 线程,您可以滚动图像将被加载,这只是我的代码给你的提示,但我真的建议你使用 AsyncTask。

这是我的 bindViewHolderviewHolder

@Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(mLayoutResourceId, parent, false);


        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        new CargarImg(position,holder).execute(); //here i execute the async to load the images smoothly

    }

再多一点:

public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView letterText;
        public ImageView imageView;

        private MyViewHolder(View view) {
            super(view);
            letterText =  view.findViewById(R.id.grid_text);
            imageView = view.findViewById(R.id.grid_image);
        }
        void build(String title, Drawable dr) {
            letterText.setText(title);
            imageView.setImageDrawable(dr);

        }
    }

这基本上是您加载图像所需的所有适配器。