如何在Recycler View中将卡片的初始layout_width设置为Match_Parent? [英] How to set initial layout_width of cards in Recycler View to Match_Parent?

查看:214
本文介绍了如何在Recycler View中将卡片的初始layout_width设置为Match_Parent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在回收者视图中呈现项目时遇到问题.问题发生在最初加载的项目上,如您在下面的图片中所见,布局宽度没有变为屏幕宽度(match_parent).

I am facing issue with rendering items in recycler view. The problem happens with the items that are initially loaded as you can see in the pic below that layout width doesn't become screen wide(match_parent).

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

Now when I scroll, new items do span screen wide.

当我向上滚动时,以前的项目也会变成全屏显示.

And when I scroll up, previous items also become screen wide.

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

So it seems that there is some issue with initial loaded cards. Here is the code...

适配器:

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:

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>

我已经提到了这些问题,但是要么它们不能解决我的问题,要么在这些问题中犯的错误与我的不一样.

I already referred these questions but either they don't solve my problem or the mistakes made in these question are not same as mine.

  1. match_parent宽度在RecyclerView中不起作用
  2. CardView layout_width =""match_parent"与父级RecyclerView宽度不匹配
  3. RecyclerView项目没有填充宽度
  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

如果有人对如何使初始加载的项目在全屏范围内有所了解,请告诉我们. 谢谢.

if someone has any idea how to make initially loaded items screen wide please tell. Thank you.

推荐答案

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

Finally found the answer after hours of digging...

只需替换:

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

尽管我不知道为什么会这样. 因此,如果有人知道他/她可能会为此发布答案的理由,那么我会将此答案标记为已接受答案,因为这只是解决方法.

Though I don't have any idea why it happened. So If anyone knows the reasoning he/she may post an answer for this and I will mark that as accepted answer as this one is just workaround.

这篇关于如何在Recycler View中将卡片的初始layout_width设置为Match_Parent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆