【问题标题】:How to set initial layout_width of cards in Recycler View to Match_Parent?如何将 Recycler View 中卡片的初始 layout_width 设置为 Match_Parent?
【发布时间】:2018-09-02 22:58:14
【问题描述】:

我在回收站视图中呈现项目时遇到问题。问题发生在最初加载的项目上,如下图所示,布局宽度不会变成屏幕宽度(match_parent)。

现在,当我滚动时,新项目会跨越整个屏幕。

当我向上滚动时,以前的项目也会变成屏幕宽度。

因此,初始加载的卡似乎存在一些问题。这是代码...

适配器:

class SearchAdapter(searchInp : ArrayList<BusNumbers>) : RecyclerView.Adapter<SearchViewHolder>() {

    var searches : ArrayList<BusNumbers>? = searchInp

    override fun onBindViewHolder(holder: SearchViewHolder?, position: Int) {
        var bus : BusNumbers = searches!!.get(position)
        holder!!.updateUI(bus)
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): SearchViewHolder {
        var cardBuses : View = LayoutInflater.from(parent!!.context).inflate(R.layout.card_bus_numbers,parent,false)
        return SearchViewHolder(cardBuses)
    }

    override fun getItemCount(): Int {
        return searches!!.size
    }
}

持有人:

class SearchViewHolder(itemView : View?) : RecyclerView.ViewHolder(itemView) {
    var Title : TextView? = null
    init {
        Title = itemView!!.findViewById(R.id.cardSearchText)
    }

    fun updateUI(bus : BusNumbers){
        Title!!.text = bus.Number
    }
}

片段调用适配器:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    var view = inflater.inflate(R.layout.fragment_search_loader, container, false)
    var recyclerview : RecyclerView = view.findViewById(R.id.recyclerSearch)
    recyclerview.setHasFixedSize(true)

    var adapter  = SearchAdapter(BusNumberData.ourInstance.getBusNumbers())
    recyclerview.adapter = adapter

    var layoutManager = LinearLayoutManager(context)
    layoutManager.orientation = LinearLayoutManager.VERTICAL
    recyclerview.layoutManager = layoutManager

    return view
}

卡片布局:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum="360">

        <TextView
            android:id="@+id/cardSearchText"
            android:layout_width="40dp"
            android:layout_height="match_parent"
            android:layout_weight="320"
            android:gravity="center_vertical"
            android:padding="10dp"
            android:text="TestText"
            android:textSize="24sp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="40" />

    </LinearLayout>


</android.support.v7.widget.CardView>

回收商:

<android.support.v7.widget.RecyclerView android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recyclerSearch"
    xmlns:android="http://schemas.android.com/apk/res/android">

</android.support.v7.widget.RecyclerView>

片段:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mandarsadye.mandar.ess_user.Fragments.SearchPackage.SearchLoader">

    <include layout="@layout/content_search_recycler"/>

</FrameLayout>

我已经提到了这些问题,但是它们不能解决我的问题,或者这些问题中的错误与我的不同。

  1. match_parent width does not work in RecyclerView
  2. CardView layout_width="match_parent" does not match parent RecyclerView width
  3. RecyclerView items don't fill width

如果有人知道如何使初始加载的项目在屏幕上宽屏,请告诉。 谢谢。

【问题讨论】:

  • 你在cardView布局中发布你的顶级cardView! (卡片布局)

标签: android android-recyclerview kotlin android-cardview recyclerview-layout


【解决方案1】:

经过几个小时的挖掘终于找到了答案......

只需替换:

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/textreplaced"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="xyz" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/common_google_signin_btn_icon_dark" />
</android.support.v7.widget.CardView>

作者:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    app:cardBackgroundColor="@color/colorPrimary">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/textreplaced"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="xyz" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/common_google_signin_btn_icon_dark" />
    </android.support.v7.widget.CardView>



</RelativeLayout>

虽然我不知道为什么会这样。 因此,如果有人知道原因,他/她可能会为此发布答案,我会将其标记为已接受的答案,因为这只是解决方法。

【讨论】:

    【解决方案2】:

    尝试在 TextViewButton 之间放置一个 view 并将其宽度设置为 ma​​tch_parent

    【讨论】:

    • 感谢您的回复...但建议在我的情况下不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 2013-05-08
    • 2020-10-28
    • 2021-10-14
    相关资源
    最近更新 更多