今天说一下RecyclerView的基础,还及得我们讲过ListView吗,其实今天说的内容就是ListView的进化版,我个人觉得还是RecyclerView好用,而且RecyclerView的功能比较强大,可以实现ListView的功能,而它实现的功能ListView有的不能实现。今天我做了一个recyclerview最简单的用法。先看一下效果图。
看这个效果图是不是有点像ListView,对,我布局代码还是采用原来的代码,为了区分,我加了分割线。现在看一下步骤吧:
1、添加依赖库文件(在app/build.gradle添加)。implementation'com.android.support:recyclerview-v7:27.1.1'
2、创建主xml文件。(recyclerview_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#ffff0000"
android:dividerHeight="10dp"
android:id="@+id/recycler">
</android.support.v7.widget.RecyclerView>
3、创建布局xml文件(我还是用的以前的listview.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/list_photo"
android:layout_width="56dp"
android:layout_height="56dp"
android:paddingLeft="10dp"
android:src="@drawable/a1" />
<TextView
android:id="@+id/list_text"
android:layout_width="match_parent"
android:layout_toRightOf="@id/list_photo"
android:layout_height="56dp"
android:gravity="center"
android:textColor="@android:color/holo_red_dark"
android:textSize="20sp"/>
4、建立一个class文件(Dongwu.class)
public class Dongwu {
private String name;
private int imageId;
public String getName(){
return name;
}
public int getImageId() {
return imageId;
}
public Dongwu(String name,int imageId){
this.name=name;
this.imageId=imageId;
}
}
5、现在我们还要建立一个class文件(配置器:RecyclerViewAdapter)
这里我按照我理解的解释一下,这个类要实现三个方法。
- onCreateViewHolder()这个是在View添加画面,最后返回holder;
- onBindViewHolder()这个是在对布局填充数据;
- getItemCount()这个是返回子项有多少;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
private List<Dongwu> mDongwu;
public class ViewHolder extends RecyclerView.ViewHolder {
TextView dongwuName;
ImageView dongwuImage;
public ViewHolder(View View) {
super(View);
dongwuImage =(ImageView) View.findViewById(R.id.list_photo);
dongwuName =(TextView) View.findViewById(R.id.list_text);
}
}
RecyclerViewAdapter(List<Dongwu> dongwu){
mDongwu=dongwu;
}
@NonNull
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {//这个ViewGroup是一个容器
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listview,parent,false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewAdapter.ViewHolder holder, int position) {//根据position得到具体哪个子项,然后填充数据
Dongwu dongwu= mDongwu.get(position);
holder.dongwuImage.setImageResource(dongwu.getImageId());
holder.dongwuName.setText(dongwu.getName());
}
@Override
public int getItemCount() {
return mDongwu.size();
}
}
6、建立最后一个class文件,用来做各个class连接和填充实例(解释我直接在代码上写);
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class RecyclerViewMain extends AppCompatActivity {
private List<Dongwu> dongwuList = new ArrayList<>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recyclerview_main);
//定义一个方法用来添加实例
init();
//通过ID找到recyclerview
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler);
//recyclerview添加分割线其中VERTICAL_LIST是竖直方向
recyclerView.addItemDecoration(new DividerItemDecoration(this,
itemfenge.VERTICAL_LIST));
DividerItemDecoration divider = new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
divider.setDrawable(ContextCompat.getDrawable(this,R.drawable.fenge));
recyclerView.addItemDecoration(divider);
//建立一个布局来存放recyclerView
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
//实例化方法并和recyclerView建立连接
RecyclerViewAdapter adapter = new RecyclerViewAdapter(dongwuList);
recyclerView.setAdapter(adapter);
}
//添加实例对象
private void init() {
for(int i=0;i<8;i++){
Dongwu niu =new Dongwu("老牛",R.drawable.a1);
dongwuList.add(niu);
Dongwu hu=new Dongwu("老虎",R.drawable.a2);
dongwuList.add(hu);
Dongwu xiong =new Dongwu("黑熊",R.drawable.a3);
dongwuList.add(xiong);
Dongwu xiongma =new Dongwu("熊猫",R.drawable.a4);
dongwuList.add(xiongma);
}
}
}
7、建立分割线的特效(这个是网上找的,这个特效使用各种recyclerview的方向:水平,网状,横向)
public class itemfenge extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int []{
android.R.attr.listDivider
};
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
private Drawable mDivider;
private int mOrientation;
public itemfenge(Context context, int orientation){
final TypedArray a= context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(orientation);
}
public void onDraw(Canvas c, RecyclerView parent) {
if(mOrientation==VERTICAL_LIST){
drawVertical(c,parent);
}else {
drawHorizontal(c,parent);
}
onDraw(c, parent);
}
private void drawHorizontal(Canvas c, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight()-parent.getPaddingBottom();
final int childCount = parent.getChildCount();
for(int i=0;i<childCount;i++){
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int left = child.getRight()+params.rightMargin;
final int right = left+mDivider.getIntrinsicHeight();
mDivider.setBounds(left,top,right,bottom);
mDivider.draw(c);
}
}
private void drawVertical(Canvas c, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth()-parent.getPaddingRight();
final int childCount = parent.getChildCount();
for(int i=0; i <childCount; i++){
final View child =parent.getChildAt(i);
RecyclerView v= new RecyclerView(parent.getContext());
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
final int top = child.getBottom()+params.bottomMargin;
final int bottom = top+mDivider.getIntrinsicHeight();
mDivider.setBounds(left,top,right,bottom);
mDivider.draw(c);
}
}
@Override
@Deprecated
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
if(mOrientation==VERTICAL_LIST){
outRect.set(0,0,0,mDivider.getIntrinsicHeight());
}else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
public void setOrientation(int orientation) {
if(orientation !=HORIZONTAL_LIST&&orientation !=VERTICAL_LIST){
throw new IllegalArgumentException("invaslid");
}
mOrientation = orientation;
}
}
8、设置分割线的颜色(注意布局是shape)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<gradient
android:centerColor="#ff00ff00"
android:endColor="#ff0000ff"
android:startColor="#ffff0000"
android:type="linear" />
<size android:height="3dp" />
</shape>
结语:
RecyclerView是和很好用的view而且不ListView容易理解。如果我说的不对的地方,希望大家可以给我留言或发简信,我及时改正——一个初学者。