问题
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...
Adapter:
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
}
}
Holder:
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
}
}
Fragment calling adapter:
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>
Recycler:
<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>
fragment:
<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.
- match_parent width does not work in RecyclerView
- CardView layout_width="match_parent" does not match parent RecyclerView width
- RecyclerView items don't fill width
if someone has any idea how to make initially loaded items screen wide please tell. Thank you.
回答1:
Finally found the answer after hours of digging...
Just replace:
<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>
by:
<?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.
回答2:
Try to put a view between the TextView and Button and set its width to match_parent
来源:https://stackoverflow.com/questions/49463664/how-to-set-initial-layout-width-of-cards-in-recycler-view-to-match-parent