Android RecyclerView实现九宫格横向左右滑动
时间: 2023-11-16 15:16:15 浏览: 286
实现九宫格横向左右滑动的功能,可以使用 RecyclerView 和 GridLayoutManager 来实现。以下是具体步骤:
1. 在布局文件中,添加 RecyclerView 控件:
```xml
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="horizontal" />
```
2. 在代码中,初始化 RecyclerView 和 GridLayoutManager,并设置相关属性:
```java
RecyclerView recyclerView = findViewById(R.id.recycler_view);
GridLayoutManager layoutManager = new GridLayoutManager(this, 3); // 设置列数为3
layoutManager.setOrientation(RecyclerView.HORIZONTAL); // 设置水平滚动
recyclerView.setLayoutManager(layoutManager);
int spacing = getResources().getDimensionPixelSize(R.dimen.grid_spacing); // 设置item之间的间距
recyclerView.addItemDecoration(new GridSpacingItemDecoration(3, spacing, true)); // 通过自定义ItemDecoration来实现
```
其中,GridSpacingItemDecoration 是自定义的一个 RecyclerView.ItemDecoration,用来设置 item 之间的间距,具体实现如下:
```java
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
private int spanCount;
private int spacing;
private boolean includeEdge;
public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
this.spanCount = spanCount;
this.spacing = spacing;
this.includeEdge = includeEdge;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view); // item position
int column = position % spanCount; // item column
if (includeEdge) {
outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)
if (position < spanCount) { // top edge
outRect.top = spacing;
}
outRect.bottom = spacing; // item bottom
} else {
outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing)
if (position >= spanCount) {
outRect.top = spacing; // item top
}
}
}
}
```
3. 创建 RecyclerView.Adapter 和 RecyclerView.ViewHolder,并在 onBindViewHolder() 方法中设置 item 的内容:
```java
public class GridAdapter extends RecyclerView.Adapter<GridAdapter.GridViewHolder> {
private List<Integer> dataList; // 数据源
public GridAdapter(List<Integer> dataList) {
this.dataList = dataList;
}
@NonNull
@Override
public GridViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_grid, parent, false);
return new GridViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull GridViewHolder holder, int position) {
holder.imageView.setImageResource(dataList.get(position)); // 设置图片资源
}
@Override
public int getItemCount() {
return dataList.size();
}
static class GridViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
GridViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.image_view);
}
}
}
```
4. 最后在 Activity 中设置 RecyclerView 的 adapter:
```java
List<Integer> dataList = new ArrayList<>(); // 数据源
dataList.add(R.drawable.pic1);
dataList.add(R.drawable.pic2);
dataList.add(R.drawable.pic3);
dataList.add(R.drawable.pic4);
dataList.add(R.drawable.pic5);
dataList.add(R.drawable.pic6);
dataList.add(R.drawable.pic7);
dataList.add(R.drawable.pic8);
dataList.add(R.drawable.pic9);
GridAdapter adapter = new GridAdapter(dataList);
recyclerView.setAdapter(adapter);
```
这样就可以实现九宫格横向左右滑动的效果了。
阅读全文