我试图找出WebView
滚动是否已经结束。我试图通过使用下面的代码找到它。我得到了ClassCastException
。请帮帮我:
这是界面:
public interface ScrollViewListener {
void onScrollChanged(ScrollViewExt scrollView,
int x, int y, int oldx, int oldy);
}
这是扩展ScrollView
:
public class ScrollViewExt extends ScrollView {
private ScrollViewListener scrollViewListener = null;
public ScrollViewExt(Context context) {
super(context);
}
public ScrollViewExt(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ScrollViewExt(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setScrollViewListener(ScrollViewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (scrollViewListener != null) {
scrollViewListener.onScrollChanged(this, l, t, oldl, oldt);
}
}
}
这是我的活动课程:
public class MainActivity extends Activity implements ScrollViewListener{
ScrollViewExt sv;
WebView wv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sv = (ScrollViewExt) findViewById(R.id.scrollTest);
sv.setScrollViewListener(this);
wv = (WebView) findViewById(R.id.webTest);
wv.loadUrl("some url");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onScrollChanged(ScrollViewExt scrollView, int x, int y,
int oldx, int oldy) {
// TODO Auto-generated method stub
View view = (View) scrollView.getChildAt(scrollView.getChildCount() - 1);
int diff = (view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY()));
// if diff is zero, then the bottom has been reached
if (diff == 0) {
// do stuff
System.out.println("in here---->>>");
}
}
}
这是我的布局xml:
<RelativeLayout 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:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ScrollView
android:id="@+id/scrollTest"
android:layout_height="600dp"
android:layout_width="match_parent">
<WebView
android:id="@+id/webTest"
android:layout_height="600dp"
android:layout_width="match_parent"/>
</ScrollView>
</RelativeLayout>
这是我的例外:
03-11 10:55:58.744: E/AndroidRuntime(27404): FATAL EXCEPTION: main
03-11 10:55:58.744: E/AndroidRuntime(27404): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.scrolltest/com.example.scrolltest.MainActivity}: java.lang.ClassCastException: android.widget.ScrollView cannot be cast to com.demo.listener.ScrollViewExt
03-11 10:55:58.744: E/AndroidRuntime(27404): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-11 10:55:58.744: E/AndroidRuntime(27404): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-11 10:55:58.744: E/AndroidRuntime(27404): at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-11 10:55:58.744: E/AndroidRuntime(27404): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-11 10:55:58.744: E/AndroidRuntime(27404): at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 10:55:58.744: E/AndroidRuntime(27404): at android.os.Looper.loop(Looper.java:137)
03-11 10:55:58.744: E/AndroidRuntime(27404): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-11 10:55:58.744: E/AndroidRuntime(27404): at java.lang.reflect.Method.invokeNative(Native Method)
03-11 10:55:58.744: E/AndroidRuntime(27404): at java.lang.reflect.Method.invoke(Method.java:511)
03-11 10:55:58.744: E/AndroidRuntime(27404): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-11 10:55:58.744: E/AndroidRuntime(27404): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-11 10:55:58.744: E/AndroidRuntime(27404): at dalvik.system.NativeStart.main(Native Method)
03-11 10:55:58.744: E/AndroidRuntime(27404): Caused by: java.lang.ClassCastException: android.widget.ScrollView cannot be cast to com.demo.listener.ScrollViewExt
03-11 10:55:58.744: E/AndroidRuntime(27404): at com.example.scrolltest.MainActivity.onCreate(MainActivity.java:21)
03-11 10:55:58.744: E/AndroidRuntime(27404): at android.app.Activity.performCreate(Activity.java:5008)
03-11 10:55:58.744: E/AndroidRuntime(27404): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-11 10:55:58.744: E/AndroidRuntime(27404): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-11 10:55:58.744: E/AndroidRuntime(27404): ... 11 more
答案 0 :(得分:2)
你有
<ScrollView // its a scorllview
android:id="@+id/scrollTest"
但是当你初始化时,你有
sv = (ScrollViewExt) findViewById(R.id.scrollTest);
// casting to ScrollViewExt which should be ScrollView
如果您在xml中有ScrollView,则将其初始化为
sv = (ScrollView) findViewById(R.id.scrollTest);
或
<yourpackagename.ScrollViewExt
android:id="@+id/scrollTest"
android:layout_height="600dp"
android:layout_width="match_parent">
<WebView
android:id="@+id/webTest"
android:layout_height="600dp"
android:layout_width="match_parent"/>
</yourpackagename.ScrollViewExt >
答案 1 :(得分:1)
不必在布局中使用ScrollView,而是必须使元素成为ScrollViewExt的完全命名空间限定名称
<com.demo.listener.ScrollViewExt
android:id="@+id/scrollTest"
android:layout_height="600dp"
android:layout_width="match_parent">
<WebView
android:id="@+id/webTest"
android:layout_height="600dp"
android:layout_width="match_parent"/>
</com.demo.listener.ScrollViewExt>
答案 2 :(得分:1)
尝试替换 ScrollView在您的Xml布局中使用ScrollViewExt