2012-12-20 61 views
2

在我的应用程序,我有一个listAdapter时正在改变,其中IAM装载PHONENUMBERS和从电话簿中的图像,使用如下安卓:列表视图不工作,图片scrollling列表

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

和IAM加载图像的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) { 

     } 
面临的问题,而每个时刻i滚动IAM图像被改变为每个联系人滚动列表视图中,也滚动

IAM不顺畅。我不知道我在代码中做错了什么。任何帮助表示赞赏。

回答

1

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

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

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

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

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

编辑:

我从来没有与QuickContactBadge工作之前,我会写我怎么会为ImageView的做代码,你可以改变的必要部分。 内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 

} 
+0

可以详细说明“getView放置一些空白图像或图像占位符,它将是空白的”? – teekib

+0

我用一个例子编辑了这篇文章。我从来没有使用过'QuickContactBadge',所以我编写代码就好像它是用于ImageView的,你改变了必要的位。并认真研究缓存的东西 – Budius

+0

@任何尝试缓存的东西... iam不从服务器下载这些图像,从设备本身仍然需要缓存?..你可以建议我一些链接开始..谢谢 – teekib

0

尝试设置nullImageView

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

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

} 

工作对我来说..

0

我建议u到看看这个博客: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); 
}});