1. 布局效果对比
RecycleView默认就支持,线性布局,网格布局,瀑布流布局。
2. API对比
2.1 ListView
继承重写BaseAdaoter类
自定义ViewHolder和convertView一起完成复用优化工作。
2.2 RecycleView
继承重写RecycleView.Adapater和RecycleView.ViewHolder
设置布局管理器,控制布局效果
package com.sihan.chatroomdemo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {
private List<Msg> mMsgList;
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Msg msg =mMsgList.get(position);
if(msg.getType()==Msg.TYPE_RECEIVED){
holder.leftLayout.setVisibility(View.VISIBLE);
holder.rightLayout.setVisibility(View.GONE);
holder.leftMsg.setText(msg.getContent());
}else if(msg.getType()==Msg.TYPE_SENT){
holder.leftLayout.setVisibility(View.GONE);
holder.rightLayout.setVisibility(View.VISIBLE);
holder.rightMsg.setText(msg.getContent());
}
}
@Override
public int getItemCount() {
return mMsgList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder{
LinearLayout leftLayout;
LinearLayout rightLayout;
TextView leftMsg;
TextView rightMsg;
public ViewHolder(@NonNull View view){
super(view);
leftLayout = view.findViewById(R.id.left_layout);
rightLayout = view.findViewById(R.id.right_layout);
leftMsg = view.findViewById(R.id.left_msg);
rightMsg = view.findViewById(R.id.right_msg);
}
}
public MsgAdapter (List<Msg> msgList){
mMsgList = msgList;
}
}
第二步设置布局管理器, 控制布局效果。
LinearLayoutManager layoutManager = new LinearLayoutManager(ChatRoom.this);
msgRecyclerView = findViewById(R.id.msg_recycler_view);
msgRecyclerView.setLayoutManager(layoutManager);
adapter = new MsgAdapter(msgList);
msgRecyclerView.setAdapter(adapter);
从使用上来看:
RecycleView相比ListView在基础上主要有几点:
ViewHolder的编写更规范了。
RecycleView复用Item的工作已经内置了, 不需要像ListView那样设置setTag.
RecycleView需要多出一步布局管理器LayoutManager的设置工作。
3. 布局效果
LayoutManager只是一个抽象类而已, 系统提供了LinearLayoutManager(线性布局效果)、GridLayoutManager(网格布局效果)、StaggeredGridLayoutManager(瀑布流布局效果)。 如果想自定义布局可以继承LayoutManager。
它有下面常用的API
canScrollHorizontally();//能否横向滚动
canScrollVertically();//能否纵向滚动
scrollToPosition(int position);//滚动到指定位置
setOrientation(int orientation);//设置滚动的方向
getOrientation();//获取滚动方向
findViewByPosition(int position);//获取指定位置的Item View
findFirstCompletelyVisibleItemPosition();//获取第一个完全可见的Item位置
findFirstVisibleItemPosition();//获取第一个可见Item的位置
findLastCompletelyVisibleItemPosition();//获取最后一个完全可见的Item位置
findLastVisibleItemPosition();//获取最后一个可见Item的位置
4. 空数据处理
ListView提供了setEmptyView这个api来处理Adapter中数据为空的情况。
mListView.setEmptyView(findViewById(R.id.empty_layout));//设置内容为空时显示的视图
RecycleView并没有提供此类API,得自定义。
局部刷新
ListView中提供了 notifyDataSetChanged(), 当我们更新了数据源后, 通过需要Adapter的notifyDataSetChanged来通知视图更新变化, 但实际并不是每个Item都需要重绘。
RecycleViewAdapter则提供了notifyItemChanged用于简单更新单个ItemView的刷新。
5. 监听Item事件
ListView有单击, 长按, 选中某个Item。
而RecycleView 只提供了 addOnItemTouchListener, 就是监听Item的触摸事件。 如果想像ListView那样监听某个Item的特定操作方法,就得自定义。
6. 嵌套滚动机制
Andorid事件分发时, 由父View向子View传递, 一旦某个字View开始接受进行处理,那么下面的所有事件都将由这个View来进行。
嵌套滚动机制就是为了弥补这一机制的不足, 为了让子View能和父view同时处理一个事件。 关键在于NestedScrollingChild 和 NestedScrollingParent 两个接口, 以及系统对这两个接口的实现类NestedScrollingChildHelper 和 NestedScrollingParentHelper