加依赖:
compile 'com.android.support:recyclerview-v7:26.1.0'
布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical" 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" tools:context="com.example.day_0401_recycview.MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/rcv" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:onClick="addItem" android:text="添加" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:onClick="remvoeItem" android:text="移除" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
主方法:
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private List<String> list; private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RecyclerView rcv = findViewById(R.id.rcv); //设置布局管理器 /** * 第一个参数是一个上下文文本 * 第二个参数是方向 * 第三个参数是否反转 * * 第一种是 线性布局 * 第二种是 表格布局 * 第三种是 瀑布流布局 */ rcv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)); //设置item添加和删除动画 rcv.setItemAnimator(new DefaultItemAnimator()); //增加分割线 rcv.addItemDecoration(new MyDividerItemDecoration(this,LinearLayoutManager.VERTICAL)); //第一个参数的话是上下文 //第二个参数是一行有几个 // rcv.setLayoutManager(new GridLayoutManager(this,4)); // rcv.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL)); ititData(); adapter = new MyAdapter(list); rcv.setAdapter(adapter); //MainActivity 回调adapter方法 adapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() { @Override public void itemClick(View v, int position) { Toast.makeText(MainActivity.this, "item点击事件", Toast.LENGTH_SHORT).show(); } @Override public void itemLongClick(View v, int position) { Toast.makeText(MainActivity.this, "item点击长按事件", Toast.LENGTH_SHORT).show(); } }); } private void ititData() { list = new ArrayList<>(); for (int x=0;x<10;x++){ list.add("item"+x); } } //添加item的点击事件 public void addItem(View view) { adapter.add(); } //添加item的点击事件 public void remvoeItem(View view) { adapter.remove(); } }
MyDividerItemDecoration 设置参数:
import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.support.v4.view.ViewCompat; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; public class MyDividerItemDecoration 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 MyDividerItemDecoration(Context context, int orientation) { // 获取默认主题的属性 final TypedArray a = context.obtainStyledAttributes(ATTRS); mDivider = a.getDrawable(0); a.recycle(); setOrientation(orientation); } @Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { // 绘制间隔 if (mOrientation == VERTICAL_LIST) { drawVertical(c, parent); } else { drawHorizontal(c, parent); } } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { if (mOrientation == VERTICAL_LIST) { outRect.set(0, 0, 0, mDivider.getIntrinsicHeight()); } else { outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0); } } private void setOrientation(int orientation) { if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) { throw new IllegalArgumentException("invalid orientation"); } mOrientation = orientation; } //绘制间隔 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); final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child .getLayoutParams(); final int top = child.getBottom() + params.bottomMargin + Math.round(ViewCompat.getTranslationY(child)); final int bottom = top + mDivider.getIntrinsicHeight(); mDivider.setBounds(left, top, right, bottom); mDivider.draw(c); } } //绘制间隔 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 + Math.round(ViewCompat.getTranslationX(child)); final int right = left + mDivider.getIntrinsicHeight(); mDivider.setBounds(left, top, right, bottom); mDivider.draw(c); } } }
MyAdapter 适配器:
import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import java.util.List; public class MyAdapter extends RecyclerView.Adapter { List<String> list; OnItemClickListener listener; public MyAdapter(List<String> list) { this.list = list; } /** * 创建ViewHolder * @param parent * @param viewType * @return */ @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(parent.getContext(), R.layout.rc_item, null); //创建ViewHolder 把view通过构造方法传过去 MyViewHolder myViewHolder = new MyViewHolder(view); return myViewHolder; } /** * 绑定view 简单的可以认为是给item设置数据用的 * @param holder * @param position */ @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { final MyViewHolder myViewHolder = (MyViewHolder) holder; myViewHolder.mtv.setText(list.get(position)); myViewHolder.bt.setText("aaaa"); //给recycleView的item设置点击事件 myViewHolder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //索引 int layoutPosition = myViewHolder.getLayoutPosition(); //进行回调 listener.itemClick(myViewHolder.itemView,layoutPosition); } }); //长按事件 myViewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { //索引 int layoutPosition = myViewHolder.getLayoutPosition(); //进行回调 listener.itemLongClick(myViewHolder.itemView,layoutPosition); return true; } }); } //recyclerView 的item的个数 @Override public int getItemCount() { return list.size(); } //recyclerView的监听事件 public void setOnItemClickListener(OnItemClickListener listener){ this.listener = listener; } class MyViewHolder extends RecyclerView.ViewHolder{ TextView mtv; Button bt; public MyViewHolder(View view) { super(view); mtv =view.findViewById(R.id.rv_tv); bt = view.findViewById(R.id.rv_bt); } } public void add(){ list.add(0,"我是新加的数据"); // notifyDataSetChanged(); notifyItemInserted(0); } //删除item的方法 public void remove(){ if (list==null){ return; }else { //进行移除 list.remove(0); notifyItemRemoved(0); } } interface OnItemClickListener{ void itemClick(View v,int position); void itemLongClick(View v,int position); } }
rc_item适配器布局:
<?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"> <TextView android:id="@+id/rv_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/rv_bt" android:focusable="false" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>