Question

I'm trying to find out if WebView scroll has reached end. I'm trying to find it by using below code. I'm getting ClassCastException. Please help me out:

This is interface:

public interface ScrollViewListener {
void onScrollChanged(ScrollViewExt scrollView, 
        int x, int y, int oldx, int oldy);
}

This is class which extends 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);
    }
}
}

This is my activity class:

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---->>>");
        }

}

}

This is my layout 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>

This is my exception:

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
Was it helpful?

Solution

You have

 <ScrollView  // its a scorllview
    android:id="@+id/scrollTest"

But when you initialize you have

  sv = (ScrollViewExt) findViewById(R.id.scrollTest); 
  // casting to ScrollViewExt which should be ScrollView

If you have ScrollView in xml then initialize it as

  sv = (ScrollView) findViewById(R.id.scrollTest); 

or

  <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 >

OTHER TIPS

Instead of using ScrollView in your layout, you have to make your element the fully namespace qualified name of 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>

Try to Replace ScrollView in your Xml Layout with ScrollViewExt

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top