ListView和RecycleView比较

来源:互联网 发布:怎么筛选excel数据 编辑:程序博客网 时间:2024/06/05 04:38

RecyclerView is a more advanced and flexible version of ListView. This widget is a container for large sets of views that can be recycled and scrolled very efficiently. Use the RecyclerView widget when you have lists with elements that change dynamically.

RecycleView是ListView的更高度定制版,当你需要高效的展示大量数据时候,动态改变元素的列表的时候,就用这个。

优点:

1.RecyclerView封装了viewholder的回收复用。

2.RecyclerView使用布局管理器管理子view的位置(LinerLayoutManager、GridLayoytManager、StaggeredGridLayoytManager),不需拘泥于ListView的线性方式,还支持自定义方式。

3.自带的ItemAnimation可以设置加载和移除时的动画,方便做出各种动态浏览的效果。

4.分开的View 。

  在listview中,adapter一般这样写

if (convertView == null) {      holder = new ViewHolder();       LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();       convertView = inflater.inflate(               R.layout.list_device_binding, parent, false);       holder.deviceImage = (ImageView) convertView               .findViewById(R.id.bluetoothDeviceImage);       holder.deviceName = (TextView) convertView               .findViewById(R.id.bluetoothDeviceName);       holder.deviceType = (TextView) convertView               .findViewById(R.id.bluetoothDeviceType);       convertView.setTag(holder);   } else {
<span style="color: rgb(51, 51, 51); font-family: 'microsoft yahei'; font-size: 14px; line-height: 26px;">在RecyclerView中,将其分开</span>
<pre name="code" class="java">@Overridepublic A onCreateViewHolder(ViewGroup parent, int viewType) {           final View view = LayoutInflater.from(mContext).          inflate(R.layout.listitem_track_history, parent, false);    return new ViewHolder(view);  }  @Overridepublic void onBindViewHolder(A holder, int position) {     Data da=getData(position);    holder.tvDate.setText(da.getDate());}

holder = (ViewHolder) convertView.getTag(); }

5.相对简单。

ListView  三重继承

ublic class ListView extends AbsListView public abstract class AbsListView extends AdapterView<ListAdapter>public abstract class AdapterView<T extends Adapter> extends ViewGroup 


RecyclerView直接继承ViewGroup


缺点:

1.不能简单的加头部和尾部

2.不能简单的设置Item的点击事件


实现方案:

1.不能简单的添加header和Footer

这里提供一个https://github.com/tumblr/Bookends 开源库tumbler


@Overridepublic RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {    if (isHeader(viewType)) {        int whichHeader = Math.abs(viewType - HEADER_VIEW_TYPE);        View headerView = mHeaders.get(whichHeader);        return new RecyclerView.ViewHolder(headerView) { };    } else if (isFooter(viewType)) {        int whichFooter = Math.abs(viewType - FOOTER_VIEW_TYPE);        View footerView = mFooters.get(whichFooter);        return new RecyclerView.ViewHolder(footerView) { };    } else {        return mBase.onCreateViewHolder(viewGroup, viewType);    }}@Overridepublic void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {        if (position < mHeaders.size()) {            // Headers don't need anything special      } else if (position < mHeaders.size() + mBase.getItemCount()) {        // This is a real position, not a header or footer. Bind it.        mBase.onBindViewHolder(viewHolder, position - mHeaders.size());     } else {        // Footers don't need anything special    }}@Override   public int getItemViewType(int position) {    if (position < mHeaders.size()) {        return HEADER_VIEW_TYPE + position;} else if (position < (mHeaders.size() + mBase.getItemCount())) {    return mBase.getItemViewType(position - mHeaders.size());} else {    return FOOTER_VIEW_TYPE + position - mHeaders.size() - mBase.getItemCount();}

2.不能简单的添加点击事件onItemClickListener

在listView中,添加点击事件

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {       @Override        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {           User user= parent.getItemAtPosition(position);                           }});


但是,如果你使用这个RecycleView,会发现没有这个接口了。RecyclerView不再负责Item视图的布局及显示,所以RecyclerView也没有为Item开放OnItemClick等点击事件,这就需要我们自己实现,实现的方法像看到有三种,目前下面这种比较合理,所以推荐如下写法

1)让你的viewholder实现onClickListener,然后在这个方法里面回调我们自己写的接口。

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {    private TextView tvDate;                 public ViewHolder(View itemView) {        super(itemView);                tvDate = (TextView) itemView.findViewById(R.id.tv_date);        itemView.setOnClickListener(this);    }    @Override    public void onClick(View v) {        if(listener!=null){            listener.onItemClick(getPosition(),mList.get(getPosition()));        }    }}//我们回调的自定义接口public  interface  onListItemClickListener{         void onItemClick(int position,TrackHistory mTrackHistory);}

2)接着在你的Adapter里面加多个set方法,里面设置回调接口

public  void setOnItemClickListener(onListItemClickListener listener){          this.listener = listener;}



0 0
原创粉丝点击