首先,您只需要将 2 个 RecyclerViews 对象放入您的活动/片段布局。为了以后更好的滚动,请将主题包装在 NestedScrollView 中,如下所示:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#ffffe0"
tools:context="info.devexchanges.gridlistrecyclerview.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:text="@string/os"
android:textStyle="bold" />
<android.support.v7.widget.RecyclerView
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="@string/corporation"
android:textStyle="bold" />
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
当然,您总是需要为每个 RecyclerView 项目创建布局。在此每个项目都是一个 CardView:item_list.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="5dp"
android:contentDescription="@string/app_name" />
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toRightOf="@+id/image"
android:gravity="center" />
</RelativeLayout>
</android.support.v7.widget.CardView>
item_grid.xml
<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"
app:cardCornerRadius="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:id="@+id/image"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:contentDescription="@null" />
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/image"
android:layout_marginTop="5dp"
android:background="@color/colorPrimaryDark"
android:gravity="center"
android:padding="5dp"
android:textColor="#ffffff"
android:textStyle="bold" />
</RelativeLayout>
</android.support.v7.widget.CardView>
现在,我们必须基于 RecyclerView.Adapter 为 2 个 RecyclerViews 创建 2 个适配器类:
ListViewAdapter.java
import android.app.Activity;
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;
import info.devexchanges.gridlistrecyclerview.R;
import info.devexchanges.gridlistrecyclerview.RecyclerViewItem;
public class ListViewAdapter extends RecyclerView.Adapter<ListViewAdapter.ViewHolder> {
private Activity activity;
private List<RecyclerViewItem> items;
public ListViewAdapter(Activity activity, List<RecyclerViewItem> items) {
this.activity = activity;
this.items = items;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = activity.getLayoutInflater();
View view = inflater.inflate(R.layout.item_list, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
viewHolder.imageView.setImageResource(items.get(position).getDrawableId());
viewHolder.textView.setText(items.get(position).getName());
}
@Override
public int getItemCount() {
return items.size();
}
/**
* View holder to display each RecylerView item
*/
protected class ViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView;
private TextView textView;
public ViewHolder(View view) {
super(view);
imageView = (ImageView) view.findViewById(R.id.image);
textView = (TextView)view.findViewById(R.id.text);
}
}
}
GridViewAdapter.java
import android.app.Activity;
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;
import info.devexchanges.gridlistrecyclerview.R;
import info.devexchanges.gridlistrecyclerview.RecyclerViewItem;
public class GridViewAdapter extends RecyclerView.Adapter<GridViewAdapter.ViewHolder> {
private List<RecyclerViewItem> items;
private Activity activity;
public GridViewAdapter(Activity activity, List<RecyclerViewItem> items) {
this.activity = activity;
this.items = items;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
LayoutInflater inflater = activity.getLayoutInflater();
View view = inflater.inflate(R.layout.item_grid, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(GridViewAdapter.ViewHolder viewHolder, int position) {
viewHolder.imageView.setImageResource(items.get(position).getDrawableId());
viewHolder.textView.setText(items.get(position).getName());
}
@Override
public int getItemCount() {
return items.size();
}
/**
* View holder to display each RecylerView item
*/
protected class ViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView;
private TextView textView;
public ViewHolder(View view) {
super(view);
textView = (TextView)view.findViewById(R.id.text);
imageView = (ImageView) view.findViewById(R.id.image);
}
}
}
在您的活动或片段编程代码中没有特别之处,从布局文件中定位所有 xml 元素,为 RecyclerViews 创建 LayoutManager,初始化适配器并附加它们,......这是我的主要活动的完整代码:
MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import info.devexchanges.gridlistrecyclerview.adapter.GridViewAdapter;
import info.devexchanges.gridlistrecyclerview.adapter.ListViewAdapter;
public class MainActivity extends AppCompatActivity {
private RecyclerView listView;
private RecyclerView gridView;
private ListViewAdapter listViewAdapter;
private GridViewAdapter gridViewAdapter;
private ArrayList<RecyclerViewItem> corporations;
private ArrayList<RecyclerViewItem> operatingSystems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (RecyclerView) findViewById(R.id.list);
gridView = (RecyclerView) findViewById(R.id.grid);
setDummyData();
listView.setHasFixedSize(true);
gridView.setHasFixedSize(true);
//set layout manager and adapter for "ListView"
LinearLayoutManager horizontalManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
listView.setLayoutManager(horizontalManager);
listViewAdapter = new ListViewAdapter(this, corporations);
listView.setAdapter(listViewAdapter);
//set layout manager and adapter for "GridView"
GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
gridView.setLayoutManager(layoutManager);
gridViewAdapter = new GridViewAdapter(this, operatingSystems);
gridView.setAdapter(gridViewAdapter);
}
private void setDummyData() {
corporations = new ArrayList<>();
corporations.add(new RecyclerViewItem(R.drawable.microsoft, "Microsoft"));
corporations.add(new RecyclerViewItem(R.drawable.apple, "Apple"));
corporations.add(new RecyclerViewItem(R.drawable.google, "Google"));
corporations.add(new RecyclerViewItem(R.drawable.oracle, "Oracle"));
corporations.add(new RecyclerViewItem(R.drawable.yahoo, "Yahoo"));
corporations.add(new RecyclerViewItem(R.drawable.mozilla, "Mozilla"));
operatingSystems = new ArrayList<>();
operatingSystems.add(new RecyclerViewItem(R.drawable.bbos, "BlackBerry OS"));
operatingSystems.add(new RecyclerViewItem(R.drawable.ios, "iOS"));
operatingSystems.add(new RecyclerViewItem(R.drawable.tizen, "Tizen"));
operatingSystems.add(new RecyclerViewItem(R.drawable.android, "Android"));
operatingSystems.add(new RecyclerViewItem(R.drawable.symbian, "Symbian"));
operatingSystems.add(new RecyclerViewItem(R.drawable.firefox_os, "Firefox OS"));
operatingSystems.add(new RecyclerViewItem(R.drawable.wp_os, "Windows Phone OS"));
}
}
这是这个项目的POJO类:RecyclerViewItem.java
public class RecyclerViewItem {
private int drawableId;
private String name;
public RecyclerViewItem(int drawableId, String name) {
this.drawableId = drawableId;
this.name = name;
}
public int getDrawableId() {
return drawableId;
}
public String getName() {
return name;
}
}
注意:永远不要忘记将 RecyclerView 和 CardView 依赖项放入您的应用程序级 build.gradle 文件:
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.android.support:cardview-v7:27.1.1'