Android 如何实施横向和横向;recyclerview gridlayout中的垂直链类型概念?

Android 如何实施横向和横向;recyclerview gridlayout中的垂直链类型概念?,android,android-recyclerview,grid-layout,Android,Android Recyclerview,Grid Layout,和往常一样,我在这个网站上读了几篇关于这个主题的帖子。如果这真的是重复,我将删除这个问题。这里的大多数情况都是处理方向变化或根据屏幕大小修改固定值。我不担心固定宽度和高度值 我想要实现的是一个6x10的网格,无论屏幕大小如何(方向固定为纵向) 这是我的recyclerview项目,我开始在视图上包装内容,然后尝试匹配父项,两者都没有任何区别。在小屏幕上,它很适合,在大屏幕上它位于中间,但大小相同。 我知道我不能直接在我的recyclerview项目xml中创建链,因为我需要从多个视图中创建一个链

和往常一样,我在这个网站上读了几篇关于这个主题的帖子。如果这真的是重复,我将删除这个问题。这里的大多数情况都是处理方向变化或根据屏幕大小修改固定值。我不担心固定宽度和高度值

我想要实现的是一个6x10的网格,无论屏幕大小如何(方向固定为纵向)

这是我的recyclerview项目,我开始在视图上包装内容,然后尝试匹配父项,两者都没有任何区别。在小屏幕上,它很适合,在大屏幕上它位于中间,但大小相同。

我知道我不能直接在我的recyclerview项目xml中创建链,因为我需要从多个视图中创建一个链。我试图用spread chain和权重1手动设置主布局,然后将布局设置为0dp,运行代码,显然它不起作用

所以,关键问题是,有没有一种方法可以简单地编写项目xml,使其成为动态的,还是我们总是必须创建一个排序子类来生成每个屏幕大小的不同布局

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="2dp">

    <LinearLayout
        android:id="@+id/layout_background"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginEnd="4dp"
        android:layout_marginBottom="3dp"
        android:background="@drawable/level_background"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:padding="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/TV_Level"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center|center_horizontal|center_vertical"
            android:textColor="@color/sub_level_button_text_color" />

        <RatingBar
            android:id="@+id/ratingBar"
            style="@style/Widget.AppCompat.RatingBar.Small"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/StarOutline"
            android:isIndicator="true"
            android:numStars="3"
            android:progressTint="@color/StarOutline"
            android:stepSize="0.5" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

为了进一步帮助这一点,这里是我的适配器

package com.maxcell.sumitup


import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.RatingBar
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView



class LevelsAdaptor(private val onClick:(id:Int,mainLevel:Int,subLevel:Int,stars:Float,bonusGame:Boolean,type:Int)->Unit) : ListAdapter<Levels, LevelsAdaptor.LevelsViewHolder>(LevelsComparator()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LevelsViewHolder {
        val view: View = LayoutInflater.from(parent.context)
            .inflate(R.layout.rv_storymode_items, parent, false)
        return LevelsViewHolder(view)
    }

    override fun onBindViewHolder(holder: LevelsViewHolder, position: Int) {
        val current = getItem(position)
        holder.bind(
            current.id,
            current.mainLevel,
            current.subLevel,
            current.stars,
            current.unlocked,
                current.bonusround,
                current.questiontype
        )
    }


    inner class LevelsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val TVLEVEL: TextView = itemView.findViewById(R.id.TV_Level)
        private val rating: RatingBar = itemView.findViewById(R.id.ratingBar)
        private val layoutBackground: LinearLayout = itemView.findViewById(R.id.layout_background)
        private val myContext = itemView.context
        //private val myContext: Context = sumView.context
        fun bind(ID: Int, ML: Int, SL: Int, Stars: Float, Unlocked: Boolean,BonusRound:Boolean,QuestionType:Int) {
            TVLEVEL.text = ID.toString()
            rating.rating = Stars

            if (Unlocked){
                itemView.isEnabled=true;itemView.isClickable=true;layoutBackground.setBackgroundResource(R.drawable.level_background)
            }else{
                itemView.isEnabled=false;itemView.isClickable=false;layoutBackground.setBackgroundResource(R.drawable.standard_background_deactivated);TVLEVEL.setTextColor(myContext.getColor(R.color.ColorOnDeactivated))
            }
            itemView.setOnClickListener {
                //Set your codes about intent here
                onClick(ID,ML,SL,Stars,BonusRound,QuestionType)
            }
        }

        }
    }

    class LevelsComparator : DiffUtil.ItemCallback<Levels>() {
        override fun areItemsTheSame(oldItem: Levels, newItem: Levels): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: Levels, newItem: Levels): Boolean {
            return oldItem == newItem
        }
    }
package com.maxcell.sumitup
导入android.view.LayoutInflater
导入android.view.view
导入android.view.ViewGroup
导入android.widget.LinearLayout
导入android.widget.RatingBar
导入android.widget.TextView
导入androidx.recyclerview.widget.DiffUtil
导入androidx.recyclerview.widget.ListAdapter
导入androidx.recyclerview.widget.recyclerview
class-LevelsAdaptor(private-val-onClick:(id:Int,mainLevel:Int,subLevel:Int,stars:Float,bonusGame:Boolean,type:Int)->Unit):ListAdapter(LevelsComparator()){
override fun onCreateViewHolder(父级:ViewGroup,viewType:Int):LevelsViewHolder{
val view:view=LayoutInflater.from(parent.context)
.充气(R.layout.rv\u storymode\u项目,父项,false)
返回级别查看文件夹(视图)
}
覆盖BindViewHolder(holder:LevelsViewHolder,position:Int){
val current=getItem(位置)
持有者绑定(
当前的.id,
当前.main级别,
当前。子级,
现在的明星们,
当前未锁定,
当前的bonusround,
当前问题类型
)
}
内部类LevelsViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){
private val TVLEVEL:TextView=itemView.findViewById(R.id.TV_级别)
private val rating:RatingBar=itemviewbyd(R.id.RatingBar)
private val layoutBackground:LinearLayout=itemView.findViewById(R.id.layout_background)
private val myContext=itemView.context
//private val myContext:Context=sumView.Context
有趣的绑定(ID:Int,ML:Int,SL:Int,Stars:Float,Unlocked:Boolean,BonusRound:Boolean,QuestionType:Int){
TVLEVEL.text=ID.toString()
评级=星级
如果(未锁定){
itemView.isEnabled=true;itemView.isClickable=true;LayoutBackgroundResource.setBackgroundResource(R.drawable.level_background)
}否则{
itemView.isEnabled=false;itemView.isClickable=false;LayoutBackgroundResource.setBackgroundResource(R.drawable.standard\u background\u停用);TVLEVEL.setTextColor(myContext.getColor(R.color.ColorOnDeactivated))
}
itemView.setOnClickListener{
//在此处设置有关意图的代码
onClick(ID、ML、SL、Stars、BonusRound、QuestionType)
}
}
}
}
类级别比较程序:DiffUtil.ItemCallback(){
覆盖乐趣项相同(旧项:级别,新项:级别):布尔值{
返回oldItem.id==newItem.id
}
覆盖乐趣内容相同(旧项:级别,新项:级别):布尔值{
返回oldItem==newItem
}
}

如果您将
RecyclerView
宽度设置为
match\u parent
、您的
ConstraintLayout
LinearLayout
TextView
RatingBar
宽度设置为
match\u parent
,则项目将拉伸以填充整个屏幕宽度(假设您使用的是
GridLayoutManager

但是,您会注意到它们不适合垂直显示屏幕,您必须滚动到它们。将“高度”设置为“匹配父项”在这里没有帮助,如果您希望它们精确匹配,您必须计算适配器中的高度

不过,这可能会让某些设备(如屏幕较长的手机)看起来有点奇怪

为了使布局更美观,您可能需要在
ConstraintLayout
上设置以下属性:

android:layout_height="0dp"
app:layout_constraintDimensionRatio="3:1"

如果只有6x10个视图,并且它们都同时显示在屏幕上,那么您真的需要使用回收器视图吗?我实际有600个视图,并且只显示特定阶段/级别的结果。更改级别后,recylcerview会更新。我每次将其限制为60个,但填充视图的数据会因阶段而异。Scre添加enshot是为了显示它在10.1屏幕上的外观好的,我理解。因此我相信您的选择是手动计算每个项目的宽度(在
onBindViewHolder
中)…这不是一个很好的方法,或者…我更喜欢的方法是让一个
RecyclerView
项目实际上是整个阶段的整个6x10网格。因此,recycler视图总共有10个项目,每个项目代表一个阶段。我认为这样做不会对性能产生任何影响,因为所有60个项目都是无论哪种方式都可以立即显示。当然,只是不在XML布局中。您可以从片段/活动中获取视图高度/宽度,并将其作为构造函数参数传递给适配器。这在
onCreateViewHolder中<