我无法禁用滚动RecyclerView.我试着打电话rv.setEnabled(false)但我仍然可以滚动.
如何禁用滚动?
假设您创建的应用程序具有与您通过"滚动活动"向导创建的应用程序类似的UI,但您希望滚动标记具有捕捉功能,如下所示:
<android.support.design.widget.CollapsingToolbarLayout ... app:layout_scrollFlags="scroll|exitUntilCollapsed|snap" >
Run Code Online (Sandbox Code Playgroud)
事实证明,在许多情况下,它存在捕捉问题.有时UI不会捕捉到顶部/底部,使得CollapsingToolbarLayout保持在两者之间.
有时它也会尝试捕捉到一个方向,然后决定捕捉到另一个方向.
您可以在此处查看所附视频的两个问题.
我认为这是我在RecyclerView上使用setNestedScrollingEnabled(false)时遇到的问题之一,所以我在这里问了一下,但后来我注意到即使使用解决方案也没有使用此命令,甚至在使用时一个简单的NestedScrollView(由向导创建),我仍然可以注意到这种行为.
这就是为什么我决定这个报告是一个问题,在这里.
可悲的是,我找不到StackOverflow上那些奇怪错误的解决方法.
为什么会发生,更重要的是:如何在仍然使用它应该具有的行为的同时避免这些问题?
编辑:这是一个很好的改进Kotlin版本的接受答案:
class RecyclerViewEx @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyle: Int = 0) : RecyclerView(context, attrs, defStyle) {
private var mAppBarTracking: AppBarTracking? = null
private var mView: View? = null
private var mTopPos: Int = 0
private var mLayoutManager: LinearLayoutManager? = null
interface AppBarTracking {
fun isAppBarIdle(): Boolean
fun isAppBarExpanded(): Boolean
}
override fun dispatchNestedPreScroll(dx: …Run Code Online (Sandbox Code Playgroud) 可以使用以下方法将RecyclerView捕捉到其中心:
LinearSnapHelper().attachToRecyclerView(recyclerView)
Run Code Online (Sandbox Code Playgroud)
例:
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val inflater = LayoutInflater.from(this)
recyclerView.adapter = object : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val textView = holder.itemView as TextView
textView.setBackgroundColor(if (position % 2 == 0) 0xffff0000.toInt() else 0xff00ff00.toInt())
textView.text = position.toString()
}
override fun getItemCount(): Int {
return 100
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
val view = inflater.inflate(android.R.layout.simple_list_item_1, parent, false) as TextView
val cellSize = …Run Code Online (Sandbox Code Playgroud)