Search code examples
androidkotlinscrollview

scrollview doesn't work in mobile but work in simulation


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.


Solution

  • 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" />