0

我正在开发一个 android 应用程序,在该应用程序中实现了具有一些图像和文本的CustomListView 。我在 web 服务调用后在 listview 中提供数据,它需要一些时间来呈现列表,当我滚动列表时它滚动不顺畅。

这是我的列表适配器的代码:

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

}

输出:

在此处输入图像描述

有什么方法可以使列表平滑滚动。

4

1 回答 1

3

您正在解码 getView 中的图像。这是一个昂贵的电话,你会在滚动时做很多事情。使用 LRUCache 缓存图像,因此您可以重用先前解码的结果(如果存在)。如果它不在缓存中,则仅在 getView 中对其进行解码。

于 2013-04-12T06:54:59.090 回答