2012-12-20 34 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); 
}});