Holder vs RecylerView.ViewHolder

时间:2017-04-08 05:32:10

标签: java android listview android-recyclerview android-viewholder

ListView Adapter,并使用持有人。但后来我读到recyclerView.ViewHolder并且现在感到困惑,它与我现在使用的那个有什么不同?我的意思是出于优化目的,我想知道在不使用recyclerView的情况下仅使用持有者是否不够好。

    public class NewsAdapter extends ArrayAdapter<News> {

    Context context;
    List<News> myList;

    public NewsAdapter(Context context, int resource, List<News> objects) {
        super(context, resource, objects);

        this.context = context;
        this.myList = objects;
    }



    @Override
    public News getItem(int position) {
        if(myList != null)
            return myList.get(position);
        return null;
    }

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

        Holder holder;

        if (convertView == null){

            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


            convertView = inflater.inflate(R.layout.list_news, null);

              holder = new NewsAdapter.Holder();

            holder.title = (TextView)convertView.findViewById(R.id.textViewTitle);
            holder.datePosted = (TextView)convertView.findViewById(R.id.textViewDate);
            holder.imageView = (ImageView)convertView.findViewById(R.id.imageViewThumbnailpic);

            convertView.setTag(holder);

        }else{

             holder = (Holder)convertView.getTag();

        }


        News news = getItem(position);

        holder.title.setText(news.getTitle());
        holder.datePosted.setText(news.getDatePost());

        Picasso.with(context)
                .load(news.getImgUrl())
                .placeholder(R.drawable.coin25)
                .error(R.drawable.hnbspic)
                .into(holder.imageView);

        return convertView;
    }

    private class Holder{

        ImageView imageView;
        TextView title;
        TextView datePosted;


    }
}

1 个答案:

答案 0 :(得分:0)

最好使用Recyclerview,因为它已针对各种场景进行了优化,而不仅仅针对View holder模式进行了优化,例如它提供了确定项目布局方式的选项,或者是什么应该是动画或自定义在每个项目中绘图。您可以阅读更多this媒体帖子