【问题标题】:How can I set all the items width are the same in RecyclerView?如何在 RecyclerView 中设置所有项目的宽度相同?
【发布时间】:2020-11-13 02:41:00
【问题描述】:

我想将所有项目的宽度设置为相同。因为视图周围有线条,有些文本很长,而另一些很短。所以,这些物品看起来像楼梯柜或其他东西,但我希望它们看起来干净。 (也许,我将来也需要将高度设置为相同。)

我想设置所有项目中文本最长的宽度和高度。

这三个值我都设置好了,

var maxLength = 0
var maxWidth = 0
var longestP = 0

并且,每当它设置新数据时都会初始化值。

    fun setData(data: ArrayList<AnswerData>?, min: Int?, max: Int?) {
        if (data == null) {
            items = ArrayList()
        } else {
            data.forEach {
                it.isChecked = false
            }
            this.items = data
        }

        this.min = min ?: -1
        this.max = max ?: 30
        maxLength = 0
        maxWidth = 0
        longestP = 0

        notifyDataSetChanged()
    }

这是我认为可行的逻辑。 (但没有)

for( i in 0 until items.size){
    val length = items[i].label!!.length
    if( length > maxLength){
        maxLength = length
        longestP = i
    }
}

if(longestP == position){
    maxWidth = holder.cb!!.measuredWidth
}else{
    resizeView(holder.cb!!, maxWidth, holder.cb!!.measuredHeight)
    notifyItemChanged(position)
}

它是 resizeView()。

    private fun resizeView(view: View, newWidth: Int, newHeight: Int) {
        Log.d("size", "$newWidth, $newHeight")
        val params = view.layoutParams
        params.width = newWidth
        params.height = newHeight
        view.layoutParams = params
    }

有什么问题?

【问题讨论】:

  • 如果要设置固定大小,为什么不在xml文件中设置Height和Width?
  • 我想用最长的文字来适应宽度和高度。而且每次最长的文字都不一样。(不能指望。)@ST
  • 是的。如果可以剪切长文本,可以设置maxLine或maxWidth使其适合。
  • 如果文字不能剪切,可以设置固定宽度,让文字自动滚动显示全文内容(参考:android-er.blogspot.com/2016/08/…)。否则,您可以设置固定宽度,并允许文本延伸到 2-3 行。
  • 不,这与我想要实现的不同。我想将所有项目视图设置为其中最长的项目。这些项目与每个 API 调用不同。它可以是 113dp 或 52dp,或者我不知道。宽度应灵活,同时所有项目必须具有相同的宽度。 @ST

标签: android android-layout android-recyclerview


【解决方案1】:

我更改了适配器中的某些部分,它可以工作。

    var maxLength = 0
    var maxWidth = 0
    var maxHeight = 0
    var longestP = 0
    fun setData(data: ArrayList<AnswerData>?) {
        ...

        maxLength = 0
        maxWidth = 0
        maxHeight = 0
        longestP = 0

        ...
    }
    for( i in 0 until items.size){
        val length = items[i].label!!.length
        if( length > maxLength){
            maxLength = length
            longestP = i
        }
    }

    if(longestP == position){
        maxWidth = holder.tv!!.width
        maxHeight = holder.tv!!.height
        items[position].width = maxWidth
        items[position].height = maxHeight
    }else if(holder.tv!!.width < maxWidth){
        items[position].width = maxWidth
        items[position].height = maxHeight
        resizeView(holder.tv!!, items[position].width, items[position].height)
    }

诀窍是数据模型具有宽度和高度值。并在项目视图大于 0 时设置它们的宽度或高度。

【讨论】:

    【解决方案2】:

    您可以将各个视图的宽度设置为match_parent,将RecyclerView 的宽度设置为wrap_content。这样,所有视图将具有相同的宽度,而不是固定宽度,取决于最长的视图。如果文本太长,TextView 会自动换行到下一行。

    我的代码:

    回收站视图

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        tools:listitem="@layout/single_item" />
    
    

    单品(我用的是 CardView)

    <androidx.cardview.widget.CardView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    

    【讨论】:

    • 你试过了吗?我不会工作。当父视图包裹子视图并且子视图要匹配父视图时...
    • 我试过了。您可以发布您的 XML 代码吗?谢谢。
    • 父视图是RecyclerView,子视图是您自定义的项目布局(例如single_item.xml
    • 你能分享答案中的代码吗?如果父母包裹孩子和孩子match_parent,那么它将相当于只有孩子包裹孩子。当项目具有固定值时,包装将起作用,例如 XXdp 或 wrap_content。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多