RecyclerView的简单使用【一】
本文参考bilibili博主天哥在奔跑教程:
地址:[https://www.bilibili.com/video/BV1Rt411e76H?p=14]
在原代码的基础上稍作改动,功能:纵向滑动的列表、可以自己控制显示输出列表item
activity_linear_recycler_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
LinearRecyclerViewActivity.java
package android.example.myapplication.recyclerview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.example.myapplication.R;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
public class LinearRecyclerViewActivity extends AppCompatActivity {
private RecyclerView mRvMain;
private LinearAdapter mAdapter;
private List<String> textInfos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_linear_recycler_view);//在Activity中显示布局视图
mRvMain = findViewById(R.id.rv_main);
//Item的布局
mRvMain.setLayoutManager(new LinearLayoutManager(LinearRecyclerViewActivity.this));
//添加分隔线
mRvMain.addItemDecoration(new MyDecoration());
//为item提供数据
textInfos = TextInit(); //初始化字符串list
upDateAdapter();
}
private List<String> TextInit() {
List<String> text = new ArrayList<>(); //模拟数据
text.add(0,"北京");
text.add(1,"上海");
text.add(2,"广州");
text.add(3,"深圳");
text.add(4,"武汉");
return text;
}
private void upDateAdapter() { //新建调用Adapter
mAdapter = new LinearAdapter(this, textInfos);
mRvMain.setAdapter(mAdapter);
}
class MyDecoration extends RecyclerView.ItemDecoration{
public void getItemOffsets(Rect outRect, View view,RecyclerView parent,RecyclerView.State state) {
super.getItemOffsets(outRect,view,parent,state);
outRect.set(0,0,0,getResources().getDimensionPixelOffset(R.dimen.dividerHeight));
}
}
}
layout_linear_item.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"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#FFF"
android:textSize="20sp"
android:background="#8BC34A"></TextView>
</LinearLayout>
LinearAdapter.java
package android.example.myapplication.recyclerview;
import android.content.Context;
import android.example.myapplication.R;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class LinearAdapter extends RecyclerView.Adapter<LinearAdapter.LinearViewHolder> {
private Context mContext;
private List<String> mText;
public LinearAdapter(Context context,List<String> textInfo) {
this.mContext = context;
this.mText = textInfo;
}
@Override
public LinearAdapter.LinearViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_linear_item, parent,false));
}
@Override
public void onBindViewHolder(LinearAdapter.LinearViewHolder holder, final int position) {
String texts = mText.get(position);
holder.bindText(texts);
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext,"click..."+position,Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return mText.size();
}
class LinearViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
public LinearViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.tv_title);
}
public void bindText(String text){
textView.setText(text);
}
}
}
运行效果截图:
总结
三、总结
现在recyclerview可以替代传统的listview,recyclerview包含四个重要的组成部分:Layout Manager:Item的布局;Adapter:为Item提供数据;Item Decoration:Item之间的分割线;Item Animator:添加、删除Item动画。
- 我在使用ItemDecoration添加item之间的分割线时,出现设置了但运行还是看不见的问题,后来才得知它默认是白色的,textview需要设置背景色才能看见。
- 另一个问题是在新建调用Adapter 时,由于已经创建了一个继承RecyclerView.Adapter的类LinearAdapter,所以声明时要使用LinearAdapter而不再是Adapter。