如何将 Recycler View 中卡片的初始 layout_width 设置为 Match_Parent?

How to set initial layout_width of cards in Recycler View to Match_Parent?

我在回收站视图中呈现项目时遇到问题。问题发生在最初加载的项目上,如下图所示,布局宽度不会变为屏幕宽度 (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
}

Card_Layout:

    <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. CardView layout_width="match_parent" does not match parent RecyclerView width

如果有人知道如何让最初加载的项目全屏显示,请告诉我。 谢谢。

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

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

只需替换:

<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>

虽然我不知道为什么会这样。 因此,如果有人知道推理 he/she 可能 post 对此的答案,我会将其标记为已接受的答案,因为这只是解决方法。