我想充分利用API21中新增的NestedScroll特性。我的布局非常简单:
HorizontalScrollView LinearLayout (明显水平)常规视图HorizontalScrollView TextView
默认情况下,nestedScrollEnabled为false。所以我在xml中为我想要优先于根HorizontalScrollView滚动的子对象(例如内部的HorizontalScrollView)启用了它。因此,它不会做任何事情。只有顶部的滚动视图能够滚动,内部的滚动视图似乎根本看不到任何滚动的触摸事件。
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.majeur.test.MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<HorizontalScrollView
android:id="@+id/scrollView2"
android:layout_width="200dp"
android:layout_height="match_parent"
android:nestedScrollingEnabled="true">
<TextView
android:layout_width="250dp"
android:layout_height="match_parent"
android:text="@string/text" />
</HorizontalScrollView>
<View
android:layout_width="200dp"
android:layout_height="match_parent"
android:background="@color/colorAccent"/>
</LinearLayout>
</HorizontalScrollView>这是它应该工作的wau,我不明白...谢谢
发布于 2016-12-23 03:26:27
您将需要使用onTouchListener拦截触摸:
// Intercept touch scroll by
transparentImageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
scrollView.requestDisallowInterceptTouchEvent(true);
// Disable touch on transparent view
return false;
case MotionEvent.ACTION_UP:
// Allow ScrollView to intercept touch events.
scrollView.requestDisallowInterceptTouchEvent(false);
return true;
case MotionEvent.ACTION_MOVE:
scrollView.requestDisallowInterceptTouchEvent(true);
return false;
default:
return true;
}
}
});而你的XML...
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.majeur.test.MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/mapLayout"
android:layout_width="match_parent"
android:layout_height="300dp" >
<HorizontalScrollView
android:id="@+id/scrollView2"
android:layout_width="200dp"
android:layout_height="match_parent"
android:nestedScrollingEnabled="true">
<TextView
android:layout_width="250dp"
android:layout_height="match_parent"
android:text="@string/text" />
</HorizontalScrollView>
<ImageView
android:id="@+id/transparent_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
<View
android:layout_width="200dp"
android:layout_height="match_parent"
android:background="@color/colorAccent"/>
</LinearLayout>
发布于 2016-12-23 03:18:35
尝试对子LinearLayout使用方向为水平的NestedScrollView。
NestedScrollView就像ScrollView一样,但它支持在新旧版本的安卓上同时充当嵌套滚动父项和子项。默认情况下,嵌套滚动处于启用状态。
https://developer.android.com/reference/android/support/v4/widget/NestedScrollView.html
https://stackoverflow.com/questions/41289957
复制相似问题