[置顶] RecyclerView的使用

时间:2021-09-01 05:30:03

RecyclerView可以去替换ListView 和GridView
RecyclerView相比较ListView和GridView,它高度解耦,更加灵活,
自带默认的动画效果,可以自定义多种样式的分割线
可以轻易的是想瀑布流效果,对于拖动比较好实现…..
以上的实现原理是通过设置LayoutManager,ItemDecoration , ItemAnimator实现令人瞠目的效果
LayoutManager是控制器显示方式,有ListView效果,GridView效果,GridView横向滑动,横/竖瀑布流
ItemDecoration是控制item的间距,同时可绘制分隔线
ItemAnimator控制item的增删动画,默认效果已经很不错了
还有个开源库RecyclerViewItemAnimators可以实现跟多效果

开始

<android.support.v7.widget.RecyclerView
android:id="@+id/id_recyclerview"
android:divider="#ffff0000"
android:dividerHeight="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />


<!--item 的布局文件代码-->
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#44ff0000"
android:layout_height="wrap_content" >


<TextView
android:id="@+id/id_num"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="1" />

</FrameLayout>
//这个是设置显示方式
// mRecyclerView.setLayoutManager(new LinearLayoutManager(activity));listview显示
// mRecyclerView.setLayoutManager(new GridLayoutManager(activity,4));//gridview显示
// mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));//gridview行显示,瀑布流必须设置item的高度 向左右滑动
// mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(8, StaggeredGridLayoutManager.HORIZONTAL)); //gridview列显示,瀑布流必须设置item的宽度,否则会出问题



//设置分隔符,自定义样式,默认样式自定义高度颜色
// mRecyclerView.addItemDecoration(new DividerItemDecoration(activity,DividerItemDecoration.VERTICAL_LIST));//默认样式,灰色,1px竖线
// mRecyclerView.addItemDecoration(new DividerItemDecoration(activity,DividerItemDecoration.HORIZONTAL_LIST));默认样式,灰色,1px横线
// mRecyclerView.addItemDecoration(new DividerItemDecoration(activity,DividerItemDecoration.VERTICAL_LIST,R.drawable.divider));//自定义的样式传入一个自定义的样式
// mRecyclerView.addItemDecoration(new DividerItemDecoration(activity,DividerItemDecoration.VERTICAL_LIST,10, Color.RED));可以自定义颜色,自定义宽度
// mRecyclerView.addItemDecoration(new DividerGridItemDecoration(activity,R.drawable.divider));//自定义的样式
// mRecyclerView.addItemDecoration(new DividerGridItemDecoration(activity,10));//第二个参数宽度

//设置动画效果
// mRecyclerView.setItemAnimator(new DefaultItemAnimator());
//重点来了,设置适配器
// mRecyclerView.setAdapter( mAdapter = new HomeAdapter());
//一定样继承RecyclerView.Adapter,并且在泛型中传入holder类,并且 它必须继承于RecyclerView.ViewHolder
class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
activity).inflate(R.layout.item_home, parent,false));
return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.tv.setText(mDatas.get(position));
//我这里是为了显示瀑布流自己生成的随机数,
//设置不同的宽高,我怕随机产生的数过小,效果不明显,对随机数的大小做了限制,
int a = new Random().nextInt(200);
ViewGroup.LayoutParams ab= holder.tv.getLayoutParams();
if (a > 100) {
ab.height=a;
holder.tv.setLayoutParams(ab);
}else {
ab.height=100;
holder.tv.setLayoutParams(ab);
}
Log.e("a",a+"");
}

//用于显示多少条数据
@Override
public int getItemCount() {
return mDatas.size();
}
//这里是模拟添加和移除的动画效果
public void addData(int position) {
mDatas.add(position, "Insert One");
//注意这里面不再用的是adapter.notifyDataSetChanged();
//而是adapter.notifyItemInserted(position);添加到第几个item的地方
notifyItemInserted(position);
}

public void removeData(int position) {
mDatas.remove(position);
//而是adapter.notifyItemRemoved(position);移除第几个item
notifyItemRemoved(position);
}

//这里虽然集成了RecyclerView.ViewHolder,但是,书写的样式跟listview没差别
class MyViewHolder extends RecyclerView.ViewHolder {

TextView tv;

public MyViewHolder(View view) {
super(view);
tv = (TextView) view.findViewById(R.id.id_num);
}
}
}



在Activity中去设置监听
mAdapter.setOnItemClickLitener(new OnItemClickLitener()
{

@Override
public void onItemClick(View view, int position)
{
//点击
Toast.makeText(HomeActivity.this, position + " click",
Toast.LENGTH_SHORT).show();
}

@Override
public void onItemLongClick(View view, int position)
{
//长按
Toast.makeText(HomeActivity.this, position + " long click",
Toast.LENGTH_SHORT).show();
mAdapter.removeData(position);
}
});