跟着《Android进阶之光》复习下RecyclerView。
重要的几点:
- 你想要控制其显示的方式,请通过布局管理器LayoutManager
- 你想要控制Item间的间隔(可绘制),请通过ItemDecoration
- 你想要控制Item增删的动画,请通过ItemAnimator
- 你想要控制点击、长按事件,请自己写
先来效果图:
然后上代码!!!
activity:
public class RecyclerViewActivity extends AppCompatActivity implements View.OnClickListener {
private RecyclerView recyclerView;
private List<String> dataList;
private int dataSize = 20;
private Button btnAddItem;
private Button btnRemoveTerm;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view);
initView();
}
private void initView() {
//list增删item的按钮
btnAddItem = findViewById(R.id.btn_addTerm);
btnRemoveTerm = findViewById(R.id.btn_removeTerm);
btnAddItem.setOnClickListener(this);
btnRemoveTerm.setOnClickListener(this);
//拿到recyclerView
recyclerView = findViewById(R.id.recyclerView);
//设置布局管理器
recyclerView.setLayoutManager(new LinearLayoutManager(this));
// recyclerView.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL));
//设置item增删时的动画
recyclerView.setItemAnimator(new DefaultItemAnimator());
//添加装饰(DividerItemDecoration是添加分割线,分割线的样式也可自定义)
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
//初始化数据
dataList = new ArrayList<>();
for (int i = 0; i < dataSize; i++) {
dataList.add("hehe" + i);
}
//创建适配器
adapter = new MyAdapter(this, dataList);
//通过适配器设置item点击事件
adapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
@Override
public void onItemClick(View v, int tag) {
Toast.makeText(RecyclerViewActivity.this, "点了,位置:"+ tag, Toast.LENGTH_SHORT).show();
}
@Override
public void onItemLongClick(View v, int tag) {
Toast.makeText(RecyclerViewActivity.this, "长按了,位置:"+ tag, Toast.LENGTH_SHORT).show();
}
});
//设置适配器
recyclerView.setAdapter(adapter);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_addTerm:
// adapter.addData(1, "addItem");
adapter.addDataWithAnimator(1, "addItemWithAnimator");
break;
case R.id.btn_removeTerm:
// adapter.removeData(1);
adapter.removeDataWithAnimator(2);
break;
default:
break;
}
}
/**
* 自定义的适配器
*/
private static class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> implements View.OnClickListener, View.OnLongClickListener {
private final Context context;
//要展示的数据
private final List<String> dataList;
private OnItemClickListener mOnItemClickListener;
public MyAdapter(Context context, List<String> dataList) {
this.context = context;
this.dataList = dataList;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//加载Item布局,然后创建ViewHolder
View view = LayoutInflater.from(context).inflate(R.layout.recyclerview_item, parent, false);
//设置监听
view.setOnClickListener(this);
view.setOnLongClickListener(this);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
//视图绑定数据
holder.textView.setText(dataList.get(position));
//position设置给itemView的Tag
holder.itemView.setTag(position);
}
@Override
public int getItemCount() {
return dataList.size();
}
/**
* 提供使用 adapter给item设置点击监听事件
* @param onItemClickListener
*/
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.mOnItemClickListener = onItemClickListener;
}
@Override
public void onClick(View v) {
if(mOnItemClickListener != null){
mOnItemClickListener.onItemClick(v, (int)v.getTag());
}
}
@Override
public boolean onLongClick(View v) {
if(mOnItemClickListener != null){
mOnItemClickListener.onItemLongClick(v, (int)v.getTag());
}
return true;
}
//添加数据
public void addData(int position, String data){
if (dataList != null) {
dataList.add(position, data);
//刷新
notifyDataSetChanged();
}
}
//添加数据with动画
public void addDataWithAnimator(int position, String addItem) {
if (dataList != null) {
dataList.add(position, addItem);
notifyItemInserted(position);
}
}
//删除数据
public void removeData(int position){
if (dataList != null) {
if ((dataList.size() > position )) {
dataList.remove(position);
//刷新
notifyDataSetChanged();
}else {
Toast.makeText(context, "长度不够!", Toast.LENGTH_SHORT).show();
}
}
}
//删除数据with动画
public void removeDataWithAnimator(int position){
if (dataList != null) {
if ((dataList.size() > position )) {
dataList.remove(position);
//刷新
notifyItemRemoved(position);
}else {
Toast.makeText(context, "长度不够!", Toast.LENGTH_SHORT).show();
}
}
}
//viewHolder
public class MyViewHolder extends RecyclerView.ViewHolder {
private final TextView textView;
public MyViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.tv_item);
}
}
/**
* Item点击事件接口,由activity实现
*/
public interface OnItemClickListener {
void onItemClick(View v, int tag);
void onItemLongClick(View v, int tag);
}
}
}
activity的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_addTerm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="add"/>
<Button
android:id="@+id/btn_removeTerm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="remove"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="64dp">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
item的xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_item"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@color/colorAccent"/>
</android.support.constraint.ConstraintLayout>
分割线的自定义:
styles.xml中:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<!--recyclerView的自定义的分割线的样式-->
<item name="android:listDivider">@drawable/bg_recyclerview_divider</item>
</style>
</resources>
drawable文件夹:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:centerColor="#ff00ff00"
android:endColor="#ff0000ff"
android:startColor="#ffff0000"
android:type="linear"/>
<size
android:width="10dp"
android:height="10dp"/>
</shape>