Android开发RecyclerView,StaggeredGridLayoutManager瀑布流,CardView的结合使用

释放双眼,带上耳机,听听看~!

加入依赖:

    //使用RecyclerView加入的依赖
compile 'com.android.support:design:25.0.0'
//使用CardView加入的依赖
compile 'com.android.support:cardview-v7:21.0.3'

直接进入正题看效果:

activity_main:
<?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="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
MainActivity:
package com.youli.snackbar;
import android.support.v7.app.AppCompatActivity;
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 Main2Activity extends AppCompatActivity {
private List<Product> data;
private int[] imgs ={
R.mipmap.zzz,R.mipmap.ee,R.mipmap.qq,R.mipmap.zzz,
R.mipmap.ee,R.mipmap.qq,R.mipmap.ww,R.mipmap.ee
,R.mipmap.qq,R.mipmap.zzz,R.mipmap.ee,R.mipmap.qq,
R.mipmap.ww,R.mipmap.ee,R.mipmap.qq,R.mipmap.zzz,
R.mipmap.ee,R.mipmap.qq,R.mipmap.zzz,R.mipmap.ee};
private String[] titles ={
"侠客行","十步杀一人,千里不留行",
"秋菊为什么这么黄","冬梅为什么这么白",
"玫瑰为什么这么红","晓月是个什么鬼",
"如花是个什么花","燕雀是个什么鸟",
"青瓷是个什么瓶","浮萍是个泡沫吗",
"翡翠可以吃吗","红缨是个什么梗",
"踏雪会有脚印的","彩石是用来画画的",
"霓凰会飞吗","鸳鸯需要打","冷月有多冷",
"飘雪飘到哪","采莲我们回家吧",
"紫苑还在等着我"
};
private RecyclerView recyclerView;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initData();
initView();
}
private void initView() {
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
//        mLayoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL); //瀑布流
//        recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
MyAdapter adpter  = new MyAdapter(data,Main2Activity.this,R.layout.item_card);
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);
}
}
}
MyAdapter:
package com.youli.snackbar;
/**
* Created by liutao on 2018/9/11.
*/
import android.support.v7.widget.RecyclerView;
import android.content.Context;
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;
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);
}
}
}

adapter 佈局:

item_card:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp">
<LinearLayout
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:layout_margin="10dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"/>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
实体类Product:
package com.youli.snackbar;
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;
}
}

简单的没话说,大家应该都能看的懂

人已赞赏
Android文章

Android通知控件SnackBar解析

2020-3-12 16:34:46

Android文章

android EditText软键盘的隐藏和显示

2020-3-12 18:34:51

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索