我有一个特定的要求是将两个ScrollView添加到FrameLayout。后台的ScrollView应显示图像列表,并与用户触摸反馈一起滚动。
顶部的ScrollView应使用smoothScrollTo(x,y)方法以编程方式滚动,具体取决于背景滚动视图位置。
为此,我必须在顶部禁用ScrollView的滚动事件,并允许背景ScrollView与用户触摸事件一起滚动。
我可以在顶部禁用scrollView的滚动事件,但无法在后台滚动ScrollView。需要帮助。
我粘贴了下面的示例布局和java代码。
活动布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="#ff0000" />
<View
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="#ffff00" />
<View
android:id="@+id/imageView3"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="#00ffff" />
</LinearLayout>
</ScrollView>
<ScrollView
android:id="@+id/list_scrollview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</ScrollView>
</FrameLayout>
&#13;
活动代码
package com.javatechig.overscroll;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ScrollView textScrollView = (ScrollView) findViewById(R.id.list_scrollview);
textScrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
ScrollView backgroundScrollView = (ScrollView) findViewById(R.id.scrollView);
backgroundScrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
}
}
&#13;
修改-1 我通过调用backgroundScrollView.onTouchEvent(event)解决了这个问题;来自textScrollView onTouch()方法。但是合适吗?或者我们还有其他办法吗?
textScrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
backgroundScrollView.onTouchEvent(event);
return true;
}
});
答案 0 :(得分:1)
我通过调用backgroundScrollView.onTouchEvent(event)解决了这个问题。 它很棒。
textScrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
backgroundScrollView.onTouchEvent(event);
return true;
}
});