如何禁用RecyclerView滚动?

我无法禁止在中滚动RecyclerView。我尝试打电话,rv.setEnabled(false)但仍然可以滚动。


如何禁用滚动?


烙印99
浏览 6808回答 3
3回答

慕森王

REAL REAL的答案是: 对于API 21和更高版本:无需Java代码。 您可以android:nestedScrollingEnabled="false" 在xml中设置 :<android.support.v7.widget.RecyclerView&nbsp; &nbsp; &nbsp;android:id="@+id/recycler"&nbsp; &nbsp; &nbsp;android:layout_width="match_parent"&nbsp; &nbsp; &nbsp;android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp;android:clipToPadding="true"&nbsp; &nbsp; &nbsp;android:nestedScrollingEnabled="false"&nbsp; &nbsp; &nbsp;tools:listitem="@layout/adapter_favorite_place">

慕虎7371278

这有点棘手,但可行。您可以在中启用/禁用滚动RecyclerView。这是一次空洞的RecyclerView.OnItemTouchListener窃取,每一次触摸事件都会使目标无效RecyclerView。public class RecyclerViewDisabler implements RecyclerView.OnItemTouchListener {&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onTouchEvent(RecyclerView rv, MotionEvent e) {&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {&nbsp; &nbsp; }}使用它:RecyclerView rv = ...RecyclerView.OnItemTouchListener disabler = new RecyclerViewDisabler();rv.addOnItemTouchListener(disabler);&nbsp; &nbsp; &nbsp; &nbsp; // disables scolling// do stuff while scrolling is disabledrv.removeOnItemTouchListener(disabler);&nbsp; &nbsp; &nbsp;// scrolling is enabled again&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android