美文网首页
使用RecyclerView实现瀑布流

使用RecyclerView实现瀑布流

作者: 这个杀手不太累 | 来源:发表于2017-09-06 16:26 被阅读8127次

效果如下:

jdfw.gif
  • 项目目录结构
11111.png
  • 在build.gradle文件中加入recyclerview和cardview的依赖
  compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
  compile 'com.android.support:cardview-v7:26.0.0-alpha1'
  • 对应Java Bean
public class Beauty {
    /**
     * 名字
     */
    private String name;
    /**
     * 图片id
     */
    private int imageId;

    public Beauty(String name, int imageId) {
        this.name = name;
        this.imageId = imageId;
    }

    public String getName() {
        return name;
    }

    public int getImageId() {
        return imageId;
    }
}
  • RecyclerView Item布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:padding="4dp"
    app:cardBackgroundColor="#ffffffff"
    app:cardCornerRadius="15dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/image_item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop" />

        <TextView
            android:id="@+id/name_item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</android.support.v7.widget.CardView>
  • BeautyAdapter 继承自RecyclerView.Adapter<>
public class BeautyAdapter extends RecyclerView.Adapter<BeautyAdapter.BeautyViewHolder> {
    /**
     * 上下文
     */
    private Context mContext;
    /**
     * 数据集合
     */
    private List<Beauty> data;

    public BeautyAdapter(List<Beauty> data, Context context) {
        this.data = data;
        this.mContext = context;
    }

    @Override
    public BeautyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //加载item 布局文件
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.beauty_item, parent, false);
        return new BeautyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(BeautyViewHolder holder, int position) {
        //将数据设置到item上
        Beauty beauty = data.get(position);
        holder.beautyImage.setImageResource(beauty.getImageId());
        holder.nameTv.setText(beauty.getName());
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    static class BeautyViewHolder extends RecyclerView.ViewHolder {
        ImageView beautyImage;
        TextView nameTv;

        public BeautyViewHolder(View itemView) {
            super(itemView);
            beautyImage = itemView.findViewById(R.id.image_item);
            nameTv = itemView.findViewById(R.id.name_item);
        }
    }
}
  • MainActivity 布局文件(只有一个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"
    android:layout_height="match_parent"
    android:background="#88ffffff"
    tools:context="com.example.recyclerviewdemo.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>
  • MainActivity代码
public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private List<Beauty> data = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        //使用瀑布流布局,第一个参数 spanCount 列数,第二个参数 orentation 排列方向
        StaggeredGridLayoutManager recyclerViewLayoutManager = 
                new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        //线性布局Manager
//        LinearLayoutManager recyclerViewLayoutManager = new LinearLayoutManager(this);
//        recyclerViewLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //网络布局Manager
//        GridLayoutManager recyclerViewLayoutManager = new GridLayoutManager(this, 3);
        //给recyclerView设置LayoutManager
        recyclerView.setLayoutManager(recyclerViewLayoutManager);
        initData();
        BeautyAdapter adapter = new BeautyAdapter(data, this);
        //设置adapter
        recyclerView.setAdapter(adapter);
    }

    /**
     * 生成一些数据添加到集合中
     */
    private void initData() {
        Beauty beauty;
        for (int i = 0; i < 10; i++) {
            beauty = new Beauty("美女" + i, R.mipmap.mm1);
            data.add(beauty);
            beauty = new Beauty("美女" + i, R.mipmap.mm2);
            data.add(beauty);
            beauty = new Beauty("美女" + i, R.mipmap.mm3);
            data.add(beauty);
            beauty = new Beauty("美女" + i, R.mipmap.mm4);
            data.add(beauty);
            beauty = new Beauty("美女" + i, R.mipmap.mm5);
            data.add(beauty);
            beauty = new Beauty("美女" + i, R.mipmap.mm6);
            data.add(beauty);
            beauty = new Beauty("美女" + i, R.mipmap.mm7);
            data.add(beauty);
            beauty = new Beauty("美女" + i, R.mipmap.mm8);
            data.add(beauty);
            beauty = new Beauty("美女" + i, R.mipmap.mm9);
            data.add(beauty);
            beauty = new Beauty("美女" + i, R.mipmap.mm10);
            data.add(beauty);
        }
    }
}

相关文章

网友评论

      本文标题:使用RecyclerView实现瀑布流

      本文链接:https://www.haomeiwen.com/subject/kukvjxtx.html