自定义布局的RecycleView需要自己实现Adapter,ViewHolder和布局;
自定义Adapter继承RecycleView.Adapter,重写getItemCount(),onBindViewHolder()和onCreateViewHolder();
自定义ViewHolder继承于RecycleView.ViewHolder;
getItemCount()返回RecycleView中Item的个数,onBindViewHolder()主要实现数据和布局的绑定,onCreateViewHolder()加载布局;
效果:
适配器代码:
package com.cloud.recycleviewdemo; import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView; import java.util.List; class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ItemHolder> { private List<Item> mItems; RecycleViewAdapter(List<Item> items) {
mItems = items;
} @Override
public int getItemCount() {
return mItems.size();
} @Override
public void onBindViewHolder(@NonNull ItemHolder holder, int position) {
//设置Item图片
holder.image.setImageResource(mItems.get(position).getImageResId());
//设置Item文字
holder.title.setText(mItems.get(position).getTitle());
} @NonNull
@Override
public ItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ItemHolder(LayoutInflater
.from(parent.getContext()).inflate(R.layout.item, parent, false));
} class ItemHolder extends RecyclerView.ViewHolder { ImageView image;
TextView title; ItemHolder(View item) {
super(item);
image = item.findViewById(R.id.image_view);
title = item.findViewById(R.id.text_title);
}
}
}
Item类:
package com.cloud.recycleviewdemo; class Item { private int mImageResId;
private String mTitle; Item(int imageResId, String title) {
mImageResId = imageResId;
mTitle = title;
} public int getImageResId() {
return mImageResId;
} public void setImageResId(int imageResId) {
mImageResId = imageResId;
} public String getTitle() {
return mTitle;
} public void setTitle(String title) {
mTitle = title;
}
}
MainActivity代码:
package com.cloud.recycleviewdemo; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity { private RecyclerView mRecyclerView;
private LinearLayoutManager mLinearLayoutManager;
private RecycleViewAdapter mRecycleViewAdapter; private List<Item> mData; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); bindViews();
initData();
//初始化线性布局管理器
mLinearLayoutManager = new LinearLayoutManager(this);
//设置布局管理器
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
//初始化适配器
mRecycleViewAdapter = new RecycleViewAdapter(mData);
//设置适配器
mRecyclerView.setAdapter(mRecycleViewAdapter);
} private void bindViews() {
mRecyclerView = findViewById(R.id.recycle_view);
} private void initData() {
mData = new ArrayList<>();
mData.add(new Item(R.drawable.witcher, "The Witcher 3: Wild Hunt"));
mData.add(new Item(R.drawable.recore, "Recore"));
mData.add(new Item(R.drawable.origins, "Assassin's Creed Origins"));
mData.add(new Item(R.drawable.hunter, "theHunter: Call of the Wild"));
}
}
Item布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:elevation="2dp"
android:background="@drawable/background_item"> <ImageView
android:id="@+id/image_view"
android:contentDescription="@null"
android:layout_width="0dp"
android:layout_height="235dp"
android:layout_margin="6dp"
tools:src="@drawable/witcher"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" /> <TextView
android:id="@+id/text_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#000000"
android:layout_margin="6dp"
android:paddingBottom="4dp"
app:layout_constraintTop_toBottomOf="@+id/image_view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:text="The Witcher 3: Wild Hunt" /> </android.support.constraint.ConstraintLayout>
MainActivity布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"> <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@color/colorPrimary"
android:elevation="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"> <ImageView
android:contentDescription="@null"
android:layout_width="36dp"
android:layout_height="36dp"
android:src="@drawable/icon" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/app_name"
android:textColor="#FFFFFF"
android:textSize="18sp" /> </android.support.v7.widget.Toolbar> <android.support.v7.widget.RecyclerView
android:id="@+id/recycle_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layout_constraintVertical_bias="1.0"> </android.support.v7.widget.RecyclerView> </android.support.constraint.ConstraintLayout>