在研究您的问题时,我学到了一些新东西:我碰巧查看了 RecyclerView 的 GridLayoutManager,我注意到您可以设置自定义 SpanSizeLookup。现在,如果您垂直滚动,“跨度”只是一列,如果您水平滚动,则只是一行。因此 SpanSizeLookup 允许您指定例如,项目 0 占用 2 列,项目 1 占用 1 列,等等。
事实证明,如果您将 RecyclerView 与 GridLayoutManager 一起使用,则解决方案很简单:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create a grid layout with two columns
GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
// Create a custom SpanSizeLookup where the first item spans both
columns
layoutManager.setSpanSizeLookup(new
GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return position == 0 ? 2 : 1;
}
});
RecyclerView recyclerView = (RecyclerView)
findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(new MyGridAdapter());
}
关于适配器的说明:RecyclerView.Adapter 与 ListAdapter 不兼容。您需要从 RecyclerView.Adapter 扩展您的适配器并进行适当的更改。
这是我为我的测试项目创建的适配器:
public static class MyViewHolder extends RecyclerView.ViewHolder {
ImageView mImageView;
TextView mTextView;
public MyViewHolder(View itemView) {
super(itemView);
mImageView = (ImageView) itemView.findViewById(R.id.imageView);
mTextView = (TextView) itemView.findViewById(R.id.textView);
}
}
public static class MyGridAdapter extends
RecyclerView.Adapter<MyViewHolder> {
private int[] mDrawables;
public MyGridAdapter() {
this.mDrawables = new int[] {
R.drawable.images_01,
R.drawable.images_02,
.
.
.
};
}
@Override
public int getItemCount() {
return mDrawables.length;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
LayoutInflater inflater = (LayoutInflater)
parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layout_grid_item, parent,
false);
MyViewHolder holder = new MyViewHolder(view);
// set up any onClickListener you need on the view here
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.mImageView.setImageResource(mDrawables[position]);
holder.mTextView.setText("Image " + position);
}
}
Here xml of Activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.syneotek.gridlayoutmanagerdemo.Demo2">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerview">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
这里是 Grid item_list 的 xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="@+id/grid_item_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" >
</ImageView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text1"
android:layout_gravity="bottom"
android:gravity="center"
android:textStyle="bold"
android:textAppearance="?android:textAppearanceMedium"
android:text="Munaf Patel"/>
</LinearLayout>
Best of Luck.