0

I have used a RecyclerView which is shaped to horizontal using LayoutManager, I put my RecyclerView in HorizontalScrollView and it works well, in the android simulation of the Android Studio while in the real phone, it works some times! I mean it scrolls a little and can't scroll completely but if you try to scroll it so much suddenly it can scroll completely.

here is my recycler view code:

    <HorizontalScrollView
        android:id="@+id/scrollbar"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">


           <androidx.recyclerview.widget.RecyclerView
               android:id="@+id/recycler_challenge"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content" />


    </HorizontalScrollView>

and these codes show how I used RecyclerView in my Fragment:

    val recyclerView = view.findViewById<RecyclerView>(R.id.recycler_challenge)

    recyclerView.layoutManager = LinearLayoutManager(view.context, LinearLayoutManager.HORIZONTAL, false)


    val challenges = ArrayList<challenge_model>()

    challenges.add(challenge_model("https://pic1.jpg", "picture one", false,"pic_1"))
    challenges.add(challenge_model("https://pic1.jpg", "picture two", false,"pic_2"))
    challenges.add(challenge_model("https://pic1.jpg", "picture two", false,"pic_3"))
    challenges.add(challenge_model("https://pic1.jpg", "picture two", false,"pic_4"))
    challenges.add(challenge_model("https://pic1.jpg", "picture two", false,"pic_5"))


    val adapter = challenge_adapter(challenges)
    recyclerView.adapter = adapter
    adapter.notifyDataSetChanged()

based on the code above, about not scrolling completely, I mean I can scroll and see three picture and it seems it is it! but some times suddenly it can be scrolled completely and show all 5 images.

Mohammad Derakhshan
  • 1,262
  • 11
  • 33

2 Answers2

2

Try with the only recyclerview, delete HorizontalScrollView from parent. For HorizontalScroll you can use below code.

recycler_challenge.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));


<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_challenge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager" />
Akshay Raiyani
  • 1,243
  • 9
  • 21
-1

When you are setting adapter for first time, you don't need to notifyDataSetChanged(), So try removing

adapter.notifyDataSetChanged()

You should use it whenever there is a change in list items afterwards anywhere in class.

Karan Mehta
  • 1,442
  • 13
  • 32