效果如下:
- 項目目錄結(jié)構(gòu)
- 在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;
/**
* 數(shù)據(jù)集合
*/
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) {
//將數(shù)據(jù)設置到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);
//使用瀑布流布局,第一個參數(shù) spanCount 列數(shù),第二個參數(shù) orentation 排列方向
StaggeredGridLayoutManager recyclerViewLayoutManager =
new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
//線性布局Manager
// LinearLayoutManager recyclerViewLayoutManager = new LinearLayoutManager(this);
// recyclerViewLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//網(wǎng)絡布局Manager
// GridLayoutManager recyclerViewLayoutManager = new GridLayoutManager(this, 3);
//給recyclerView設置LayoutManager
recyclerView.setLayoutManager(recyclerViewLayoutManager);
initData();
BeautyAdapter adapter = new BeautyAdapter(data, this);
//設置adapter
recyclerView.setAdapter(adapter);
}
/**
* 生成一些數(shù)據(jù)添加到集合中
*/
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);
}
}
}