【问题标题】:Calling a function to update my UI in background调用函数在后台更新我的 UI
【发布时间】:2018-11-09 06:46:31
【问题描述】:

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

ArrayAdapter 类

@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;
}
  • MyLeanCloudApp 是扩展Application的类

  • userTotalCharge 是一个类的变量

我尝试使用Handler

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

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

【问题讨论】:

    标签: android android-asynctask android-gridview android-handler


    【解决方案1】:

    最好的办法是使用 AsyncTask。

    您将在后台加载图像,在后期执行中,您将使用图像填充不同的视图,我在我的项目中使用它,滚动非常流畅,就像在滚动时的背景将开始填充视图

    看一下,我正在使用它来平滑地填充图像

    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);
    
            }
        }
    

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

    【讨论】:

    • 我正在使用带有 GridLayoutManager 的 recyclerview , RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager...
    • 我无法发布所有代码,因为它与工作相关,但该提示应该可以帮助您,只需在 doinbackground 中获取您的图像资源并在发布后执行填充您的持有人,在这种情况下我有一个带有文字和图像的持有人,我将其传递给它
    • 没问题,看看在 async im 中传递了两个重要的东西,1 是用图像膨胀的位置,另一个是拥有数据来填充该视图的持有者跨度>
    • 当然。让我做一些搜索以了解。会回来的
    猜你喜欢
    • 2012-02-01
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    • 2015-03-20
    相关资源
    最近更新 更多