I found that if I include this code productPrice.setText(MyLeanCloudApp.getInstance().userTotalCharge.getPriceRangeString(product, context));
, my gridview would not able to scroll smoothly.
ArrayAdapter 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;
}
MyLeanCloudApp is the class that extends Application
userTotalCharge is a variable of a class
I tried to use Handler
Handler h = new Handler();
h.post(new Runnable() {
@Override
public void run() {
productPrice.setText(MyLeanCloudApp.getInstance().userTotalCharge.getPriceRangeString(product, context));
}
});
But, still can't scroll smoothly. Any suggestions?
The best thing is to use an AsyncTask.
In the background you're going to be loading the images, and in the post execute you will be inflating the different views with the images, I use this in my project and the scroll is really smooth as this is doing in the background as I scroll it will be start populating the views
Take a look, I'm using this to populate images smoothly
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))));
}
}
As you can see I'm loading the images while I'm scrolling, so, since I'm not loading them all at once, it won't freeze the UI thread and you can scroll and the images will be loading, this is just a hint to you from my code, but I really suggest you to use AsyncTask.
And this is my bindViewHolder
and viewHolder
@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
}
A little more:
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);
}
}
This is basically all the adapter you need to have to load your images.