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.