Home > Software design >  RecyclerView doesn't display all data
RecyclerView doesn't display all data

Time:03-02

I am working on an application with multiple recycler views inside a fragment. Everything is working properly, except from one recycler view which doesn't display all data. I get the data after an API call, and I have already checked that they are loaded successfully. Any help would be much appreciated, thank you in advance!!

Here is the recycler view inside the fragment:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    tools:context=".ui.fragments.ExploreFragment">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:fillViewport="true"
        android:scrollbars="none">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@ id/rvExploreFragmentFeaturedProducts"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="30dp"
                >

            </androidx.recyclerview.widget.RecyclerView>


        </RelativeLayout>

    </ScrollView>

</RelativeLayout>

Here is the cardview which will be loaded repeatedly inside the recycler view, which is not properly displayed (only the image view and shapeable image view gets displayed):

<?xml version="1.0" encoding="utf-8"?>
<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:id="@ id/layoutFeaturedProductRowCardView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:cardCornerRadius="10dp"
    app:cardElevation="5dp"
    android:layout_marginBottom="40dp">

    <RelativeLayout
        android:id="@ id/rlFeaturedProducts"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="@drawable/rounded_frame_recommproducts_explore">

        <com.google.android.material.imageview.ShapeableImageView
            android:id="@ id/ivFeaturedProduct"
            android:layout_width="335dp"
            android:layout_height="180dp"
            android:scaleType="centerCrop"
            app:shapeAppearanceOverlay="@style/rounded_cornersTop"
            tools:src="@tools:sample/backgrounds/scenic" />

        <ImageView
            android:id="@ id/imageViewFavorite"
            android:layout_width="wrap_content"
            android:layout_height="26dp"
            android:layout_alignEnd="@id/ivFeaturedProduct"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="20dp"
            android:src="@drawable/ic_favorite_up"/>

        <TextView
            android:id="@ id/textViewProductTitle"
            android:layout_width="295dp"
            android:layout_height="wrap_content"
            android:layout_below="@ id/ivFeaturedProduct"
            android:layout_marginStart="15dp"
            android:layout_marginTop="20dp"
            android:fontFamily="@font/semibold"
            android:textColor="@color/colorPrimaryDarkV2"
            android:textSize="16sp" />

        <TextView
            android:id="@ id/textViewCategory"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/textViewProductTitle"
            android:layout_alignStart="@id/textViewProductTitle"
            android:fontFamily="@font/semibold"
            android:textSize="12sp"/>

        <TextView
            android:id="@ id/textViewDuration"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/textViewCategory"
            android:layout_alignStart="@id/textViewCategory"
            android:layout_marginTop="20dp"
            android:drawablePadding="10dp"
            android:fontFamily="@font/medium"
            android:text="@string/durationlabel"
            android:textColor="@color/colorPrimaryLightV2"
            android:textSize="12sp"
            app:drawableLeftCompat="@drawable/ic_time_icon" />

        <TextView
            android:id="@ id/textViewDurationValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@id/textViewDuration"
            android:layout_alignBaseline="@id/textViewDuration"
            android:layout_marginStart="2dp"
            android:fontFamily="@font/medium"
            android:textColor="@color/colorPrimaryDarkV2"
            android:textSize="12sp"/>

        <TextView
            android:id="@ id/textViewLanguage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/textViewDuration"
            android:layout_alignStart="@id/textViewDuration"
            android:layout_marginTop="12dp"
            android:drawablePadding="10dp"
            android:fontFamily="@font/medium"
            android:text="@string/languagelabel"
            android:textColor="@color/colorPrimaryLightV2"
            android:textSize="12sp"
            app:drawableLeftCompat="@drawable/ic_audio_icon" />

        <TextView
            android:id="@ id/textViewLanguageValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@id/textViewLanguage"
            android:layout_alignBaseline="@id/textViewLanguage"
            android:layout_marginStart="2dp"
            android:fontFamily="@font/medium"
            android:textColor="@color/colorPrimaryDarkV2"
            android:textSize="12sp" />

        <TextView
            android:id="@ id/textViewThreeSixtyImages"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/textViewLanguage"
            android:layout_alignStart="@id/textViewLanguage"
            android:layout_marginTop="12dp"
            android:drawablePadding="10dp"
            android:fontFamily="@font/medium"
            android:text="@string/theesixtylabel"
            android:textColor="@color/colorPrimaryLightV2"
            android:textSize="12sp"
            app:drawableLeftCompat="@drawable/ic_images_icon"
            android:visibility="gone"/>

        <TextView
            android:id="@ id/ratingLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@id/ratingValue"
            android:layout_toStartOf="@id/ratingValue"
            android:layout_marginTop="2dp"
            android:drawablePadding="10dp"
            android:fontFamily="@font/semibold"
            android:textColor="@color/colorPrimaryDarkV2"
            android:textSize="14sp"
            app:drawableLeftCompat="@drawable/ic_rating_small_tag" />

        <TextView
            android:id="@ id/ratingValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toStartOf="@id/ratingCount"
            android:layout_alignBaseline="@id/ratingCount"
            android:layout_marginEnd="2dp"
            android:fontFamily="@font/semibold"
            android:textColor="@color/colorPrimaryDarkV2"
            android:textSize="14sp"
            />

        <TextView
            android:id="@ id/ratingCount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/ivFeaturedProduct"
            android:layout_alignTop="@id/textViewCategory"
            android:layout_alignRight="@id/ivFeaturedProduct"
            android:layout_marginEnd="15dp"
            android:fontFamily="@font/semibold"
            android:textColor="@color/colorPrimaryLightV2"
            android:textSize="14sp" />

        <TextView
            android:id="@ id/priceLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/ratingCount"
            android:layout_alignEnd="@id/ratingCount"
            android:layout_marginTop="20dp"
            android:fontFamily="@font/medium"
            android:text="@string/fromLabel"
            android:textColor="@color/colorPrimaryLightV2"
            android:textSize="12sp" />

        <TextView
            android:id="@ id/salesValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignEnd="@id/priceLabel"
            android:layout_below="@id/priceLabel"
            android:layout_marginTop="2dp"
            android:fontFamily="@font/medium"
            android:textSize="12sp"
            android:textColor="@color/colorPrimaryV2"
            android:visibility="gone"/>

        <TextView
            android:id="@ id/priceValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/priceLabel"
            android:layout_alignEnd="@id/priceLabel"
            android:layout_marginTop="18dp"
            android:fontFamily="@font/bold"
            android:textColor="@color/colorPrimaryDarkV2"
            android:textSize="18sp" />

        <TextView
            android:id="@ id/perPersonLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/priceValue"
            android:layout_alignBaseline="@id/textViewThreeSixtyImages"
            android:layout_alignEnd="@ id/priceValue"
            android:fontFamily="@font/medium"
            android:layout_marginBottom="24dp"
            android:text="@string/perPersonLabel"
            android:textColor="@color/colorPrimaryLightV2"
            android:textSize="12sp" />

    </RelativeLayout>

</androidx.cardview.widget.CardView>

Then, here is the adapter giving the card view the values:

class ProductsAdapter(private val products: List<Product>) :
    RecyclerView.Adapter<ProductsAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) : ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.view_layout_featuredproduct_row, parent, false)

        view.layoutParams.height = (parent.measuredWidth - 40)/2   30
        view.requestLayout()

        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindProduct(products[position])
    }
    

    override fun getItemCount(): Int = products.size

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        private val image = view.findViewById<ShapeableImageView>(R.id.ivFeaturedProduct)
        private val featuredProductTitle = view.findViewById<TextView>(R.id.textViewProductTitle)
        private val category = view.findViewById<TextView>(R.id.textViewCategory)
        private val featuredProductDuration = view.findViewById<TextView>(R.id.textViewDurationValue)
        private val language = view.findViewById<TextView>(R.id.textViewLanguageValue)
        private val averageRating = view.findViewById<TextView>(R.id.ratingValue)
        private val ratingcount = view.findViewById<TextView>(R.id.ratingCount)
        private val pricevalue = view.findViewById<TextView>(R.id.priceValue)
        private val context = view.context
        private val resources = view.resources

        fun bindProduct(product: Product) {
            with(product) {

                val uri = this.header_image
                Picasso.get().load(uri).
                placeholder( R.drawable.progress_animation ).
                transform(ColorFilterTransformation(ContextCompat.getColor(context, R.color.colorOverlay))).
                into(image)

                val categoryHelper = this.sku

                if (categoryHelper[0].equals('S')){
                    category.text = resources.getString(R.string.skipTheLineTours)
                    category.setBackgroundResource(R.drawable.label_skipthelinetour)
                    category.setTextColor(resources.getColor(R.color.colorSkipTheLineTourTxt))
                }

                if (categoryHelper[0].equals('A')){
                    category.text = resources.getString(R.string.audioTours)
                    category.setBackgroundResource(R.drawable.label_audiotour)
                    category.setTextColor(resources.getColor(R.color.colorAudioTourTxt))

                }
                if(categoryHelper[0].equals('V')){
                    category.text = resources.getString(R.string.virtualTours)
                    category.setBackgroundResource(R.drawable.label_virtualtour)
                    category.setTextColor(resources.getColor(R.color.colorVirtualTourTxt))

                }

                featuredProductTitle.text = this.title
                featuredProductDuration.text = this.duration " minutes"
                language.text = languages.size.toString() " languages"
                //category.text = this.sku
                averageRating.text = this.average_rating
                ratingcount.text = "(" this.rating_count.toString() ")"
                pricevalue.text = this.retail_price.toString() "€"

            }
        }
    }
}

And last but not least, here is the code generating the recycler view:

class ExploreFragment : Fragment() {
    
    private lateinit var viewProductsAdapter: ProductsAdapter

    private val model: ProductsViewModel by activityViewModels()

    private lateinit var featuredProducts: MutableList<Product>

    private lateinit var viewManager : LinearLayoutManager

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment

        return inflater.inflate(R.layout.fragment_explore, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)


       
        loadFeaturedProducts()

        }

    private fun loadFeaturedProducts() {
        if(!NetworkState().isNetworkAvailable([email protected]!!.applicationContext)){
            return;
        }

        featuredProducts = mutableListOf()

        val recyclerView = rvExploreFragmentFeaturedProducts

        viewProductsAdapter = ProductsAdapter(this.featuredProducts)

        viewManager = LinearLayoutManager(this.context, LinearLayoutManager.VERTICAL, false)
        recyclerView.layoutManager = viewManager
        //recyclerView.setHasFixedSize(true)
        recyclerView.adapter = viewProductsAdapter


        Timber.i("Fragment: load featured products")

        model.getProducts().observe(viewLifecycleOwner, { products->
            if(products.status == Status.SUCCESS) {
                this.featuredProducts.addAll(products.data!!)
                viewProductsAdapter.notifyDataSetChanged()

            }else {
                Timber.i("Featured Products message: " products.message)
                Snackbar.make(view!!, "Cannot load featured products! Error:" products.message, Snackbar.LENGTH_LONG).show()
            }
        })

    }


}

CodePudding user response:

Thank you for your answers - I figured out my mistake:

I couldn't see anything that was below the ImageView inside my CardView, even though it loaded correctly.

On ProductsListAdapter I forgot to remove an unnecessary calculation:

view.layoutParams.height = (parent.measuredWidth - 40)/2   30
view.requestLayout()

which was what caused the problem!

CodePudding user response:

Instead of using the following code:

<androidx.recyclerview.widget.RecyclerView
                android:id="@ id/rvExploreFragmentFeaturedProducts"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="30dp"
                >
            </androidx.recyclerview.widget.RecyclerView>

Please update it with

<androidx.recyclerview.widget.RecyclerView
                android:id="@ id/rvExploreFragmentFeaturedProducts"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="30dp"
                />

the Reason you are not getting data is tags! You are not closing tag of Recycler View instead you are creating a new scope to add more things in it like we do with Layouts.

So remove : </androidx.recyclerview.widget.RecyclerView> and add "/" to the closing tag of recycler view or just simply copy and paste my code above

  • Related