关于RecyclerView,我想大家都不陌生了,其基本使用就直接上流程步骤了。
第一步:添加依赖;
compile 'com.android.support:recyclerview-v7:25.1.1'
第二步:创建对象并初始化;
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.aaaaa.snaphelperdemo.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recyclerview);
// 设置布局管理器
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayout.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
// 设置item添加、移除动画
recyclerView.setItemAnimator(new DefaultItemAnimator());
// 添加分割线
recyclerView.addItemDecoration(new DividerItemDecoration(
getApplicationContext(), DividerItemDecoration.VERTICAL));
// 添加adapter
}
第三步:写adapter;
A 布局文件recyclerview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/recyclerview_item_text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:layout_gravity="center"/>
</LinearLayout>
B Adapter代码
package com.example.aaaaa.recyclerviewtest;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by 49375 on 2019/3/19.
*/
public class RecyclerViewTestAdapter extends RecyclerView.Adapter<RecyclerViewTestAdapter.MyHolder> {
@Override
public RecyclerViewTestAdapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item, null);
MyHolder myHolder = new MyHolder(view);
return myHolder;
}
@Override
public void onBindViewHolder(RecyclerViewTestAdapter.MyHolder holder, int position) {
holder.textView.setText(String.valueOf(position));
}
@Override
public int getItemCount() {
return 20;
}
// ViewHolder用来初始化控件
public class MyHolder extends RecyclerView.ViewHolder {
TextView textView;
public MyHolder(View itemView) {
super(itemView);
textView = (TextView)itemView.findViewById(R.id.recyclerview_item_text);
}
}
}
C onCreate中recyclerView设置adapter
// 添加adapter
RecyclerView.Adapter adapter = new RecyclerViewTestAdapter();
recyclerView.setAdapter(adapter);
以上,就是recyclerView的基本使用步骤,后续将会对RecyclerView实现侧滑删除、snaphelper实现view Pager+fragment左右翻页的功能等做记录。