Android RecyclerView Width wider than Screen - android

I am setting up a RecyclerView with CardViews. However, the CardViews are extending to the right side of the screen, despite all constraints.
Here is my code:
////////////////////////////////////////////////////////
Adapter:
class ExpertsRecyclerAdapter: RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var expertsData: List<Map<String, Any>> = ArrayList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val inflater = LayoutInflater.from(parent.context)
val v = inflater.inflate(R.layout.layout_experts_item, parent, false)
return ExpertsViewHolder(v)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder) {
is ExpertsViewHolder -> {
// holder.setup(expertsData[position])
}
}
}
override fun getItemCount(): Int {
return 10
}
fun submitList (data: List<Map<String, Any>> ) {
expertsData = data
}
inner class ExpertsViewHolder constructor(expertsView: View): RecyclerView.ViewHolder(expertsView) {
fun setup(data: Map<String, Any>) {
}
}
}
///////////////////////////////////////////////////////////////////////
Fragment Containing RecyclerView
class ExpertsFragment : Fragment() {
private lateinit var expertsAdapter: ExpertsRecyclerAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_experts, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
initRecyclerView()
}
private fun initRecyclerView () {
experts_recycler_view.hasFixedSize()
experts_recycler_view.layoutManager = LinearLayoutManager(context)
experts_recycler_view.adapter = ExpertsRecyclerAdapter()
experts_recycler_view.itemAnimator = DefaultItemAnimator()
}
}
//////////////////////////////////////////////////////
XML for RecyclerView Item
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/secondaryGray"
android:orientation="vertical"
android:padding="10dp">
<androidx.cardview.widget.CardView
android:id="#+id/corner"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
app:cardCornerRadius="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
/>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
/////////////////////////////////////////////////////
XML for RecyclerView Fragment
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/secondaryGray"
android:layout_alignParentRight="true"
tools:context=".ExpertsFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/experts_recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Try this simple remove ConstraintLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rv_xxx"/>
</LinearLayout>
I am not sure about this but you can try with below code also
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:padding="10dp">
<androidx.cardview.widget.CardView
android:id="#+id/corner"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
app:cardCornerRadius="20dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
/>
</androidx.cardview.widget.CardView>

I figured it out. I had mistakenly changed the width of the fragment above the bottom navigation bar.

Related

Multiple RecyclerView(two) has lag - android

I have a list that load in RecyclerView with CardView and in CardView I have a RecyclerView that have (max) 10 items, I load it now, but it has lag:
My First LessonContentListAdapter.kt:
class LessonContentListAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var onItemClickListener: ((LessonsContentList) -> Unit)? = null
fun setOnItemClickListener(listener: (LessonsContentList) -> Unit) {
onItemClickListener = listener
}
private val callback = object : DiffUtil.ItemCallback<LessonsContentList>() {
override fun areItemsTheSame(
oldItem: LessonsContentList,
newItem: LessonsContentList
): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(
oldItem: LessonsContentList,
newItem: LessonsContentList
): Boolean {
return oldItem == newItem
}
}
val differ = AsyncListDiffer(this, callback)
override fun getItemCount(): Int {
return differ.currentList.size
}
inner class LessonContentListItems(private val lessonContentListItemsBinding: LessonContentListItemsBinding) :
RecyclerView.ViewHolder(lessonContentListItemsBinding.root) {
fun bind(lessons: LessonsContentList) {
lessonContentListItemsBinding.txtContentTitleO.text = lessons.titleOriginal
lessonContentListItemsBinding.txtContentTitleT.text = lessons.titleTranslate
lessonContentListItemsBinding.txtTime.text = lessons.timeLaps
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val view =
LessonContentListItemsBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
return LessonContentListItems(view)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
(holder as LessonContentListItems).bind(differ.currentList[position])
val childLessonContentListVectorsAdapter =
ChildLessonContentListVectorsAdapter(
differ.currentList[position].imgLevels
)
holder.itemView.recyclerLessonsContentListVectors.layoutManager = LinearLayoutManager(
holder.itemView.recyclerLessonsContentListVectors.context,
LinearLayoutManager.HORIZONTAL,
false
)
holder.itemView.recyclerLessonsContentListVectors.setHasFixedSize(true)
holder.itemView.recyclerLessonsContentListVectors.adapter =
childLessonContentListVectorsAdapter
}
}
Second ChildLessonContentListVectorsAdapter.kt:
class ChildLessonContentListVectorsAdapter(imgLevels: List<ImgLevelsDetalis>) :
RecyclerView.Adapter<ChildLessonContentListVectorsAdapter.MyViewHolder>() {
var childModelArrayList: List<ImgLevelsDetalis> = imgLevels
class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var heroImage: ImageView
init {
heroImage = itemView.findViewById(R.id.imgHaveStudied)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val view: View = LayoutInflater.from(parent.context)
.inflate(R.layout.child_lessons_content_list, parent, false)
return MyViewHolder(view)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val currentItem: ImgLevelsDetalis = childModelArrayList[position]
holder.heroImage.setImageResource(currentItem.img)
}
override fun getItemCount(): Int {
return childModelArrayList.size
}
}
First fragment_lesson_content_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:clickable="true"
android:fitsSystemWindows="true"
android:focusable="true"
tools:context=".presentation.ui.lessoncontentlist.LessonContentListFragment">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbarLessonContentList"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/purple_500"
android:contentInsetStart="0dp"
android:contentInsetLeft="0dp"
android:minHeight="?attr/actionBarSize"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<ImageButton
android:id="#+id/imgBackContent"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:background="#null"
android:paddingLeft="10dp"
android:src="#drawable/ic_baseline_arrow_back_24" />
<TextView
android:id="#+id/txtTitleContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#color/white"
android:textStyle="bold" />
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerLessonsContentList"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbarLessonContentList" />
</androidx.constraintlayout.widget.ConstraintLayout>
second lesson_content_list_items.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
card_view:cardBackgroundColor="#color/purple_700"
card_view:cardCornerRadius="4dp"
card_view:layout_constraintLeft_toLeftOf="parent"
card_view:layout_constraintRight_toRightOf="parent"
card_view:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="10">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="5"
android:orientation="horizontal"
android:weightSum="10">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="8"
android:orientation="vertical">
<TextView
android:id="#+id/txtContentTitleO"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Grammer"
android:textColor="#color/white"
android:textStyle="bold" />
<TextView
android:id="#+id/txtContentTitleT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="مکالمه"
android:textColor="#color/white" />
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="right"
android:orientation="horizontal">
<TextView
android:id="#+id/txtTime"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:text="2Min"
android:textColor="#color/white"
android:textStyle="bold" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:adjustViewBounds="true"
android:scaleType="center"
android:src="#drawable/ic_baseline_timelapse_24" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:gravity="right"
android:orientation="horizontal">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerLessonsContentListVectors"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:orientation="horizontal" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
Third child_lessons_content_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right">
<ImageView
android:id="#+id/imgHaveStudied"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Firstly, you can use setInitialItemPrefetchCount() for a prefetching feature for nested RecyclerView. for more information please check this article
I think, the main reason, loading images from the server blocks the main thread. You can do this asynchronously. please check this article too.

No view found for id when trying to go from fragment to fragment

I am using a Tabbar Layout to navigate between 3 fragments. (Home,Scanner and History)
Now in the "History" Fragment I have a button(called buttonChart), which should open a new fragment called "ChartFragment".
I tried using the transaction method, but it keeps giving me the error:
No view found for id 0x7f0900df (com.example.nlp_expense_tracker:id/historyFragment) for
fragment ChartFragment{78bb9fe} (4658de1b-d3fe-4f12-98b7-735b882380f1 id=0x7f0900df)
Can anyone help me out?
Here is the History Fragment:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val binding = FragmentHistoryBinding.bind(view)
val exampleAdapter = ExampleAdapter()
binding.apply{
recyclerView.apply{
layoutManager = LinearLayoutManager(requireContext())
adapter = exampleAdapter
}
buttonChart.apply {
setOnClickListener {
val fragment = ChartFragment()
val transaction = requireActivity().supportFragmentManager.beginTransaction()
transaction.replace(R.id.historyFragment, fragment)
transaction.disallowAddToBackStack()
transaction.commit()
}
}
}
The ChartFragment:
#AndroidEntryPoint
class ChartFragment : Fragment(R.layout.fragment_chart) {
private val viewModel: PurchaseViewmodel by viewModels()
private lateinit var sparkView: SparkView
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_chart, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
sparkView = view.findViewById(R.id.sparkView)
val adapter = ReceiptsSparkAdapter(viewModel.receiptsChart)
sparkView.adapter = adapter
}
}
And the MainActivity:
#AndroidEntryPoint
class MainActivity : AppCompatActivity() {
private lateinit var viewPager: ViewPager
private lateinit var tabs: TabLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewPager = findViewById(R.id.viewPager)
tabs = findViewById(R.id.tabs)
setUpTabs()
}
private fun setUpTabs(){
val adapter = ViewPagerAdapter(supportFragmentManager)
adapter.addFragment(HomeFragment(),"")
adapter.addFragment(Scanner(),"")
adapter.addFragment(HistoryFragment(),"")
viewPager.adapter = adapter
tabs.setupWithViewPager(viewPager)
tabs.getTabAt(0)!!.setIcon(R.drawable.ic_baseline_home_24)
tabs.getTabAt(2)!!.setIcon(R.drawable.ic_baseline_show_chart_24)
tabs.getTabAt(1)!!.setIcon(R.drawable.ic_baseline_add_shopping_cart_24)
}
}
#addet the layout for activity and fragment
Activity Layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#drawable/backgroundcolor">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
>
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:tabGravity="fill"
app:tabInlineLabel="true"
app:tabMode="fixed"
app:tabTextColor="#android:color/white"
app:tabBackground="#drawable/backgroundtab"
app:tabIconTint="#c9d6df"
app:tabIndicatorColor="#c9d6df"
/>
</com.google.android.material.appbar.AppBarLayout>
<com.example.nlp_expense_tracker.ViewPagerCustom
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/viewPager"
android:layout_below="#+id/appBarLayout"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
And the Fragment XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.History.HistoryFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
<Button
android:id="#+id/buttonChart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#+id/textViewEuro"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textViewEuro" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Recent Purchases"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="4dp"
android:layout_marginLeft="8dp"
android:textColor="#color/white"/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total amount spent:"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginBottom="50dp"
android:layout_marginLeft="8dp"
android:textSize="25sp"
android:textColor="#color/white"/>
<TextView
android:id="#+id/totalSumTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:gravity="left"
android:text="0.0"
android:textColor="#color/white"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView3"
android:layout_marginStart="5dp"/>
<TextView
android:id="#+id/textViewEuro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:text="€"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/totalSumTextView"
android:textColor="#color/white"/>
</androidx.constraintlayout.widget.ConstraintLayout>

Recyclerview design issue

I have recyclerview to show my order items but it has design issue and after 3 days changing it I have no other solution for it!
screenshot
Issues
Recyclerview is not covering page full height
Items have space same as recyclerview height (it shows 1 item per page!)
Code
fragment_order.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/orderItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".main.OrdersFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/orders_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
order_items.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:id="#+id/cardView2"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginBottom="5dp"
app:cardElevation="2dp"
android:layout_margin="5dp">
<FrameLayout
android:background="#color/orders"
android:layout_width="4dp"
android:layout_height="match_parent"/>
<TextView
android:id="#+id/order_Iid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:gravity="start|top"
android:text="#string/order_ID"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="#+id/order_status_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|start"
android:layout_marginStart="10dp"
android:layout_marginTop="35dp"
android:text="#string/order_status"
android:textColor="#5CDCBD"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/order_price_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|end"
android:layout_marginEnd="10dp"
android:text="#string/price"
android:textSize="12sp"
android:textStyle="bold" />
</androidx.cardview.widget.CardView>
</LinearLayout>
OrdersFragment.kt
class OrdersFragment : Fragment() {
lateinit var sesssion: SessionManager
lateinit var laundriesRecycler: RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val root = inflater.inflate(R.layout.fragment_orders, container, false)
sesssion = SessionManager(context)
laundriesRecycler = root.findViewById(R.id.orders_list)
getOrders()
return root
}
private fun getOrders() {
var session = SessionManager(context)
session.checkLogin()
var user = session.getUserDetails()
var token: String? = user.get(SessionManager.KEY_ACCESS_TOKEN)
val tokenFull = "Bearer $token"
val queue = Volley.newRequestQueue(context)
val url = "https://example.com/api/orders"
val stringReq : StringRequest =
object : StringRequest(
Method.GET, url,
Response.Listener { response ->
val list = Gson().fromJson(response, OrderArr::class.java)
laundriesRecycler.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
laundriesRecycler.adapter = OrdersAdapter(context, list)
},
Response.ErrorListener {
Toast.makeText(context, R.string.errorMessage, Toast.LENGTH_LONG)
.show()
}
){
override fun getHeaders(): Map<String, String> {
val headers = HashMap<String, String>()
headers["Content-Type"] = "application/json"
headers["Authorization"] = tokenFull
return headers
}
}
queue.add(stringReq)
}
}
Any idea?
Update
OrdersAdapter.kt
class OrdersAdapter(
val context: Context?,
private var orderList: OrderArr
) : RecyclerView.Adapter<OrdersAdapter.OrderViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OrdersAdapter.OrderViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(
R.layout.orders_items,
parent,
false
)
//fixing width issue
val outMetrics = DisplayMetrics()
val display = parent.display
display?.getRealMetrics(outMetrics)
itemView.layoutParams = RecyclerView.LayoutParams(outMetrics.widthPixels, RecyclerView.LayoutParams.MATCH_PARENT)
return OrdersAdapter.OrderViewHolder(itemView)
}
override fun onBindViewHolder(holder: OrdersAdapter.OrderViewHolder, position: Int) {
val currentItem = orderList.data[position]
holder.orderId.text = "ID: " + currentItem.id
holder.price.text = if(currentItem.amount!= null){"Rp. " + currentItem.amount + " /KG"}else{"Not provided"}
holder.status.text = if(currentItem.lastProgress!= null) {currentItem.lastProgress.progress.name} else{"Unknown"}
holder.itemView.setOnClickListener {
val i: Intent = Intent(context, OrdersActivity::class.java)
i.putExtra("orderIDArgument", currentItem.id.toString())
it.context.startActivity(i);
}
}
override fun getItemCount() = orderList.data.size
class OrderViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
val orderId: TextView = itemView.findViewById(R.id.order_Iid)
val status: TextView = itemView.findViewById(R.id.order_status_text)
val price: TextView = itemView.findViewById(R.id.order_price_text)
}
}
Update 2
Activity_main.xml
Every fragment will replace each other inside this activity
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="56dp">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentFragmentId"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/main_graphs"
tools:context=".MainActivity" />
</ScrollView>
<!--bottom navigation bar with items-->
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bottomAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/colorPrimaryNewTheme"
android:backgroundTint="#color/colorPrimaryNewTheme"
app:backgroundTint="#android:color/white"
app:fabAlignmentMode="end"
app:fabCradleMargin="10dp"
app:fabCradleRoundedCornerRadius="20dp"
app:fabCradleVerticalOffset="14dp"
app:hideOnScroll="true"
app:menu="#menu/bottom_menu"
tools:ignore="BottomAppBar,MissingConstraints" />
<!--Floating action button which is anchored to the bottom navigation button-->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/colorPrimaryNewTheme"
android:contentDescription="#string/app_name"
android:src="#drawable/order"
app:backgroundTint="#color/colorPrimaryNewTheme"
app:layout_anchor="#id/bottomAppBar"
app:layout_dodgeInsetEdges="bottom"
tools:ignore="MissingConstraints" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Update 3
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".main.MainFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/header_imageP"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/himage"
android:scaleType="centerCrop"
android:src="#drawable/main_header"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="42dp"
android:layout_marginTop="31dp"
android:textColor="#FFFFFF"
android:text="#string/nameHint" />
<TextView
android:id="#+id/useremail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/username"
android:layout_alignParentStart="true"
android:layout_marginStart="42dp"
android:layout_marginTop="8dp"
android:textColor="#FFFFFF"
android:text="#string/emailHint" />
<ImageView
android:id="#+id/logo"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_below="#+id/useremail"
android:layout_alignParentStart="true"
android:layout_marginStart="41dp"
android:layout_marginTop="13dp"
android:contentDescription="#string/himage"
android:src="#drawable/logo" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="21dp"
android:layout_marginTop="118dp"
android:layout_toEndOf="#+id/logo"
android:text="#string/app_name"
android:textColor="#FFFFFF"
android:textSize="18sp"
android:textStyle="bold" />
<RelativeLayout
android:id="#+id/t1"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_below="#+id/header_imageP"
android:layout_marginTop="4dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginStart="6dp"
android:text="HIIII" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/t2"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_below="#+id/t1"
android:layout_marginTop="4dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginStart="6dp"
android:text="BYEEEE" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
First remove code for customizing width and height of recycler item view inside Adapter.
Like:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OrdersAdapter.OrderViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(
R.layout.order_item,
parent,
false
)
//fixing width issue
/*val outMetrics = DisplayMetrics()
val display = parent.display
display?.getRealMetrics(outMetrics)
itemView.layoutParams = RecyclerView.LayoutParams(outMetrics.widthPixels, RecyclerView.LayoutParams.MATCH_PARENT)*/
return OrdersAdapter.OrderViewHolder(itemView)
}
Second, you are not putting any constraint to FragmentContainerView thats why it goes to (0,0) position by default and you have set it's height to wrap_content, so your recycler view looks like the posted image. And it is not recommended to use recycler view inside scroll view. Try with customizing Activity_main.xml as below:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bottomAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:fabAlignmentMode="end"
app:fabCradleMargin="10dp"
app:fabCradleRoundedCornerRadius="20dp"
app:fabCradleVerticalOffset="14dp"
app:hideOnScroll="true"
app:menu="#menu/bottom_menu"
tools:ignore="BottomAppBar,MissingConstraints"
app:layout_constraintBottom_toBottomOf="parent"/>
<fragment
android:id="#+id/nav_view"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#+id/bottomAppBar"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/main_graphs" />
<!-- put FAB here -->
</androidx.constraintlayout.widget.ConstraintLayout>

RecyclerView not inflating items

Here is the main layout that consist of the recyclerview, collapsingtoolbar layout and other layout i'm using
making the height of the recyclerview wrap conent didnt solve the problem, and i'm correcly passing the size of the item in the adapter as well,
hope you help me find what i'm doing wrong , ive been on this for some days now
main layout
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="300dp"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:expandedTitleTextAppearance="#android:color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="#+id/love_music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TITILE"
android:textColor="#android:color/white"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="subtitle"
android:textColor="#android:color/white"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<include layout="#layout/content_main" />
below is my adapter class
class FeedsAdapter(val items: List<Advert>) : RecyclerView.Adapter<FeedsAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.item_card, parent,false)
return ViewHolder(v)
}
override fun getItemCount(): Int = items.size
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val post:Advert = items[position]
holder.postName.text = post.username
holder.postDesc.text = post.description
holder.postLocation.text = post.location
holder.timePosted.text = post.created_at
Picasso.get().load(post.image1).into(holder.postImage)
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val postName = itemView.findViewById(R.id.post_name) as TextView
val postDesc = itemView.findViewById(R.id.post_description) as TextView
val postLocation = itemView.findViewById(R.id.post_location)as TextView
val timePosted = itemView.findViewById(R.id.post_time_posted) as TextView
val postImage = itemView.findViewById(R.id.post_image) as ImageView
}
}
The fragment where i'm trying to call use the layout
class HomeFragment : Fragment() {
private lateinit var homeViewModel: HomeViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
homeViewModel = ViewModelProviders.of(this).get(HomeViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_home, container, false)
val recyclerView = view?.findViewById<RecyclerView>(R.id.advert_recyclerView)
recyclerView?.layoutManager = LinearLayoutManager(activity,LinearLayoutManager.VERTICAL ,false)
//val textView: TextView = root.findViewById(R.id.text_home)
/* homeViewModel.text.observe(this, Observer {
textView.text = it
})*/
Coroutines.main{
ApiClient.instance.getAdvert().enqueue(object: Callback<AdvertResponse> {
override fun onFailure(call: Call<AdvertResponse>, t: Throwable) {
Toast.makeText(activity,"login failed ${t.message}",Toast.LENGTH_LONG).show()
}
override fun onResponse(
call: Call<AdvertResponse>,
response: Response<AdvertResponse>
) {
val ads: List<Advert> = response.body()!!.Advert
val adapter = FeedsAdapter(ads)
recyclerView?.adapter = adapter
adapter.notifyDataSetChanged()
}
})
}
return root
}
}
The individual item i'm tryning to inflate into the recyclerview
androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/post_profile_pic"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="#tools:sample/avatars" />
<TextView
android:id="#+id/post_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="Ruth Agwu"
app:layout_constraintStart_toEndOf="#+id/post_profile_pic"
app:layout_constraintTop_toTopOf="#+id/post_profile_pic" />
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="45dp"
android:layout_height="12dp"
android:layout_marginStart="16dp"
app:layout_constraintStart_toEndOf="#+id/post_name"
app:layout_constraintTop_toTopOf="#+id/post_name" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/ratingBar"
app:srcCompat="#drawable/more_vert_24dp" />
<ImageView
android:id="#+id/imageView8"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toEndOf="#+id/post_profile_pic"
app:layout_constraintTop_toBottomOf="#+id/post_name"
app:srcCompat="#drawable/location_on_24dp" />
<TextView
android:id="#+id/post_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Jabi"
app:layout_constraintStart_toEndOf="#+id/imageView8"
app:layout_constraintTop_toBottomOf="#+id/post_name" />
<TextView
android:id="#+id/post_time_posted"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="25 min ago"
app:layout_constraintStart_toEndOf="#+id/post_location"
app:layout_constraintTop_toTopOf="#+id/post_location" />
<ImageView
android:id="#+id/post_image"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/post_time_posted"
app:srcCompat="#drawable/solar_panel" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="50dp"
android:background="#drawable/ash_background"
app:layout_constraintBottom_toBottomOf="#+id/post_image"
app:layout_constraintEnd_toEndOf="#+id/post_image"
app:layout_constraintStart_toStartOf="#+id/post_image">
<TextView
android:id="#+id/post_description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:gravity="start|top"
android:text="We specialize on all kinds of cakes and snack's. cooking Catering and events management"
android:textColor="#android:color/white"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
changing the home fragment to this worked like magic
class HomeFragment : Fragment() {
private lateinit var homeViewModel: HomeViewModel
lateinit var rvRecyclerView : RecyclerView
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
var view = inflater.inflate(R.layout.fragment_home, container, false)
rvRecyclerView = view!!.findViewById<RecyclerView>(R.id.advert_recyclerView)
rvRecyclerView.layoutManager = LinearLayoutManager(activity,LinearLayoutManager.VERTICAL ,false)
//homeViewModel = ViewModelProviders.of(this).get(HomeViewModel::class.java)
fetchFeeds()
return view
}
private fun fetchFeeds() {
Coroutines.main{
ApiClient.instance.getAdvert().enqueue(object: Callback<AdvertResponse> {
override fun onFailure(call: Call<AdvertResponse>, t: Throwable) {
Toast.makeText(activity,"login failed ${t.message}",Toast.LENGTH_LONG).show()
}
override fun onResponse(
call: Call<AdvertResponse>,
response: Response<AdvertResponse>
) {
val ads: List<Advert> = response.body()!!.Advert
val adapter = FeedsAdapter(ads)
rvRecyclerView.adapter = adapter
adapter.notifyDataSetChanged()
}
})
}
}
}

RecyclerView items not showing properly

In my project, I have used RecyclerView which is shown in layout-preview as I expected but the problem is when I run the application in emulator/device the items of RecyclerView not showing as layout-preview shown.
Here is the XML of recyclerview.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".view.LActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="#layout/rv_sample" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
android:src="#drawable/ic_add"
android:id="#+id/fabAdd"
app:tint="#color/white"
app:backgroundTint="#color/colorPrimary"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_height="wrap_content"/>
</androidx.constraintlayout.widget.ConstraintLayout>
XML of recyclerview items.
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
app:cardCornerRadius="10dp"
android:minHeight="60dp"
app:cardUseCompatPadding="true"
app:strokeColor="#color/design_default_color_secondary_variant"
app:strokeWidth="1dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvWord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginEnd="10dp"
android:drawableStart="#drawable/arrow_right"
android:fontFamily="monospace"
android:text="#{item.word}"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{item.type}"
android:layout_marginEnd="10dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="#id/tvWord" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
Adapter class
class MyAdapter(private var itemList: List<Item>, private val listener: ItemClickListener) :
RecyclerView.Adapter<MyAdapter.VHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = RvSampleBinding.inflate(inflater)
return VHolder(binding)
}
override fun getItemCount(): Int = itemList.size
override fun onBindViewHolder(holder: VHolder, position: Int) = holder.bind(itemList[position])
inner class VHolder(private val binding: RvSampleBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(item: Item) {
binding.item = item
binding.root.setOnClickListener {
listener.onItemClick(item)
}
binding.executePendingBindings()
}
}
Screenshot of layout-preview
And Screenshot of actual output.
I have run the application in several emulators and devices but can't figure out where is the problem.
Here I am going to answer my own question. So that this answer will help other users who will face same issue.
Thanks, #MikeM for his helpful comment. All credit goes to him.
Problem: The problem is in my adapter, I was inflating the item-layout without the parent viewgroup passed.
Cause: If you don't pass the parent in your layout-inflater that will inflate your layout with default layout-params and that wrap your content in both directions(width and height), even though you might have match_parent for the layout_width.
in my case, I have tried like this
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = RvSampleBinding.inflate(inflater) // here i didn't pass viewgroup and boolean parameters.
return VHolder(binding)
}
Solution: My problem is solved by modifying the onCreateViewHolder like the following.
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = RvSampleBinding.inflate(inflater,parent,false) //here i have passed viewgroup and boolean parameter and that's solve my problem
return VHolder(binding)
}
So basically your parent Layout is constraint layout, so you have to add all constraint related to all the views, One suggestion just used another parent layout instead of Constraint layout just for the check/validation purpose.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".RecyclerViewActivity"
android:id="#+id/recyclerView_ex1">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="#layout/recycler_view_item_ex1" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
android:src="#drawable/ic_add"
android:id="#+id/fabAdd"
app:tint="#color/cardview_light_background"
app:backgroundTint="#color/colorPrimary"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_height="wrap_content"/>
</androidx.constraintlayout.widget.ConstraintLayout>
itemview
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tool="http://schemas.android.com/tools"
android:padding="5dp"
app:cardCornerRadius="10dp"
android:minHeight="60dp"
app:cardUseCompatPadding="true"
app:strokeColor="#color/colorAccent"
app:strokeWidth="1dp"
xmlns:android="http://schemas.android.com/apk/res/android"
tool:context=".RecyclerViewActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvWord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginEnd="10dp"
android:drawableStart="#drawable/arrow_right"
android:fontFamily="monospace"
android:text="word"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="#+id/tvType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="type"
android:layout_marginEnd="10dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="#id/tvWord"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
Please vote if it works.
Happy coding. Thanks

Categories

Resources