Android Recycleview的用法

前言

最近没得什么项目可以做,闲来无事想写一些东西,看了看以前做的项目,用到的最多的就是列表了,今天就说一下recycleview。

这个世界并不是掌握在那些嘲笑者的手中,而恰恰掌握在能够经受得住嘲笑与批评仍不断往前走的人手中。


RecycleView 的介绍

自Android 5.0之后,谷歌公司推出了RecylerView控件,主要用于列表的展示是一个很强大的滑动组件,说到这里就会想到不是已经有listview吗?为啥还要弄一个recycleview出来了?确实我觉得这个问题回答很简单,之所以出他是因为他肯定比listview强大的,不然谷歌为啥要出它。

recycleview与listview的比较
相同:都可以实现垂直方向的滚动列表效果;都需要使用适配器(Adapter)。
不同点:ListView只能实现垂直滚动列表,但RecyclerView还可以实现水平、多列、跨列等复杂的滚动列表;RecyclerView不但需要Adapter,还必须有LayoutManager,用法更复杂一些。

recycleview说:你listview管不了的我要管,你listview管的了的我也要管,我recycleview说的,listview我吃定了,耶稣也留不住。
listview说:牛bi!


线性布局的Recycleview

效果图:
这里写图片描述

先来看看XML的布局:

 <android.support.v7.widget.RecyclerView
        android:id="@+id/xian_recycle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp">
        </android.support.v7.widget.RecyclerView>

再来看看每一个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="wrap_content"
    android:layout_marginTop="5dp"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/img"
            android:layout_width="150dp"
            android:layout_height="match_parent" />

            <TextView
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:textColor="#363535"
                android:textSize="14sp" />

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#cfcdcd"></View>

</LinearLayout>

adapter的写法

public class XianAdapter extends RecyclerView.Adapter<XianAdapter.XianHolder> {
    private Context context = null;
    //数据集合
    private ArrayList<Name> list = null;
    //点击事件的接口
    private ItemClick click = null;

    public XianAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    public ItemClick getClick() {
        return click;
    }

    public void setClick(ItemClick click) {
        this.click = click;
    }

    public ArrayList<Name> getList() {
        return list;
    }

    public void setList(ArrayList<Name> list) {
        this.list = list;
    }

    @Override
    public XianHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    //添加item的布局文件
        View view = LayoutInflater.from(context).inflate(R.layout.xian_recycle_item, parent, false);
        XianHolder holder = new XianHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(XianHolder holder, final int position) {
        //给每个item设置数据
        if (list != null) {
            holder.img.setImageResource(list.get(position).getImg());
            holder.name.setText(list.get(position).getName());
        }
        //给每个item添加监听
        if (click != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    click.ItemClick(position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        //列表的长度
        return list.size();
    }

    class XianHolder extends RecyclerView.ViewHolder {
        //找到布局文件里面的控件
        private ImageView img = null;
        private TextView name = null;

        public XianHolder(View itemView) {
            super(itemView);
            img = (ImageView) itemView.findViewById(R.id.img);
            name = (TextView) itemView.findViewById(R.id.name);
        }
    }
}

这个地方我做了一个数据的类Name

public class Name {
    private String name = null;
    private int img = 0;


    public Name(String name, int img) {
        this.name = name;
        this.img = img;

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getImg() {
        return img;
    }

    public void setImg(int img) {
        this.img = img;
    }
}

还有一个接口文件

public interface ItemClick {
    void ItemClick(int position);
}

再来看看主页面是怎么写的:

public class XianActivity extends AppCompatActivity{
    private RecyclerView recyclerView = null;
    private XianAdapter adapter = null;
    private ArrayList<Name> list = null;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        initData();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xian);
        initView();
    }

    public void initView(){
        //找到recycleview
        recyclerView = (RecyclerView) findViewById(R.id.xian_recycle);
        //指定布局方式这个地方时线性布局
        RecyclerView.LayoutManager manager = new LinearLayoutManager(this);
        //指定好的设置给recycleview
        recyclerView.setLayoutManager(manager);
        //适配器弄出来
        adapter = new XianAdapter(this);
        //把添加好数据的结合传给适配器
        adapter.setList(list);
        //监听也是一样
        adapter.setClick(click);
        //recycleview设置适配器
        recyclerView.setAdapter(adapter);
        //adapter更新数据
        adapter.notifyDataSetChanged();
    }

    //item的监听事件
    private ItemClick click = new ItemClick() {
        @Override
        public void ItemClick(int position) {
        //点击弹出每一行的名字
            Toast.makeText(XianActivity.this, list.get(position).getName(), Toast.LENGTH_SHORT).show();
        }
    };
    //插入数据
    public void initData(){
        list = new ArrayList<>();
        list.add(new Name(" 蒙奇·D·路飞",R.mipmap.one));
        list.add(new Name(" 罗罗亚·索隆 ",R.mipmap.two));
        list.add(new Name("香吉士",R.mipmap.three));
        list.add(new Name(" 妮可·罗宾",R.mipmap.four));
        list.add(new Name("娜美",R.mipmap.five));
        list.add(new Name("乌索普",R.mipmap.six));
        list.add(new Name("托尼托尼·乔巴",R.mipmap.seven));
        list.add(new Name("弗兰奇",R.mipmap.eight));
        list.add(new Name("布鲁克 ",R.mipmap.nine));
    }
}
recycleview网格布局

效果图:
这里写图片描述

要实现这种网格的效果你只需要稍微的改动一下item的布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_margin="5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:scaleType="fitXY"
        android:id="@+id/img"
        android:layout_width="150dp"
        android:layout_height="100dp" />

    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#363535"
        android:textSize="14sp" />
</LinearLayout>

然后改一句话就能实现
把线性布局的:

RecyclerView.LayoutManager manager = new LinearLayoutManager(this);

改成

RecyclerView.LayoutManager manager = new GridLayoutManager(this, 3);

这句话就是用网格布局3表示的一行有多少个。

Recycleview 瀑布流

效果图:
这里写图片描述

改下item布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#ffffff"
    android:layout_margin="5dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:scaleType="fitXY"
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#363535"
        android:textSize="14sp" />

</LinearLayout>

然后改一句话就能实现
把线性布局的:

RecyclerView.LayoutManager manager = new LinearLayoutManager(this);

改成

RecyclerView.LayoutManager manager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);

这样就搞定了瀑布流。

这篇就写到这里了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值