2013-04-26 143 views
0

我想滾動我簡單的listview,但不幸的是它不能順利滾動。加速listview滾動

我不明白什麼似乎是問題。我沒有做任何幻想。

public class ArticleRowAdapter extends BaseAdapter { 
     private static final int TYPE_MAINARTICLE = 0; 
     private static final int TYPE_ARTICLE = 1; 
     private static final int TYPE_MAX_COUNT = TYPE_ARTICLE + 1; 

     private Context context;   
     private ArrayList<ArticleRow> mData = new ArrayList<ArticleRow>(); 
     private LayoutInflater mInflater; 

     public ArticleRowAdapter(Context context,ArrayList<ArticleRow> data) {   
      this.context = context; 
      this.mData = data; 
      mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     } 

     static class ArticleRowHolder 
     { 
      TextView tvTitle; 
      ImageView ivImage; 
      TextView tvSubTitle; 
     } 

     public void addItem(final ArticleRow item) { 
      mData.add(item); 
      notifyDataSetChanged(); 
     } 

     @Override 
     public int getItemViewType(int position) { 
      return position == 0 ? TYPE_MAINARTICLE : TYPE_ARTICLE; 
     } 

     @Override 
     public int getViewTypeCount() { 
      return TYPE_MAX_COUNT; 
     } 

     @Override 
     public int getCount() { 
      return mData.size(); 
     } 

     @Override 
     public ArticleRow getItem(int position) { 
      return mData.get(position); 
     } 

     @Override 
     public long getItemId(int position) { 
      return position; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      ArticleRowHolder holder = null; 
      int type = getItemViewType(position); 

      if(convertView == null) 
      { 
       holder = new ArticleRowHolder(); 
       switch(type) 
       { 
       case TYPE_MAINARTICLE: 
        convertView = mInflater.inflate(R.layout.mainarticle,parent, false);     
        break; 
       case TYPE_ARTICLE: 
        convertView = mInflater.inflate(R.layout.article,parent , false);     
        break;    
       } 

       holder.tvTitle = (TextView)convertView.findViewById(R.id.articleTitle); 
       holder.ivImage = (ImageView)convertView.findViewById(R.id.articleImage);    
       holder.tvSubTitle = (TextView)convertView.findViewById(R.id.articleSubTitle);   
       convertView.setTag(holder); 
      } 
      else 
      { 
       holder = (ArticleRowHolder)convertView.getTag(); 
      } 

      ArticleRow articleRow = mData.get(position); 
      holder.tvTitle.setText((CharSequence)articleRow.title); 
      holder.tvSubTitle.setText(Html.fromHtml(articleRow.subTitle));  

      if(articleRow.imageURL == null) 
       holder.ivImage.setImageDrawable(this.context.getResources().getDrawable(R.drawable.dunk)); 
      else 
      { 
holder.ivImage.setImageDrawable(this.context.getResources().getDrawable(R.drawable.dunk)); 
      } 

      return convertView; 
    } 
+0

http://stackoverflow.com/questions/16032164/about-viewholder-pattern-implementation-optimisation-in-listview也許有用 – Stan 2013-04-26 14:14:58

回答

0

我可以說,滯後的原因是imageViews,那是肯定的:

holder.ivImage = (ImageView)convertView.findViewById(R.id.articleImage); 

儘量不要填寫imageViews與圖片,看看會發生什麼:)。還要檢查谷歌的BitmapFun和LazyList - 無論是關於列表圖像/網格視圖