RecyclerView采用StaggeredGridLayoutManager布局的瀑布流效果

参考网友的文章,自己动手试试:

布局文件:

activity_main.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:background="#060000"
    android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>

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="wrap_content"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"/>
    <TextView
        android:id="@+id/title"
        android:textColor="#fff"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"/>
</LinearLayout>

实体类:Product.java

package com.example.tes.recyclerviews;
 
/**
 * Created by no1 on 2016/7/14.
 */
public class Product {
    private int img;
    private String title;
 
    public Product(int img, String title) {
        this.img = img;
        this.title = title;
    }
 
    public int getImg() {
        return img;
    }
 
    public void setImg(int img) {
        this.img = img;
    }
 
    public String getTitle() {
        return title;
    }
 
    public void setTitle(String title) {
        this.title = title;
    }
}

适配器:MyAdapter.java

package com.example.tes.recyclerviews;
 
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
 
import java.util.List;
 
/**
 * Created by no1 on 2016/7/14.
 */
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {
    private List<Product> data;
    private Context context;
    private int resouce_id;
 
 
    public MyAdapter(List<Product> data, Context context, int resouce_id) {
        this.data = data;
        this.context = context;
        this.resouce_id = resouce_id;
    }
 
 
    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(resouce_id,null);
        MyHolder myHolder = new MyHolder(view);
        return myHolder;
    }
 
    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        holder.img.setImageResource(data.get(position).getImg());
        holder.title.setText(data.get(position).getTitle());
    }
 
    @Override
    public int getItemCount() {
        return data.size();
    }
 
    public class MyHolder extends ViewHolder {
        private TextView title;
        private ImageView img;
 
        public MyHolder(View view) {
            super(view);
            img = (ImageView) view.findViewById(R.id.img);
            title = (TextView) view.findViewById(R.id.title);
        }
    }
}

主活动:MainActivity.java

package com.example.tes.recyclerviews;
 
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
 
import java.util.ArrayList;
import java.util.List;
 
public class MainActivity extends Activity {
    private List<Product> data;
    private int[] imgs ={
            R.drawable.pp0,R.drawable.pp1,R.drawable.pp2,R.drawable.pp3,R.drawable.pp4,R.drawable.pp5,R.drawable.pp6,R.drawable.pp7,R.drawable.pp8,R.drawable.pp9,
            R.drawable.pp10,R.drawable.pp11,R.drawable.pp12,R.drawable.pp13,R.drawable.pp14,R.drawable.pp15,R.drawable.pp16,R.drawable.pp17,R.drawable.pp18,R.drawable.pp19
    };
    private String[] titles ={
           "春雪","夏雨","秋菊","冬梅","玫瑰","晓月","如花","燕雀","青瓷","浮萍",
            "翡翠","红缨","踏雪","彩石","霓凰","鸳鸯","冷月","飘雪","采莲","紫苑"
    };
    private RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initView();
    }
 
    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
        MyAdapter adpter  = new MyAdapter(data,MainActivity.this,R.layout.item);
        recyclerView.setAdapter(adpter);
        SpacesItemDecoration decoration=new SpacesItemDecoration(16);
        recyclerView.addItemDecoration(decoration);
    }
 
 
    private void initData() {
        data = new ArrayList<Product>();
        for(int i=0;i<imgs.length;i++){
            Product product = new Product(imgs[i],titles[i]);
            data.add(product);
        }
    }
}

分割线类:SpaceItemDecoration.java

package com.example.tes.recyclerviews;
 
/**
 * Created by no1 on 2016/7/14.
 */
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;
 
public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
 
    private int space;
 
    public SpacesItemDecoration(int space) {
        this.space=space;
    }
 
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.left=space;
        outRect.right=space;
        outRect.bottom=space;
        if(parent.getChildAdapterPosition(view)==0){
            outRect.top=space;
        }
    }
}

原文:https://blog.csdn.net/zhangfei8625/article/details/51910825

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值