如上图所示,此页面包含两个RecyclerView的嵌套实现,外层为瀑布流布局,里层为线性布局。
public class ChildAdapter extends RecyclerView.Adapter<ChildAdapter.ChildViewHolder> {
private List<String> mBooks;
private int mLayoutId;
public ChildAdapter(List<String> list, int layoutId) {
mBooks = list;
mLayoutId = layoutId;
}
@NonNull
@Override
public ChildViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(mLayoutId, viewGroup, false);
return new ChildViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ChildViewHolder childViewHolder, int i) {
childViewHolder.textView.setText(mBooks.get(i));
}
@Override
public int getItemCount() {
return mBooks.size();
}
static class ChildViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public ChildViewHolder(View view) {
super(view);
textView = (TextView) view.findViewById(R.id.text_book);
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text_book"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
LinearLayout>
public class BookShelf {
private String name;
private List<String> books;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setBooks(List<String> books) {
this.books = books;
}
public List<String> getBooks() {
return books;
}
}
在外层RecyclerView的Adapter中绑定里层RecyclerView与ChildAdapter的关系。
public class ParentAdapter extends RecyclerView.Adapter<ParentAdapter.ParentViewHolder> {
private List<BookShelf> mDatas;
private int mLayoutId;
private Context mContext;
public ParentAdapter(List<BookShelf> bookList, int layoutId, Context context) {
mDatas = bookList;
mLayoutId = layoutId;
mContext = context;
}
@NonNull
@Override
public ParentViewHolder onCreateViewHolder(@NonNull final ViewGroup viewGroup, final int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(mLayoutId, viewGroup, false);
return new ParentViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ParentViewHolder viewHolder, int i) {
BookShelf bookShelf = mDatas.get(i);
viewHolder.textView.setText(bookShelf.getName());
// 子RecyclerView
LinearLayoutManager manager = new LinearLayoutManager(mContext);
manager.setOrientation(LinearLayoutManager.VERTICAL);
viewHolder.recyclerView.setLayoutManager(manager);
viewHolder.recyclerView.setAdapter(new ChildAdapter(bookShelf.getBooks(), R.layout.layout_item_book));
}
@Override
public int getItemCount() {
return mDatas.size();
}
static class ParentViewHolder extends RecyclerView.ViewHolder {
TextView textView;
RecyclerView recyclerView;
public ParentViewHolder(View view) {
super(view);
textView = (TextView) view.findViewById(R.id.text_view);
recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view_parent);
}
}
}
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#ccffff"
android:layout_margin="5dp"
app:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/text_view"
android:textStyle="bold"
android:textSize="20sp"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_parent"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
LinearLayout>
android.support.v7.widget.CardView>
public class MainActivity extends AppCompatActivity {
private List<BookShelf> mDatas = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
initData();
StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
ParentAdapter adapter = new ParentAdapter(mDatas, R.layout.layout_staggered_grid_nest_adapter_item, this);
recyclerView.setAdapter(adapter);
}
// 组装数据
private void initData() {
mDatas.clear();
BookShelf bookShelf1 = new BookShelf();
bookShelf1.setName("科幻类");
String[] books1 = new String[]{"三体", "流浪地球", "降临"};
bookShelf1.setBooks(Arrays.asList(books1));
mDatas.add(bookShelf1);
BookShelf bookShelf2 = new BookShelf();
bookShelf2.setName("政治类");
String[] books2 = new String[]{"美国陷阱", "从赫鲁晓夫到普京", "为什么是以色列", "南京大屠杀"};
bookShelf2.setBooks(Arrays.asList(books2));
mDatas.add(bookShelf2);
BookShelf bookShelf3 = new BookShelf();
bookShelf3.setName("文学类");
String[] books3 = new String[]{"我们仨", "小姨多鹤", "我与地坛", "黄金时代", "雪国"};
bookShelf3.setBooks(Arrays.asList(books3));
mDatas.add(bookShelf3);
BookShelf bookShelf4 = new BookShelf();
bookShelf4.setName("医学类");
String[] books4 = new String[]{"只有医生知道", "基因传", "癌症传"};
bookShelf4.setBooks(Arrays.asList(books4));
mDatas.add(bookShelf4);
BookShelf bookShelf5 = new BookShelf();
bookShelf5.setName("财经类");
String[] books5 = new String[]{"小狗钱钱", "激荡四十年", "巴菲特和他的财富人生"};
bookShelf5.setBooks(Arrays.asList(books5));
mDatas.add(bookShelf5);
BookShelf bookShelf6 = new BookShelf();
bookShelf6.setName("悬疑类");
String[] books6 = new String[]{"白夜行", "东方列车谋杀案", "心理罪"};
bookShelf6.setBooks(Arrays.asList(books6));
mDatas.add(bookShelf6);
BookShelf bookShelf7 = new BookShelf();
bookShelf7.setName("技术类");
String[] books7 = new String[]{"android进阶之光", "Kotlin实战", "黑客与画家", "程序是怎样跑起来的"};
bookShelf7.setBooks(Arrays.asList(books7));
mDatas.add(bookShelf7);
}
}
<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="#ccffff"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:clipToPadding="false"
android:paddingBottom="50dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
RelativeLayout>
可在这里下载双层RecyclerView嵌套实现的demo