Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6536023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:26:33+00:00 2026-05-25T10:26:33+00:00

I am trying to create a horizontal listview of webviews. As suggested by a

  • 0

I am trying to create a horizontal “listview” of webviews. As suggested by a previous answer to a question, I am using the ViewPager library from the compatibility pack.

Unfortunately when I go to add my webview to the view collection, using

((ViewPager) collection).addView((View)mainContent,0);

I get the casting issue.

09-07 20:45:30.690: ERROR/AndroidRuntime(8166): java.lang.ClassCastException: android.webkit.WebView cannot be cast to android.support.v4.view.ViewPager

This crashes the app.

Now it almost makes sense, casting different data types makes things go wrong, but in a ViewPager example app I had, they are casting TextView to the ViewPager which is a view…

I just don’t know what other way to actually “store” my views and display them in the app! I have

private class AwesomePagerAdapter extends PagerAdapter{
    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    @Override
    public int getCount() {
        return NUM_AWESOME_VIEWS;
    }


    @Override
    public Object instantiateItem(View collection, int position) {

        ViewPager viewPager = (ViewPager) collection;

        collection = layoutInflater.inflate(R.layout.maincontentview, null);

        WebView mainContent = (WebView)collection.findViewById(R.id.mainContent);

        JSInterface jsInterface = new JSInterface(mainContent);


        Display display = getWindowManager().getDefaultDisplay(); 
        int width = display.getWidth();
        int height = display.getHeight();
        //jsInterface.fit(width, height);
        //jsInterface.flip(width, height);

        WebSettings webSettings = mainContent.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setPluginsEnabled(true);
        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setSupportZoom(false);

        mainContent.setVerticalScrollBarEnabled(false);
        mainContent.setHorizontalScrollBarEnabled(false);

        /*mainContent.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                return(event.getAction() == MotionEvent.ACTION_MOVE);
            }
        });*/

        mainContent.setWebViewClient(new WebViewClient()
        {
            @Override
            public void onPageFinished(WebView view, String url) 
            {
                 /*mainContent.loadUrl("javascript:var script = document.createElement('script');" +  
                            "script.type = 'text/javascript';" +
                            "script.src = 'jquery.min.js';" + 
                            "document.getElementsByTagName('head')[0].appendChild(script);");*/
                WebView mainContent = (WebView)view.findViewById(R.id.mainContent);
                 Display display = getWindowManager().getDefaultDisplay(); 
                 int width = display.getWidth();
                 int height = display.getHeight();
                 mainContent.loadUrl("javascript:fit("+width+","+height+");");
                 mainContent.loadUrl("javascript:flip("+width+","+height+");");

            }
        });

        mainContent.setWebChromeClient(new WebChromeClient()
        {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) 
            {
                Log.e("alert triggered", message);
                return false;         
            }
        });

        //use load file from string, article[i].html
        mainContent.loadUrl("file:///android_asset/cache_manifest_test.html");

        ((ViewPager) collection).addView((View)mainContent,0);

        return mainContent;
    }

    /**
     * Remove a page for the given position.  The adapter is responsible
     * for removing the view from its container, although it only must ensure
     * this is done by the time it returns from {@link #finishUpdate()}.
     *
     * @param container The containing View from which the page will be removed.
     * @param position The page position to be removed.
     * @param object The same object that was returned by
     * {@link #instantiateItem(View, int)}.
     */
    @Override
    public void destroyItem(View collection, int position, Object view) {
        ((ViewPager) collection).removeView((WebView) view);
    }



    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==((WebView)object);
    }


    /**
     * Called when the a change in the shown pages has been completed.  At this
     * point you must ensure that all of the pages have actually been added or
     * removed from the container as appropriate.
     * @param container The containing View which is displaying this adapter's
     * page views.
     */
    @Override
    public void finishUpdate(View arg0) {}


    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {}

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate(View arg0) {}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:26:33+00:00Added an answer on May 25, 2026 at 10:26 am

    this works… I’ve had to cut some of the webview stuff down as i was getting errors with that.

    The main problem you had was using the ‘collection’ view rather than creating a new one.

    package com.msi.awesomepager;
    
    import android.app.Activity;
    import android.content.Context;   
    import android.os.Bundle;
    import android.os.Parcelable;
    import android.support.v4.view.PagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.util.Log;
    import android.view.Display;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.webkit.JsResult;
    import android.webkit.WebChromeClient;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.TextView;
    
    public class AwesomePagerActivity extends Activity {
    
        private ViewPager awesomePager;
        private static int NUM_AWESOME_VIEWS = 20;
        private Context context;
        private AwesomePagerAdapter awesomeAdapter;
        LayoutInflater inflater ;
    
    
    
        /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        context = this;
    
        inflater = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
        awesomeAdapter = new AwesomePagerAdapter();
        awesomePager = (ViewPager) findViewById(R.id.awesomepager);
        awesomePager.setAdapter(awesomeAdapter);
    
    }
    
    private class AwesomePagerAdapter extends PagerAdapter{
    
        @Override
        public int getCount() {
            return NUM_AWESOME_VIEWS;
        }
    
    
        @Override
        public Object instantiateItem(View collection, int position) {
    
            View layout; 
            LayoutInflater mInflater =  (LayoutInflater)
                    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            layout  = mInflater.inflate(R.layout.maincontentview, null);
    
            WebView mainContent = (WebView)layout.findViewById(R.id.mainContent);
    
            mainContent.getSettings().setJavaScriptEnabled(true);
    
            WebSettings webSettings = mainContent.getSettings();
            webSettings.setJavaScriptEnabled(true);
            webSettings.setBuiltInZoomControls(true);
            mainContent.requestFocusFromTouch();
    
            mainContent.setWebViewClient(new WebViewClient());
            mainContent.setWebChromeClient(new WebChromeClient());
    
            mainContent.loadUrl("file:///android_asset/test.html");
    
            ((ViewPager) collection).addView((View)layout,0);
    
            return layout;
        }
    
        /**
         * Remove a page for the given position.  The adapter is responsible
         * for removing the view from its container, although it only must ensure
         * this is done by the time it returns from {@link #finishUpdate()}.
         *
         * @param container The containing View from which the page will be removed.
         * @param position The page position to be removed.
         * @param object The same object that was returned by
         * {@link #instantiateItem(View, int)}.
         */
        @Override
        public void destroyItem(View collection, int position, Object view) {
            ((ViewPager) collection).removeView((View) view);
        }
    
    
    
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==((View)object);
        }
    
    
        /**
         * Called when the a change in the shown pages has been completed.  At this
         * point you must ensure that all of the pages have actually been added or
         * removed from the container as appropriate.
         * @param container The containing View which is displaying this adapter's
         * page views.
         */
        @Override
        public void finishUpdate(View arg0) {}
    
    
        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {}
    
        @Override
        public Parcelable saveState() {
            return null;
        }
    
        @Override
        public void startUpdate(View arg0) {}
    
       }
    
    }
    

    Hope this helps. M

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a horizontal list from a nested list markup, as an
I am trying to create a scrollable horizontal list using iscroll. The list seems
I'm trying to create a horizontal 100% stacked bar graph using HTML and CSS.
im trying to create a horizontal scrolling box to create a timeline effect... but
I'm trying to create a horizontal menu with a thick border bar that shows
I'm trying to create a two-level horizontal navigation menu (or menubar) that displays the
I'm trying to create a Rhombus style links menu which goes horizontal across the
I'm currently trying to create a tweet button with the horizontal count feature dynamically:
I've implemented horizontal page view with many images. And I'm trying to create a
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.