简体   繁体   English

如何将此URL加载到WebView?

[英]How to load this URL into WebView?

There is a rather specific webpage loaded into WebView which URL is like http://www.site.com/mob/ (basically a mobile-optimized web page). WebView中加载了一个相当特定的网页,其URL类似于http://www.site.com/mob/ (基本上是针对移动设备优化的网页)。 This webpage display 25 articles only and on the bottom is a button "More articles". 该网页仅显示25条文章,底部是“更多文章”按钮。

When a user presses it, I catch URL http://www.site.com/Web/MobHomeItems.aspx?page=N (where N is 2, 3, 4...) and after that another 25 items have been loaded on the same screen. 当用户按下它时,我捕获了URL http://www.site.com/Web/MobHomeItems.aspx?page=N (其中N是2、3、4 ...),此后又加载了25个项目在同一屏幕上。

Now, when I click on some article and go to article details, and later return to the page via the Back key, the WebView forgets how many articles have been loaded and simply loads the default page with 25 displayed articles. 现在,当我单击某些文章并转到文章详细信息,然后通过后退键返回页面时, WebView 忘记已加载了多少文章,而只加载了显示25条文章的默认页面。 Imagine how frustrating this would be to a user if he came to 100th article. 想象一下,如果用户来到第100条文章,这将给用户带来多大的挫败感。

I tried overriding many methods in WebClient and in WebChromeClient , but so far I have been unable to load N number of pages loaded via "More Articles" button. 我尝试覆盖WebClientWebChromeClient许多方法,但是到目前为止,我无法加载通过“更多文章”按钮加载的N个页面。 For example, I first thought this would help, but it did not. 例如,我首先认为这会有所帮助,但没有帮助。

        @Override
        public void onLoadResource(WebView view, String url) {

            //http://www.site.com/Web/MobHomeItems.aspx?page=2

            if (url.contains("?page=")) {

                //save this URL for later and on return from 
               // article details, pass it to LoadResource()

            super.onLoadResource(view, url);
        }

Then I tried similar approach with other method - basically remembering how many pages have been loaded on the main page, and then on return from article details, simply tell webview to load this URL. 然后,我尝试了与其他方法类似的方法-基本是记住主页上已加载了多少页,然后从文章详细信息返回时,只需告诉webview加载此URL。

Can anyone help me? 谁能帮我? How to append loaded pages to the main page? 如何将加载的页面追加到主页? Should I use JavaScript here maybe? 我应该在这里使用JavaScript吗?

PS. PS。 Loading mentioned URL http://www.site.com/Web/MobHomeItems.aspx?page=N does not help as it loads this concrete page into the WebView only, and it does not append this Nth page to the main page. 加载提到的URL http://www.site.com/Web/MobHomeItems.aspx?page=N并没有帮助,因为它仅将此具体页面加载到WebView ,并且没有将第N页附加到主页上。

EDIT 编辑

As @Raghunandan asked, I do not have problems loading back to 1st page ( ?page=1 ). 正如@Raghunandan所问,我没有问题可以加载回第一页( ?page=1 )。 This is default when user presses Back button on article details. 用户在文章详细信息上按“后退”按钮时,这是默认设置。 I want to load to the page where a user was before pressing article details . 我想在按文章详细信息之前加载到用户所在的页面 If he was on ?page=100 , I want to load back to that page eg I want to have 25x100 articles open. 如果他在?page=100 ,我想加载回该页面,例如,我想打开25x100的文章。 Again, default is always "open 25 articles or ?page=1 or http://www.site.com ". 同样,默认始终为“打开25篇文章或?page=1http://www.site.com ”。

Override the method shouldOverrideUrlLoading of WebViewClient. 重写WebViewClient的方法shouldOverrideUrlLoading。

like this: 像这样:

public boolean shouldOverrideUrlLoading (WebView view, String url) {
   if (url is kind of article detail) {
       WebView newOne = new WebView(); // create a new Webview for displaying the details.
       view.setVisibility(View.INVISIBLE); // hiding current page (article list)  
       return true; // To tell the WebView we have process this url.
   } 
   return false;
}
  1. The user click one link of article's detail. 用户单击文章详细信息的一个链接。
  2. shouldOverriderUrlLoading would be triggered. shouldOverriderUrlLoading将被触发。
  3. We created one new WebView to open the url. 我们创建了一个新的WebView来打开URL。
  4. Hiding current page 隐藏当前页面
  5. The user reading artical 用户阅读文章
  6. The user click back key, close the newOne WebView then make the previous WebView visible.The article list will show up immediately and remained the old statement 用户单击返回键,关闭newOne WebView,然后使以前的WebView可见。文章列表将立即显示,并保留旧的语句

.

There is a another way to do this. 还有另一种方法可以做到这一点。

The WebSettings has a private method "setPageCacheCapacity" , the default value is 0 , you could enlarge it (may be 5). WebSettings具有私有方法“ setPageCacheCapacity”,默认值为0,您可以将其放大(可以为5)。

You can access this method by using reflection of java. 您可以通过使用Java反射来访问此方法。

The method can enable WebView to cache more than one document. 该方法可以使WebView缓存多个文档。 In the other word. 换句话说。 when user press the back key, the WebView will go back to the older document. 当用户按返回键时,WebView将返回到较早的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM