Smoothing is not there in android listview scrolling, like whatsapp new chat contact list does

懵懂的女人 提交于 2019-12-11 20:33:39

问题


I have my listview with 1 image view and 2 text views but when I scroll it down it don't work smooth, so what should I do??

Now here is my code..

class ReceiptAdapter extends BaseAdapter {

    ArrayList<GetterSetter> receiptlist;
    private LayoutInflater inflater;

    // private int[] colors = new int[] { Color.parseColor("#C8A6DA"),
    // Color.parseColor("#F6F4AB"), Color.parseColor("#A2C3D0"),
    // Color.parseColor("#F1B4A1") };

    private int[] colors = new int[] { Color.parseColor("#2280aee3"),
            Color.parseColor("#22888888") };

    public ReceiptAdapter(Context context,
            ArrayList<GetterSetter> receiptlist) {
        // TODO Auto-generated method stub
        this.receiptlist = receiptlist;
        // inflater = LayoutInflater.from(context);
        // inflater = LayoutInflater.from(context.getApplicationContext());
        inflater = (LayoutInflater) getActivity().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return receiptlist.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder = null;
        if (convertView == null) {
            // convertView = inflater.inflate(R.layout.custom_list, null);
            convertView = inflater.inflate(R.layout.custom_list, parent,
                    false);

            // if (position % 2 == 1) {
            // convertView.setBackgroundColor(Color.BLUE);
            // } else {
            // convertView.setBackgroundColor(Color.CYAN);
            // }

            int colorPos = position % colors.length;
            convertView.setBackgroundColor(colors[colorPos]);

            holder = new ViewHolder();
            holder.Title = (TextView) convertView.findViewById(R.id.name);
            holder.Total = (TextView) convertView.findViewById(R.id.total);
            holder.Img = (ImageView) convertView
                    .findViewById(R.id.profile_image);

            Animation animation = null;
            animation = AnimationUtils.loadAnimation(getActivity(),
                    R.anim.wave);
            animation.setDuration(200);
            convertView.startAnimation(animation);
            animation = null;

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.Title.setText(receiptlist.get(position).getTitle());
        holder.Total.setText(receiptlist.get(position).getTotal());

        String path = receiptlist.get(position).getImg();
        File fileImg = new File(path);
        Bitmap bitmap = null;
        if (fileImg.exists()) {

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 7;



            bitmap = BitmapFactory.decodeFile(fileImg.getAbsolutePath(),
                    options);

            // bitmap = BitmapFactory.decodeFile(fileImg.getAbsolutePath());
            holder.Img.setImageBitmap(bitmap);

        }
        else
        {
            Bitmap icon = BitmapFactory.decodeResource(getActivity().getResources(),
                    R.drawable.no_image);

            holder.Img.setImageBitmap(icon);
        }

        holder.Img.setScaleType(ScaleType.CENTER_CROP);

        return convertView;
    }

    class ViewHolder {
        TextView Title;
        TextView Total;
        ImageView Img;
    }


}

and my async task in which in postExecute() I am calling the custom list adapter as,

ReceiptAdapter adapter = new ReceiptAdapter(getActivity(),
                        recList);
                setListAdapter(adapter);

So, my question is where am I getting wrong?

Like in whatsapp though in contact list, it loads images and shows in image view, I am getting images from sd card and showing in image view, so why the scroll view can't work smoothly?

Whats should I implement for that?

I have searched many code also on stackoverflow, too but I found the same errors of others but didn't get any good feasible solution, so if anybody could help me out in this, then it would be appreciated. Thanks a tone!


回答1:


You need load Images in background, not in the main thread. For this purpose you should use a library that handle that. Best libraries for that are:

  • Picasso http://square.github.io/picasso/
  • Universal image download https://github.com/nostra13/Android-Universal-Image-Loader

After implement one of this libraries you should use this for loading image in background, this libraries cache images in memory and handle other options, like placeholder for error dowwload, apply animation for showing images etc.

Sorry for my bad english




回答2:


Or you could use Volley - this is the standard REST library from Google that is also capable of image loading. The Docs are here



来源:https://stackoverflow.com/questions/29555809/smoothing-is-not-there-in-android-listview-scrolling-like-whatsapp-new-chat-con

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!