Ricky Khatri
Ricky Khatri

Reputation: 960

Android Listview not scrolling smoothly with multple images

I am working on an android application in which i have implemented CustomListView having some images and texts. i am feeding data in listview after a webservice call it takes some time to render list and when i scroll the list it's not scrolling smoothly.

here is the code of my List Adapter:

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import com.tv.makamaka.Constant;
import com.tv.makamaka.R;
import com.tv.makamaka.comment.SmileyCommentDialogActivity;

public class MakaMakaProfileListAdapter extends BaseAdapter implements Filterable {
    private LayoutInflater mInflater;
    private ArrayList<Object> itemList;
    private Activity _activity;

    String caption="";
    String photoPath="";
    String comments="";
    String commentImage="";
    String commentUserId="";
    String likersImage="";
    String likersType="";
    String likersUserId="";
    String mediaid="";
    String mediaImageUrl="";
    String mediaString="";
    String mediaStringImage="";
    String mediaStringUserId="";
    String mediaType="";
    String rating="";
    String requestCount="";
    String userid="";
    String userProileImage="";
    String withfriends="";

    int index,index1,index2;

    public MakaMakaProfileListAdapter(Activity activity, ArrayList<Object> itemList) {
        this._activity = activity;
        this.itemList = itemList;
        mInflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {

        return itemList.size();
    }

    public Object getItem(int position) {
        return itemList.get(position);
    }

    public Filter getFilter() {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    static class ViewHolder {

        TextView _commentTxt;
        ImageView _userImage;
        ImageView _smallUserImage;
        ImageView _smallPostedUserImage;
        ImageView _transparentImage;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.makamaka_profile_listrow, null);
            holder = new ViewHolder();
            holder._userImage=(ImageView)convertView.findViewById(R.id.posted_userimage);
            holder._smallUserImage=(ImageView)convertView.findViewById(R.id.small_userimage);
            holder._smallPostedUserImage=(ImageView)convertView.findViewById(R.id.small_posted_userimage);
            holder._transparentImage=(ImageView)convertView.findViewById(R.id.transparent_smiley_image);
            holder._commentTxt = (TextView) convertView.findViewById(R.id.comment_txt);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder._commentTxt.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

            }
        });

        holder._transparentImage.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent intent=new Intent(_activity,SmileyCommentDialogActivity.class);
                _activity.startActivity(intent);
            }
        });
        //////////////////SET VALUES ON LIST VIEW///////////////////////////
        MakaMakaProfileBean makaMakaProfileBean=(MakaMakaProfileBean)itemList.get(position);
        ////////////////PARSE CAPTION///////////
        caption=makaMakaProfileBean.getCaption();
        ArrayList<String> captionList = new ArrayList<String>(Arrays.asList(caption.split(" , ")));
        /*int index=caption.indexOf(",");
        String caption1=caption.substring(0,index);
        int index1=caption.indexOf(",", index+1);
        String userName=caption.substring(index,index1+1);
        String timeAgo=caption.substring(index1+1);*/
        ////////////////////////////////////////

        ////////////////PARSE COMMENTS////////////
        comments=makaMakaProfileBean.getComments();
        ArrayList<String> commentList = new ArrayList<String>(Arrays.asList(comments.split(" , ")));
        //////////////////////////////////////////

        ////////////////PARSE COMMENT IMAGES//////
        commentImage=makaMakaProfileBean.getCommentImage();
        ArrayList<String> commentImageList = new ArrayList<String>(Arrays.asList(commentImage.split(" , ")));
        //////////////////////////////////////////

        ////////////////PARSE COMMENT USER ID//////
        commentUserId=makaMakaProfileBean.getCommentId();
        ArrayList<String> commentUserIdList = new ArrayList<String>(Arrays.asList(commentUserId.split(" , ")));
        //////////////////////////////////////////

        ////////////////PARSE LIKER IMAGES//////
        likersImage=makaMakaProfileBean.getlikersImage();
        ArrayList<String> likersImageList = new ArrayList<String>(Arrays.asList(likersImage.split(" , ")));
        //////////////////////////////////////////

        ////////////////PARSE LIKER TYPE//////
        likersType=makaMakaProfileBean.getComments();
        ArrayList<String> likerTypeList = new ArrayList<String>(Arrays.asList(likersType.split(" , ")));
        //////////////////////////////////////////

        ////////////////PARSE LIKER USER ID//////
        likersUserId=makaMakaProfileBean.getComments();
        ArrayList<String> likerUserIdList = new ArrayList<String>(Arrays.asList(likersUserId.split(" , ")));
        //////////////////////////////////////////

        ////////////////PARSE OTHER PARAS//////
        mediaid=makaMakaProfileBean.getmediaId();
        mediaImageUrl=makaMakaProfileBean.getMediaImageUrl();
        mediaString=makaMakaProfileBean.getMediaString();
        mediaStringImage=makaMakaProfileBean.getMediaStringImage();
        //mediaStringUserId=makaMakaProfileBean.
        mediaType=makaMakaProfileBean.getMediaType();
        rating=makaMakaProfileBean.getRating();
        requestCount=makaMakaProfileBean.getRequestCount();
        userid=makaMakaProfileBean.getUserId();
        //userProileImage=makaMakaProfileBean.getUserProfileImage();
        withfriends=makaMakaProfileBean.getWithFriends();
        //////////////////////////////////////////

        photoPath=makaMakaProfileBean.getUserProfileImage();
        if(photoPath!=null){

            URL url;
            try {
                url = new URL(photoPath);
                Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                holder._userImage.setImageBitmap(bmp);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }       
        int i=0;
        while(i<captionList.size()){
            if(i==0){
                holder._commentTxt.setText((String)captionList.get(i));
            }else if(i==1){
                holder._commentTxt.setText(Html.fromHtml((String)captionList.get(i)));
            }else if(i==2){
                holder._commentTxt.setText(Html.fromHtml((String)captionList.get(i)));
            }
            i++;
        }
        return convertView;
        /////////////////////////////////////////////////////////////////////
    }

    private Handler _handler = new Handler() {
        public void dispatchMessage(Message msg) {
            switch (msg.arg1) {

            case 123:
                break;
            default:
                break;
            }
        }
    };

    public void serverResponse(String response, int processid) {
        Message msg = new Message();
        msg.arg1 = processid;
        msg.obj = response;
        _handler.dispatchMessage(msg);
    }

    //SEND EMAIL TO INVITE FRIEND
    public void sendEmail(String emailId){
        Intent intent = new Intent(Intent.ACTION_SEND);
        //intent.setType("text/plain");
        intent.setType("message/rfc822");
        intent.putExtra(Intent.EXTRA_EMAIL, emailId);
        intent.putExtra(Intent.EXTRA_SUBJECT,Constant.EMAIL_SUBJECT);
        intent.putExtra(Intent.EXTRA_TEXT, Constant.EMAIL_CONTENT);
        _activity.startActivity(Intent.createChooser(intent, "Send Email"));
    }

}

Output:

enter image description here

Is there any way to make list with smooth scroll.

Upvotes: 0

Views: 1536

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93708

You're decoding the image in getView. That's an expensive call, and you're going to be doing it a lot while scrolling. Use an LRUCache to cache images, so you can reuse the results of previous decodes if they exist. Only decode it in getView if its not in the cache.

Upvotes: 3

Related Questions