Fresco和 RecyclerView 实现瀑布流 <图片是用本地图片来模拟网络图片的>

参考:http://blog.csdn.net/Rodulf/article/details/50768220

RecyclerView 实现瀑布流非常的简单,只需要设置布局管理为 StaggeredGridLayoutManager。然后还需要设置ItemDecorator

代码:瀑布流代码模拟网络图片

图片:瀑布流的图片


1:大概的步骤如下.具体的步骤在下面的下面.

0:导入RecyclerView,Fresco

1:布局里面添加RecyclerView,

2:创建item的布局,这个布局可以使用Fresco的SimpleDrawweView,记住要在Application里面进行初始化。

3:创建Product的实体类,这个里面包含了四个变量,图片地址,图片名称,图片的宽度,图片的高度。

4:创建 Adatper, PicRecyclerViewAdapter,在这个PicRecyclerViewAdapter里面首先要创建PicViewHolder继承RecyclerView.ViewHolder,这个ViewHolder必须要先创建,因为下面继承RecyclerView.Adapter里面要用到
5:让我们的adpter,PicRecyclerViewAdapter继承RecyclerView.Adatper, extends RecyclerView.Adapter<PicRecyclerViewAdapter.PicViewHolder>
继承里面的方法,包括onCreateViewHolder,和onBindViewHolder。

public PicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View ret = LayoutInflater.from(context).inflate(R.layout.item_recyclerview,parent,false);
        return new PicViewHolder(ret);
    }

//瀑布流里面,我们会从服务器得到宽度和高度,然后通过这个设置宽高比。
    @Override
    public void onBindViewHolder(PicViewHolder holder, int position) {
        Product product = list.get(position);
        holder.pic.setImageURI(Uri.parse(product.getLocalPosition()));
        holder.textView.setText(product.getProductName());
    }

6:创建每一个Item之间的间隔 通过创建一个继承了ItemDecorator的类来做,里面有getItemOffSet方法。来设置上下左右的距离

7:通过代码设置布局管理和adapter,以及一些模拟数据。
        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        //添加间隔,Decorator 的翻译就是装饰师,粉刷匠
        MyItemDecorator myItemDecorator = new MyItemDecorator(10);
        mRecyclerView.addItemDecoration(myItemDecorator);
        mRecyclerView.setAdapter(adapter);


2:具体的步骤:

0:导入RecyclerView,Fresco

1:布局里面添加RecyclerView,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recyclerview"></android.support.v7.widget.RecyclerView>
</RelativeLayout>

2:创建item的布局,这个布局可以使用Fresco的SimpleDrawweView,记住要在Application里面进行初始化。

千万记住了SimpleDraweeView不能是全部的wrap_content

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:fresco
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值