【问题标题】:scrollview doesn't work in mobile but work in simulationscrollview 在移动设备中不起作用,但在模拟中起作用
【发布时间】:2019-12-31 06:42:53
【问题描述】:

我使用了一个 RecyclerView,它使用 LayoutManager 调整为水平形状, 我把我的 RecyclerView 放在 Horizo​​ntalScrollView 中,效果很好,在 Android Studio 的 android 模拟中,而在真机中,它可以工作一些时间!我的意思是它滚动了一点,不能完全滚动,但如果你突然尝试滚动它,它可以完全滚动。

这是我的回收站查看代码:

    <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>

这些代码显示了我如何在 Fragment 中使用 RecyclerView:

    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()

根据上面的代码,关于不完全滚动,我的意思是我可以滚动看到三张图片,似乎就是它!但有时它会突然完全滚动并显示所有 5 张图片。

【问题讨论】:

  • 你不需要水平滚动视图。甚至 Recyclerview 都会水平和垂直滚动
  • 如果recyclerview本身也能正常工作,还需要水平滚动视图吗?
  • 请参考以下网址,这可能会对您有所帮助stackoverflow.com/questions/34258496/…
  • 请参考以下网址,这可能会对您有所帮助stackoverflow.com/questions/34258496/…
  • 谢谢,尼克。当我删除 Horizo​​ntalScrollView 时,它可以正常工作!

标签: android kotlin scrollview


【解决方案1】:

尝试使用唯一的recyclerview,从父级删除Horizo​​ntalScrollView。对于 Horizo​​ntalScroll,您可以使用以下代码。

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

【讨论】:

    【解决方案2】:

    当你第一次设置适配器时,你不需要 notifyDataSetChanged(), 所以尝试删除

    adapter.notifyDataSetChanged()
    

    以后在课堂上的任何地方,只要列表项发生变化,你就应该使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多