回收器查看单击时意外滚动



当我多次点击回收器视图时,它会在随机点击时滚动到底部。

我怀疑问题出在模拟器上。我的AVD是Nexus 5 API 27 x86。

问题:如何消除这种随机滚动?

下面是最小示例:https://github.com/OleksandrBezhan/RecyclerViewAccidentalScrolling

活动:

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recycler.layoutManager = LinearLayoutManager(this)
recycler.adapter = MyAdapter()
}
}

适配器:

class MyAdapter : RecyclerView.Adapter<MyAdapter.ViewHolder>() {
class ViewHolder(view: View, val text: TextView) : RecyclerView.ViewHolder(view)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item, parent, false)
return ViewHolder(view, view.text)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.text.text = "Hello world"
}
override fun getItemCount() = 3
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>

项目.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>

更新:我添加了OnScrollListener,它显示意外滚动时滚动状态变为SCROLL_STATE_FLING,然后SCROLL_STATE_IDLE。

而在正常情况下,滚动状态应变为 SCROLL_STATE_TOUCH_SCROLL -> SCROLL_STATE_FLING -> SCROLL_STATE_IDLE。

recycler.addOnScrollListener(object: RecyclerView.OnScrollListener(){
override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
Log.d("TEST", "onScrollStateChanged: $newState")
}
})
D/TEST: onScrollStateChanged: 2
D/TEST: onScrollStateChanged: 0 
// 1, 2, 0 in normal scrolling

这似乎是一个模拟器问题,因为在真实设备上没有问题。

最新更新