2

在我的应用程序中,我有一个 listAdapter,我在其中从电话簿加载电话号码及其图像,如下所示

 public ProfileAdapter(Activity activity, int textViewResourceId, List<Profile> listCont,int renderer, String profileType) {
        super(activity, textViewResourceId, listCont);
        this.renderer = renderer;
        this.listCont = listCont;
        this.activity = activity;
        this.profileType=profileType;
    }

    public class ViewHolder {
        View rowView;
        public TextView textEmailId;
        public TextView textContNo;
        public TextView text;
        public QuickContactBadge photo;
        public ViewHolder(View view)
        {
             rowView = view;
        }
        public TextView getTextView()
        {
             if(text ==null)
                   text = (TextView) rowView.findViewById(R.id.name);
             return text;
        }
        public TextView getTextView1()
        {
             if(textContNo ==null)
                 textContNo = (TextView) rowView.findViewById(R.id.contactno);
             return textContNo;
        }
        public TextView getTextView2()
        {
             if(textEmailId ==null)
                 textEmailId = (TextView) rowView.findViewById(R.id.emailId);
             return textEmailId;
        }
        public QuickContactBadge getQuickContactBadge()
        {
             if(photo ==null)
                 photo = (QuickContactBadge) rowView.findViewById(R.id.quickContactBadge1);
             return photo;
        }

    }


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

    View view = convertView; 
    ViewHolder holder;

    if (view == null) { 
    LayoutInflater inflater = (LayoutInflater) (getContext() 
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE)); 
    view = inflater.inflate(renderer, null); 

    holder = new ViewHolder(view);

 holder.text = (TextView) view.findViewById(R.id.name); 
 holder.textContNo = (TextView) view.findViewById(R.id.contactno); 
 holder.textEmailId = (TextView) view.findViewById(R.id.emailId);  
 holder.photo = (QuickContactBadge ) view.findViewById(R.id.quickContactBadge1);

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

 Profile contact = listCont.get(position); 
    holder.text.setText(contact.getName()); 

    QuickContactBadge photo = (QuickContactBadge ) view.findViewById(R.id.quickContactBadge1);  
    photo.setTag(contact.getMobileNo()); 
    new LoadImage(photo).execute(contact.getMobileNo()); 

    contact.getName(); 
    contact.getId(); 
        holder.textContNo.setText(contact.getNumber());
        holder.textEmailId.setText(contact.getEmail());
        return view;
    }

我正在使用 asynctask 加载图像,如下所示

  class LoadImage extends AsyncTask<String, Void, Bitmap>{ 

        private QuickContactBadge qcb; 

        public LoadImage(QuickContactBadge qcb) { 
        this.qcb= qcb; 
        } 
        @Override 
        protected Bitmap doInBackground( final String... params) { 
        activity.runOnUiThread(new Runnable() { 
        public void run() { 
        new QuickContactHelper(activity, qcb, (String) params[0]).addThumbnail(); 
        } 
        }); 
        return null; 
        } 
        @Override 
        protected void onPostExecute(Bitmap result) { 

        } 

每次滚动时,我都会在滚动列表视图时遇到问题,因为每个联系人的图像都在变化,滚动也不流畅。我不知道我在代码中做错了什么。感谢任何帮助。

4

3 回答 3

1

它运行得很慢,因为您没有在后台线程上执行任何操作。

    activity.runOnUiThread(new Runnable() { 
    public void run() { 
    new QuickContactHelper(activity, qcb, (String) params[0]).addThumbnail(); 
    } 
    }); 

您只是要求在 UI 线程上重新执行。

图像不断变化,因为它显示了滚动之前的图像,并且在该过程完成后它会放置新图像。您应该在 getView 上放置一些空白图像或图像占位符,使其为空白,然后是图像。

也尝试做一些缓存。检查 LruCache。

编辑:

在编写如何为 ImageView 编写代码并且您可以更改必要部分之前,我从未使用过 QuickContactBadge。在getView里面

public View getView(int position, View convertView, ViewGroup parent) {
   // all that processing to get the data then...
   img.setImageResource(R.id.blank_user); // put something image placeholder (blank)
   new LoadImage(img).execute(url); // download

}
于 2012-12-20T11:05:20.457 回答
0

尝试将null值设置为ImageView,

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

   img.setImageResource(null); 
   new LoadImage(img).execute(url); 

}

这个对我有用..

于 2013-03-30T12:46:56.570 回答
0

我建议你看看这个博客:https ://sriramramani.wordpress.com/2012/07/22/recycling-listview-rows/

核心代码在这里:

theListView.setRecyclerListener(new RecyclerListener() {
@Override
public void onMovedToScrapHeap(View view) {
    // Cast the view to the type of the view we inflated.
    MyCustomListRow row = (MyCustomListRow) view;

    // Get the view that holds the image and nullify the drawable.
    // GC will take care of cleaning up the memory used.
    row.getThatImageDrawableView().setImageDrawable(null);
}});
于 2014-02-02T12:18:44.740 回答