【RecyclerView】 四、RecyclerView 布局 ( 网格局管理器 GridLayoutManager )

举报
韩曙亮 发表于 2022/01/11 01:39:15 2022/01/11
【摘要】 文章目录 一、网格局管理器 GridLayoutManager二、网格局管理器默认设置三、网格局管理器水平方向设置四、完整代码示例五、RecyclerView 相关资料 本篇博客主要...



本篇博客主要讨论设置不同的布局管理器 , 以及不同布局管理器的参数设置 , 基础用法参考 【RecyclerView】 一、RecyclerView 最基本用法 ( 添加支持库 | 设置布局文件 | 自定义适配器 ) , 以及 RecyclerView 专栏 ;





一、网格局管理器 GridLayoutManager



使用代码创建 线性布局管理器 GridLayoutManager , 推荐使用

GridLayoutManager (Context context, int spanCount, @RecyclerView.Orientation int orientation, boolean reverseLayout)

构造函数 , 可以使用一行代码设置基本的 GridLayoutManager 参数 ;


参数说明 :

Context context : 当前的上下文对象, 用于获取资源.

int spanCount : 网格布局行或列的个数.

@RecyclerView.Orientation int orientation : 布局方向. 设置成 RecyclerView.VERTICAL 或 RecyclerView.HORIZONTAL.

boolean reverseLayout : 当设置成 true 时, 布局会翻转, 从尾部开始头部结束.


当方向是 RecyclerView.VERTICAL 垂直方向时 , 网格元素排列顺序是逐行排列 , 先将第一行排满 , 然后将第二行排满 , int spanCount 参数设置的是每行的元素个数 ; ( 现代人写字顺序 )

当方向是 RecyclerView.HORIZONTAL 水平方向时 , 网格元素排列顺序是逐列排列 , 先将第一列排满 , 然后将第二列排满 , int spanCount 参数设置的是每列的元素个数 ; ( 古代人写字顺序 )


构造函数原型 :

    /**
     * @param context 当前的上下文对象, 用于获取资源.
     * @param spanCount 网格布局行或列的个数. 
     * @param orientation 布局方向. 设置成 RecyclerView.VERTICAL 或 RecyclerView.HORIZONTAL.
     * @param reverseLayout 当设置成 true 时, 布局会翻转, 从尾部开始头部结束.
     */
    public GridLayoutManager(Context context, int spanCount,
            @RecyclerView.Orientation int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
        setSpanCount(spanCount);
    }

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11




二、网格局管理器默认设置



GridLayoutManager 默认设置是垂直方向 , 不翻转 , 代码如下 :

        //1 . 从布局中获取 RecyclerView
        RecyclerView recycler_view = findViewById(R.id.recycler_view);
        
        //2 . 创建并设置布局管理器
        //创建布局管理器
        GridLayoutManager layoutManager = new GridLayoutManager(
                this,
                3,
                RecyclerView.VERTICAL,
                false);

        //设置布局管理器
        recycler_view.setLayoutManager(layoutManager);

        //3 . 创建并设置列表适配器
        Adapter adapter = new Adapter();
        recycler_view.setAdapter(adapter);

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

展示效果 :

在这里插入图片描述





三、网格局管理器水平方向设置



设置网格布局水平方向 :

        //1 . 从布局中获取 RecyclerView
        RecyclerView recycler_view = findViewById(R.id.recycler_view);

        //2 . 创建并设置布局管理器
        //创建布局管理器
        /*GridLayoutManager layoutManager = new GridLayoutManager(
                this,
                3,
                RecyclerView.VERTICAL,
                false);*/

        GridLayoutManager layoutManager = new GridLayoutManager(
                this,
                3,
                RecyclerView.HORIZONTAL,
                false);

        //设置布局管理器
        recycler_view.setLayoutManager(layoutManager);

        //3 . 创建并设置列表适配器
        Adapter adapter = new Adapter();
        recycler_view.setAdapter(adapter);

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

运行效果 :

在这里插入图片描述





四、完整代码示例



package kim.hsl.recyclerview;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //1 . 从布局中获取 RecyclerView
        RecyclerView recycler_view = findViewById(R.id.recycler_view);

        //2 . 创建并设置布局管理器
        //创建布局管理器
        GridLayoutManager layoutManager = new GridLayoutManager(
                this,
                3,
                RecyclerView.VERTICAL,
                false);

        /*GridLayoutManager layoutManager = new GridLayoutManager(
                this,
                3,
                RecyclerView.HORIZONTAL,
                false);*/

        //设置布局管理器
        recycler_view.setLayoutManager(layoutManager);

        //3 . 创建并设置列表适配器
        Adapter adapter = new Adapter();
        recycler_view.setAdapter(adapter);
    }

    /**
     * RecyclerView 适配器
     */
    public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View root_view = LayoutInflater.from(MainActivity.this)
                    .inflate(R.layout.item_recyclerview, parent, false);
            return new ViewHolder(root_view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            holder.text.setText("" + position);
        }

        @Override
        public int getItemCount() {
            return 10;
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            TextView text;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                text = itemView.findViewById(R.id.text);
            }
        }
    }

}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80




五、RecyclerView 相关资料



官方文档 :

使用 RecyclerView 创建动态列表 : https://developer.android.google.cn/guide/topics/ui/layout/recyclerview

高级 RecyclerView 自定义 : https://developer.android.google.cn/guide/topics/ui/layout/recyclerview-custom


代码示例 :

GitHub 源码地址 : https://github.com/han1202012/001_RecyclerView

博客源码快照 : https://download.csdn.net/download/han1202012/14945904

( 使用 Android Studio 打开 )

文章来源: hanshuliang.blog.csdn.net,作者:韩曙亮,版权归原作者所有,如需转载,请联系作者。

原文链接:hanshuliang.blog.csdn.net/article/details/113242924

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。