AndroidStudio中GlideImageView元素之间的间隔问题
创始人
2024-08-20 22:30:05
0

使用RecyclerView和GridLayoutManager布局以及设置ItemDecoration实现ImageView元素之间的间隔。

步骤如下:

  1. 在布局文件中添加RecyclerView:

  1. 在Activity或Fragment中初始化RecyclerView:
RecyclerView recyclerView = findViewById(R.id.recyclerView);
GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(layoutManager);
MyAdapter adapter = new MyAdapter(imageList);
recyclerView.setAdapter(adapter);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, 16, true));

其中,“2”表示列数,即每行显示几个ImageView元素,“16”表示间隔大小,可以根据实际情况设置。

  1. 创建GridSpacingItemDecoration类:
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
 
    private final int spanCount;
    private final int spacing;
    private final 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
            }
        }
    }
}
  1. 创建MyAdapter类并在其中使用Glide加载图片:
public class MyAdapter extends RecyclerView.Adapter {
 
    private final List imageList;
 

相关内容

热门资讯

透视真的!边锋老友麻将有挂(w... 透视真的!边锋老友麻将有挂(wePoke),太坑了确实真的有挂(有挂规律)-哔哩哔哩1、让任何用户在...
终于知道(We辅poker助)... 终于知道(We辅poker助)外挂透明挂辅助工具(俱乐部)德州ai机器人(2020已更新)(哔哩哔哩...
玩家必备科技!边锋斗地主看底牌... 玩家必备科技!边锋斗地主看底牌软件,修改捕鱼概率辅助app,详细有挂(有挂规律)-哔哩哔哩 科技详细...
九分钟了解!红河全民雀神有挂(... 九分钟了解!红河全民雀神有挂(辅助挂)太坑了的确是真的有挂(有挂介绍)-哔哩哔哩;玩家必备必赢加哟《...
一分钟快速了解(AAPOKer... 一分钟快速了解(AAPOKer)外挂透明挂辅助app(线上)德州ai机器人(2023已更新)(哔哩哔...
透视美元局!边锋老友棋牌是有挂... 1、透视美元局!边锋老友棋牌是有挂,wepoker透视脚本链接,详细有挂(有挂技巧)-哔哩哔哩。2、...
透视ai!边锋斗地主透明器(w... 透视ai!边锋斗地主透明器(wePokE),太坑了确实真的有挂(有挂总结)-哔哩哔哩;边锋斗地主透明...
解密关于!雀神广东麻将小程序和... 解密关于!雀神广东麻将小程序和app通用(辅助挂)太坑了果真是真的有挂(有挂教学)-哔哩哔哩需要回顾...
透视安卓版(哈糖大菠萝十三张)... 此外,数据分析德州(哈糖大菠萝十三张)辅助神器app还具备辅助透视行为开挂功能,通过对客户哈糖大菠萝...
每日必备!雀神广东麻雀控制胜率... 每日必备!雀神广东麻雀控制胜率(透视)太坑了果真真的有挂(有挂规律)-哔哩哔哩;一、雀神广东麻雀控制...