RecyclerView在点击时意外滚动

时间:2018-05-16 09:17:56

标签: android user-interface

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

我怀疑问题出在模拟器上。我的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>

item.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 - &gt; SCROLL_STATE_FLING - &gt; 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

1 个答案:

答案 0 :(得分:0)

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