ListView在适配器中一直填充项目

时间:2012-03-25 18:33:32

标签: android android-listview

以下是我的ListView适配器。您可以在其中看到有FillData函数填充列表项的数据。我看到它总是被称为。列表和ListAdapter每个项目应该填一次吗?我做错了什么?

public class GlobalRecipeListAdapter extends ArrayAdapter<Recipe> {

    private final static String mTAG = "GlobalRecipeListAdapter";

    private LayoutInflater  mInflater = null;
    private int mListViewItemResId  = -1;
    private ImageLoader mImageLoader = new ImageLoader( CookTales.Instance() );

    public GlobalRecipeListAdapter(Context context, int resId, ArrayList<Recipe> items) {
         super(context, resId, items );
         mListViewItemResId = resId;
         mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = new ViewHolder();

        if (convertView == null) {
            convertView = mInflater.inflate( mListViewItemResId, null );
            holder.recipePic = new DisplayableImageView( (ImageView) convertView.findViewById( R.id.list_RecipeImageViewId ) );
            holder.name = (TextView)  convertView.findViewById( R.id.name_entry );
            holder.author = (TextView)  convertView.findViewById( R.id.username_entry );
            holder.likes = (TextView)  convertView.findViewById( R.id.likes_entry );
            holder.diffculty = (ImageView) convertView.findViewById( R.id.list_DifficultyImageViewId );
            holder.preparationTime = (ImageView) convertView.findViewById( R.id.list_TimeImageViewId );
            holder.recipePic.GetImageView().setOnClickListener(new ImageClickListener(getContext(), holder.recipePic)); 
                holder.name.setTypeface( CookTales.Fonts.ARIAL );
            holder.name.setTextSize(  CookTales.Fonts.RUNNING_TEXT_SIZE );
            holder.author.setTypeface(  CookTales.Fonts.ARIAL );
            holder.author.setTextSize(  CookTales.Fonts.RUNNING_TEXT_SIZE );
            holder.likes.setTypeface(  CookTales.Fonts.ARIAL );
            holder.likes.setTextSize(  CookTales.Fonts.RUNNING_TEXT_SIZE );
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if( super.getItem(position) != null) {
            if( position == 0 && position == getCount() - 1 ) {
                convertView.setBackgroundResource( R.drawable.list_single );                } else if( position == 0 ) {
                convertView.setBackgroundResource( R.drawable.list_up );
            }
            else if( position == getCount() - 1 ) {
                convertView.setBackgroundResource( R.drawable.list_down );
            } else {
                convertView.setBackgroundResource( R.drawable.list_middle );                }

            FillRecipeDataToHolder( position, holder );
            convertView.setTag(holder);
        } else  {
            holder = (ViewHolder)convertView.getTag();
        }    
        return convertView;
    }

    protected void FillRecipeDataToHolder(int position, ViewHolder viewHolder ) {
        viewHolder.name.setText( getItem(position).GetName() );
        viewHolder.author.setText( getItem(position).GetAuthor() );
        viewHolder.likes.setText( getItem(position).GetNumOfLikes() + " likes.");

        viewHolder.diffculty.setImageResource( Convertor.Difficulty2ImageResId( getItem(position).GetDifficulty(), mTAG ) );
        viewHolder.preparationTime.setImageResource( Convertor.Time2ImageResId( getItem(position).GetPreparationTime() ) );

        if( getItem( position ).GetRecipePicture().isValid() ){
            viewHolder.recipePic.GetImageView().setImageResource( Convertor.Category2ImageView( getItem( position ).GetCategory(), mTAG ) );
            mImageLoader.DisplayImage(  getItem( position ).GetRecipePicture().GetImageLocation(), 
                                        getItem( position ).GetRecipePicture().isUploaded() == false,
                                        getContext(), 
                                        viewHolder.recipePic );
        }
        else
        {
            viewHolder.recipePic.GetImageView().setImageResource( Convertor.Category2ImageView( getItem( position ).GetCategory(), mTAG ) );
        }
    }

    static protected class ViewHolder {
        DisplayableImageView    recipePic;
        TextView                name;
        TextView                author;
        TextView                likes;
        ImageView               diffculty;
        ImageView               preparationTime;
    }
}

2 个答案:

答案 0 :(得分:1)

当您使用标准列表适配器时,框架只能构建足以填满屏幕(以节省内存和CPU周期)。你已经构建了一个自定义的,并且正在使用一个视图持有者,它正在做同样的事情。

如果您制作的列表不会超过一个屏幕,我相信您会看到您期望的活动(或其中的紫胶)。

答案 1 :(得分:1)

每次getView()需要新视图时(因为用户上下滚动列表),都会调用FillRecipeDataToHolder()(和你的ListView)方法显示在可见屏幕上。

如果用户滚动列表直到最后一个元素,将为适配器中的每个项调用该方法,那么如果用户再次开始向上滚动,那么还将再次调用getView()方法以获取尽可能多的元素当用户滚动时(每次都会调用你的填充方法)。