【发布时间】:2018-11-09 00:52:28
【问题描述】:
我的布局中有两个 ScrollView,就像这样
<ScrollView
android:id="@+id/news_page_scroller"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Extra Content -->
<android.support.v7.widget.CardView
android:id="@+id/news_comment_card"
android:layout_width="match_parent"
android:layout_height="180dp"
app:cardCornerRadius="8sp"
app:cardElevation="@dimen/elevation_card">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/news_comment_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Comments"
android:textColor="@color/colorAccent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="@+id/news_comment_section"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/news_comment_expand"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/news_comment_header"/>
<Button
android:id="@+id/news_comment_expand"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/show_all_comments"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>
</ScrollView>
我最初在我的活动commentSection.setOnTouchListener(new IgnoreTouch()); 中使用以下代码禁用news_comment_section。
IgnoreTouch 是一个帮助类,它会像这样忽略触摸事件
private class IgnoreTouch implements View.OnTouchListener{
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
}
这可以禁用commentsList 的滚动。当单击按钮news_comment_expand 时,CardView news_comment_card 会扩展它的高度以填充屏幕,我调用以下命令
newsPageScroller.setOnTouchListener(new IgnoreTouch());
newsPageScroller.requestDisallowInterceptTouchEvent(true);
commentSection.setOnTouchListener(null);
这会禁用news_page_scroller 并启用news_comment_section。这一直有效,直到我到达news_comment_section 的顶部或底部。如果我在到达列表的顶部或底部后继续滚动news_comment_section,则父 ScrollView news_page_scroller 开始滚动。如何完全禁用父 ScrollView news_page_scroller 上的任何滚动?
【问题讨论】:
标签: android android-scrollview