如何控制导航历史里面片段的WebView [英] How to control navigation history in webview inside fragment

查看:161
本文介绍了如何控制导航历史里面片段的WebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我里面有一个片段的WebView,我希望它的浏览历史,每当我preSS后退按钮来控制。

在我的情况下,当我preSS后退按钮我离开web视图,但我想,关于pressing的按钮,我将返回到在离开之前存储在导航历史(从国家web视图)。

任何解决方案吗?

我试着用安其,但它不解决我的问题。在这里,我的code:

 公共类FragmentAll扩展片段器具
        DialogInterface.OnCancelListener,DialogInterface.OnDismissListener,
        OnDownloaExpod​​Terminated {    公共静态的newInstance片段(上下文的背景下){
        FragmentAll F =新FragmentAll();
        返回F;
    }    私人的WebView myWebView;    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
            捆绑savedInstanceState){
        (((MainActivity)getActivity())).setActionBarTitle(存储);
        myWebView =(的WebView)v.findViewById(R.id.webView1);
        setWebview();
        v.setOnKeyListener(新OnKeyListener(){
            @覆盖
            公共布尔安其(查看为arg0,ARG1 INT,KeyEvent的ARG2){
                // TODO自动生成方法存根
                如果(ARG1 == KeyEvent.KEY code_BACK&放大器;&安培; myWebView.canGoBack()){
                    myWebView.goBack();
                    返回true;
                }
                返回false;
            }
        });
        返回伏;
    }    ...}


解决方案

您需要管理的链接列表中的网页视图被加载。
你可以得到这样的网址

  web_des2.setOnTouchListener(新OnTouchListener(){            @覆盖
            公共布尔onTouch(视图V,MotionEvent事件){                如果(event.getAction()== MotionEvent.ACTION_UP){
                    HitTestResult hitTestResult = web_des2.getHitTestResult();                    如果(hitTestResult!= NULL){
                        字符串URL = hitTestResult.getExtra();                        如果(URL = NULL&放大器;&安培; url.startsWith(HTTP://!)){
                                             yourUrlList.add(URL);
                                             web_des2.loadUrl(URL);
                        }                    }
                    返回true;
                }                返回false;
            }
        });

您可以检查onBack preSS如果你的列表不为空,则得到的URL形式列表,并将其加载到web视图,并从列表中删除。
您可以使用堆栈来存储URL所以变得容易,你可以直接使用pop()方法和推()函数。如果喜欢比+1

I have a webview inside fragment and I want it to be controlled by the navigation history whenever I press the back button.

In my case, when I press the back button I leave the webview, but I want that on pressing the button I will return to the state that is stored in the navigation history (from before leaving the webview).

Any solution please?

I tried with onKey but it does not resolve my problem. Here my code:

public class FragmentAll extends Fragment implements
        DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
        OnDownloaExpodTerminated {

    public static Fragment newInstance(Context context) {
        FragmentAll f = new FragmentAll();
        return f;
    }

    private WebView myWebView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        (((MainActivity) getActivity()) ).setActionBarTitle("Store");
        myWebView = (WebView) v.findViewById(R.id.webView1);
        setWebview();
        v.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
                // TODO Auto-generated method stub
                if (arg1 == KeyEvent.KEYCODE_BACK && myWebView.canGoBack()) {
                    myWebView.goBack();
                    return true;
                }
                return false;
            }
        });
        return v;
    }

    ...

}

解决方案

you need to manage list of links is loaded in your webview. you can get the url like this

    web_des2.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if (event.getAction() == MotionEvent.ACTION_UP) {
                    HitTestResult hitTestResult = web_des2.getHitTestResult();

                    if (hitTestResult != null) {
                        String url = hitTestResult.getExtra();

                        if (url != null && url.startsWith("http://")) {
                                             yourUrlList.add(url);
                                             web_des2.loadUrl(url);
                        }

                    }
                    return true;
                }

                return false;
            }
        });

you can check onBackPress if your list does not empty then get url form list and load it into webview and remove from the list. you can use Stack to store url so become easy you can use pop() and push() function directly. if like than +1

这篇关于如何控制导航历史里面片段的WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆