一.效果:线性/网格/瀑布流
1.RecyclerView横向滑动:
LinearLayoutManager.HORIZONTAL横向滑动LinearLayoutManager.VERTICAL垂直滑动
2.RecyclerView.Adapter中刷新方法区别:
notifyDataSetChanged();整体刷新+没有动画效果
notifyItemInserted(int position,Object data):有动画效果+添加一条数据在position位置
notifyItemRemoved(position);有动画效果+删除一条数据并刷新
注意:当添加和删除的时候,要更新下标,不然有错位现象
3.RecyclerView多布局展示:
public int getItemViewType(int position)返回当前数据的itemview类型
4.RecyclerView常见方法:
LinearLayoutManager:recyclerview线性管理器(垂直水平方向);
GridLayoutManager:网格布局管理器;
StaggeredGridLayoutManager:瀑布流布局管理器;
RecyclerView.setLayoutManager(LayoutManager manager):添加布局管理器
RecyclerView.addItemDecoration(ItemDecoration decoration):添加分割线
RecyclerView.setItemAnimator(ItemAnimator animator):添加动画方法
RecyclerView.setAdapter(Adapter adapter):添加适配器
三.RecyclerView的使用
1.gradle中导入Recyclerview引用;
implementation 'com.android.support:recyclerview-v7:28.0.0‘自己搜索导入
2.适配器:
(1)RecyclerView.Adapter<范型> 重写3个方法
onCreateViewHolder:创建ViewHolder
onBindViewHolder:绑定数据
getItemCount
(2)RecyclerView.ViewHolder
代码
(1)布局activity_main.xml:
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_RV"/>
</LinearLayout>
行布局:centerCrop 填充不拉伸图片 layout_margin设置内边距
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--centerCrop 填充不拉伸图片 layout_margin设置内边距 -->
<ImageView
android:layout_margin="10dp"
android:scaleType="centerCrop"
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
(2)Java代码:MainActivity.java
public class MainActivity extends AppCompatActivity implements MainAdapter.MainItemClickLisener{
private RecyclerView rv;//控件
private ArrayList<Student> list=new ArrayList<>();//数据源
private MainAdapter mainAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rv=findViewById(R.id.rv);
list.add(new Student("A",R.drawable.f1));
list.add(new Student("A",R.drawable.f2));
list.add(new Student("A",R.drawable.f3));
list.add(new Student("A",R.drawable.f4));
list.add(new Student("A",R.drawable.image1));
mainAdapter=new MainAdapter(list,this);
//TODO 1:设置布局管理器
//线性布局
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//网格布局
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
//瀑布流布局:一般和图片结合使用,自适应图片的高度
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
rv.setLayoutManager(linearLayoutManager);
//TODO 2:设置分割线
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
rv.addItemDecoration(dividerItemDecoration);
//TODO 3:设置动画
DefaultItemAnimator defaultItemAnimator = new DefaultItemAnimator();
defaultItemAnimator.setRemoveDuration(2000);//设置延迟2秒删除的效果
defaultItemAnimator.setAddDuration(2000);
rv.setItemAnimator(defaultItemAnimator);
//TODO 4:设置点击事件
mainAdapter.setOnItemClickListener(this);
rv.setAdapter(mainAdapter);
}
@Override
public void ItemClick(int position){
//删除数据
list.remove(i);
notifyItemRemoved(i);//具有动画效果+删除数据
//产生删除错位现象----->重新整理item的下表
notifyItemRangeChanged(0,list.size());//从0开始整理list集合的长度个
}
}
(3)适配器代码:MainAdapter.java
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MainViewHolder> {
private ArrayList<Student> list;
private Context context;
public MainAdapter(ArrayList<Student> list, Context context) {
this.list = list;
this.context = context;
}
//TODO 3:创建ViewHolder
@NonNull
@Override
public MainViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.item_main, null);
return new MainViewHolder(view);
}
//TODO 4:绑定每条数据:填充数据
@Override
public void onBindViewHolder(@NonNull MainViewHolder mainViewHolder, final int i) {
mainViewHolder.iv_pic.setImageResource(list.get(i).getPicId());
mainViewHolder.tv_name.setText(list.get(i).getName()+"");
//给item设置点击事件
mainViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Toast.makeText(context, ""+i, Toast.LENGTH_SHORT).show();
// //删除数据
// list.remove(i);
// notifyItemRemoved(i);//具有动画效果+删除数据
// //产生删除错位现象----->重新整理item的下表
// notifyItemRangeChanged(0,list.size());//从0开始整理list集合的长度个
//不要在这里做处理,采用接口回调
lisener.ItemClick(i);
}
});
}
//TODO 2:返回集合的个数
@Override
public int getItemCount() {
return list.size();
}
//TODO 1:自定义ViewHolder类
class MainViewHolder extends RecyclerView.ViewHolder{
private ImageView iv_pic;//默认值是null
private TextView tv_name;
public MainViewHolder(@NonNull View itemView) {
super(itemView);
this.iv_pic=itemView.findViewById(R.id.iv_pic);
this.tv_name=itemView.findViewById(R.id.tv_name);
}
}
//监听器
private MainItemClickLisener lisener;
//设置监听方法:将摄像头安装在31206
public void setOnItemClickListener(MainItemClickLisener listener){
this.lisener=listener;
}
//定义监听的接口:摄像头
interface MainItemClickLisener{
//一有动静就会调用该方法
public void ItemClick(int position);
}
}
多布局适配器
public class SecondAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private List<PhoneBean>list = new ArrayList<>();
public void refresh(List<PhoneBean>list){
this.list.addAll(list);
notifyDataSetChanged();
}
//根据list中按position获取的type值返回
//返回数据以参数的是形式onCreateViewHolder第二参数
@Override
public int getItemViewType(int position) {
return list.get(position).type;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup,
int i) {
View view = null;
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
if (i==0){
view = inflater.inflate(R.layout.item_title,viewGroup,false);
return new TitleViewHolder(view);
}else {
view = inflater.inflate(R.layout.item_values,viewGroup,false);
return new PhoneViewHolder(view);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
if (list.get(i).type==0){
//首字母
TitleViewHolder vh = (TitleViewHolder) viewHolder;
vh.mTitle.setText(list.get(i).title);
}else {
//联系人详情
PhoneViewHolder vh = (PhoneViewHolder) viewHolder;
vh.mName.setText(list.get(i).name);
vh.mPhone.setText(list.get(i).phone);
}
}
@Override
public int getItemCount() {
return list.size();
}
//多布局操作->1布局对应1ViewHolder
//首字母布局对应的ViewHolder
class TitleViewHolder extends RecyclerView.ViewHolder{
TextView mTitle;
public TitleViewHolder(@NonNull View itemView) {
super(itemView);
mTitle = itemView.findViewById(R.id.title_Tv);
}
}
//联系人详情对应的ViewHolder
class PhoneViewHolder extends RecyclerView.ViewHolder{
TextView mName;
TextView mPhone;
public PhoneViewHolder(@NonNull View itemView) {
super(itemView);
mName = itemView.findViewById(R.id.name1_Tv);
mPhone = itemView.findViewById(R.id.phone1_Tv);
}
}
}