public class MainActivity extends AppCompatActivity { private RecyclerView mRecyclerView; List<String> mDatas; MyAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview); initData();//初始化数据 mAdapter = new MyAdapter(mDatas, this);//初始化Adapter(设配器) mRecyclerView.setAdapter(mAdapter);//设置Adapter //设置分隔线 // mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL)); //必须要设置布局管理器,不然界面是空的 完成listview的效果 // // LinearLayoutManager layout = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,true); // mRecyclerView.setLayoutManager(layout); 完成listview的效果 // //设置gridview 的效果 // mRecyclerView.setLayoutManager(new GridLayoutManager(this,3)); //这里如果是水平的,而item的宽度item的个数不足以填充宽度,item与item之间会自动有空白来代替 mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL)); mAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() { //RecyclerView的item点击事件 @Override public void onItemClickListener(int position, View v) { mAdapter.add(position); } @Override public void onItemLongClickListener(int position, View v) { mAdapter.delete(position); } }); } private void initData() { mDatas = new ArrayList<>(); for (int i = 'A'; i < 'G'; i++) { mDatas.add((char) i + ""); } } } Adapter类的代码:public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> { List<String> mList; Context mContext; private LayoutInflater mInflater; public MyAdapter(List<String> list, Context context) { mList = list; mContext = context; mInflater = LayoutInflater.from(mContext); } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { //注意这里的参数只能改第一个参数resource,只能使用LayoutInflater.inflate.(resource,viewGroup,attachToRoot); View view = mInflater.inflate(R.layout.item, parent, false); MyViewHolder myViewHolder = new MyViewHolder(view); return myViewHolder; } public interface OnItemClickListener { void onItemClickListener(int position,View v); void onItemLongClickListener(int position,View v); } private OnItemClickListener mListener; public void setOnItemClickListener(OnItemClickListener listener) { mListener = listener; } @Override public void onBindViewHolder(final MyViewHolder holder, final int position) { if (mListener != null) { holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int layoutPosition = holder.getLayoutPosition(); mListener.onItemClickListener(layoutPosition,v);//要用holder.getLayoutPosition()的数,得到布局上的item的排序 } }); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { int layoutPosition = holder.getLayoutPosition(); mListener.onItemLongClickListener(layoutPosition,v);//这个position是不准确的, // 因为当增删item,position会不准确,还是会依照一开始的条目排序,有时position并不是会随item改变而变 return false; } }); } holder.mTv.setText(mList.get(position)); //在这里设置随机高度 ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams(); layoutParams.height = 100 + r.nextInt(200); holder.itemView.setLayoutParams(layoutParams); } Random r = new Random(); @Override public int getItemCount() { if (mList == null) return 0; return mList.size(); } public void add(int position) { mList.add(position, "insert one"); notifyItemInserted(position);//代表插入第几个item } public void delete(int position) { mList.remove(position); notifyItemRemoved(position); } } class MyViewHolder extends RecyclerView.ViewHolder { TextView mTv; public MyViewHolder(View itemView) { super(itemView); mTv = (TextView) itemView.findViewById(R.id.tv); } }item布局文件代码里面就一个TextView自己可以修改:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="72dp" android:layout_margin="3dp" android:orientation="vertical" android:layout_height="50dp" android:background="#5f00"> <TextView android:id="@+id/tv" android:gravity="center" android:text="A" android:textColor="#fff" android:layout_width="72dp" android:layout_height="50dp"/> </LinearLayout>
如果要简单自定义分隔线,可以在src/res/drawable里画些样式:<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <size android:height="4dp"></size> <gradient android:centerColor="#0f0" android:endColor="#00f" android:startColor="#f00" android:type="linear"></gradient> </shape>记得要生效的话必须要在使用的主题里添加一行代码:<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!--这是从DividerItemDecoration里面的属性来的,在你使用的主题里改变一下就可以--> <item name="android:listDivider">@drawable/divider02(这里是自己画的xml文件名)</item> </style>
总结:RecyclerView比较强大,但是要自己实现item点击事件,主要的item里的position要注意,使用这个方法holder.getLayoutPosition()来替代position,还有的话是public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { //注意这里的参数只能改第一个参数resource,只能使用LayoutInflater.inflate.(resource,viewGroup,attachToRoot); View view = mInflater.inflate(R.layout.item, parent, false); //mInflater=android.view.LayoutInflater.from(mContext) MyViewHolder myViewHolder = new MyViewHolder(view); return myViewHolder; } 不可以使用View.Inflate()方法
RecyclerView的相关用法简介
最新推荐文章于 2024-07-11 00:48:06 发布