如何WebView在Android中实现Scroll Listener
我试过这个,但它没有调用我Log.i滚动webview.
package com.example.webview.full.width;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.webkit.WebView;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
public class scorllableWebview extends WebView implements OnScrollListener {
Context ctx;
AttributeSet atrs;
public scorllableWebview(Context context) {
super(context);
ctx = context;
}
public scorllableWebview(Context context, AttributeSet atters){
super(context, atters);
ctx = context;
atrs = atters;
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
Log.i("onScroll", "Called");
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
Log.i("onScrollStateChanged", …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我有一个ScrollView,其中包含一些线性视图,一些textview和一个Webview,然后是其他线性布局等.问题是WebView不会滚动.Scroll仅侦听ScrollView.有什么建议??
<ScrollView >
<TextView />
<WebView /> <-- this does not scroll
<TextView />
</ScrollView >
Run Code Online (Sandbox Code Playgroud) 我创建了一个活动,其中包含一个地图,图像和其他文本视图,我为它添加了"scrollview"标签...但是在活动开始后它会自动滚动到页面的末尾....请告诉我我为什么会发生这种情况以及如何阻止它形成移动结束所以我可以自己滚动.
这是我的代码:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.radikallab.earch.DetailsActivity"
android:background="@drawable/background10">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:text="Title"
android:textStyle="italic"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/background4"
android:id="@+id/iv"/>
<TextView
android:id="@+id/rat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Title"
android:textStyle="italic"/>
<TextView
android:id="@+id/addr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17sp"
android:text="Title"
android:textStyle="italic"/>
<WebView
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_marginTop="10dp"
android:id="@+id/webView"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我有一个用于测试目的的网页(https://storage.googleapis.com/htmltestingbucket/nested_scroll_helper.html),它只打印html在固定标题中捕获的滚动事件的计数器
当Android WebView是片段中唯一可滚动的元素时,一切都很好,WebView将滚动事件发送到页面
如果我想在WebView的上方和下方添加原生元素,那么事情会变得复杂得多.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="SOMETHING ABOVE THE WEBVIEW" />
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="SOMETHING BELOW THE WEBVIEW" />
</LinearLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
我知道在ScrollView中使用WebView并不好,但我必须在html文档中提供混合内容和正确滚动事件的单一滚动体验.我发现了很多关于此事的问题,但我能够创建一个完整的端到端解决方案
此外,我知道lint有一个官方的检查:
NestedScrolling ---------------摘要:嵌套滚动小部件
优先级:7/10严重性:警告类别:正确性
滚动窗口小部件(如ScrollView)不应包含任何嵌套滚动窗口小部件,因为它具有各种可用性问题
然而,我无法在本机中实现Web视图内容,因此我需要另一种方法来实现
我有什么:
现在我有一个Scroll视图作为父母.在这个滚动视图中,我使用的是一个加载URL然后在其中显示文本的WebView.这是我的xml:
<ScrollView
android:id="@+id/parentScroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/Heading" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp" >
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@drawable/webview" />
</RelativeLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
我想要的是:
我想在其中滚动webView.当我触摸webView时,很遗憾地会调用父滚动视图.我还要保留父滚动视图,但除此之外,我想在我触摸webView时滚动其中的WebView内容.
我怎样才能做到这一点?