安卓recyclerview的基本使用
1.先在布局文件中写
<android.support.v7.widget.RecyclerView android:id="@+id/my_recycler_view" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent"/>
2.mainactivity
public class MainActivity extends AppCompatActivity { private RecyclerView mRecyclerView; private ArrayList<String> mDatas=new ArrayList<>(); private MyAdapter myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRecyclerView = findViewById(R.id.my_recycler_view); initDatas(); myAdapter =new MyAdapter(mDatas); mRecyclerView.setAdapter(myAdapter); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); //mRecyclerView.setLayoutManager(new GridLayoutManager(this,4)); // mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.HORIZONTAL)); //配置分割线 mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST)); //事件监听 myAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() { @Override public void onClick(View v, int position, String city) { Toast.makeText(MainActivity.this,city,Toast.LENGTH_SHORT).show(); } }); int size =mDatas.size(); //添加数据到头部 myAdapter.addData(size,"adddddddddd"); //删除数据 myAdapter.removeData(0); } private static final String TAG = "MainActivity"; private void initDatas(){ mDatas.add("aa"); mDatas.add("cv"); mDatas.add("cv1"); mDatas.add("cv112"); mDatas.add("cv112"); mDatas.add("cv112"); mDatas.add("cv112"); mDatas.add("cv112"); mDatas.add("cv112"); mDatas.add("cv112"); mDatas.add("cv112"); mDatas.add("cv112"); mDatas.add("cv112"); mDatas.add("cv112"); } }
3.adpter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private static List<String> mData; private static OnItemClickListener mListener; public void setOnItemClickListener (OnItemClickListener listener){ mListener=listener; } public MyAdapter(List<String> data) { mData = data; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { // 实例化展示的view View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_rv_item, parent, false); // 实例化viewholder ViewHolder viewHolder = new ViewHolder(v); return viewHolder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { // 绑定数据 holder.mTv.setText(mData.get(position)); } @Override public int getItemCount() { return mData == null ? 0 : mData.size(); } public static class ViewHolder extends RecyclerView.ViewHolder { TextView mTv; public ViewHolder(View itemView) { super(itemView); mTv = (TextView) itemView.findViewById(R.id.item_tv); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(mListener!=null){ mListener.onClick(view,getLayoutPosition(),mData.get(getLayoutPosition())); } } }); } } interface OnItemClickListener{ void onClick(View v,int position,String city); } /* * @param 添加的位置 * @param 添加的数据 * */ public void addData(int position,String city){ mData.add(position,city); notifyItemInserted(position); } public void removeData(int position){ mData.remove(position); notifyItemRemoved(position); } }
单独创建item的布局,view_rv_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="45dp"> <TextView android:id="@+id/item_tv" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
item的下划线,要自己写一个类,有点麻烦,直接抄了网上
public class DividerItemDecoration 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 DividerItemDecoration(Context context, int orientation) { final TypedArray a = context.obtainStyledAttributes(ATTRS); mDivider = a.getDrawable(0); a.recycle(); setOrientation(orientation); } public void setOrientation(int orientation) { if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) { throw new IllegalArgumentException("invalid orientation"); } mOrientation = orientation; } @Override public void onDraw(Canvas c, RecyclerView parent) { // Log.v("recyclerview - itemdecoration", "onDraw()"); if (mOrientation == VERTICAL_LIST) { drawVertical(c, parent); } else { drawHorizontal(c, parent); } } public 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); android.support.v7.widget.RecyclerView v = new android.support.v7.widget.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); } } public 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); } } @Override 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); } } }
对了,使用之前要配置先的
api 'com.android.support:recyclerview-v7:26.1.0'
这里写了展示和点击事件,比较简单,