如何在 android 中的主 detail/flow 中的另一个网页中加载 html 页面

how to load a html page inside another web page in master detail/flow in android

我遇到了一个我无法解决的问题,我在谷歌上搜索了很多但找不到解决方案。

我使用 master detail/flow 制作了一个应用程序。只要选择一个类别项目,此应用程序就会打开一个网页,但是当我在网页中单击另一个 link 时,它会重定向到浏览器并在浏览器中打开 link。有没有办法在应用程序本身中打开所有 link? 非常感谢所有帮助!我在这里完全没有想法。 谢谢。

WebpageDetailFragment.java

public class WebpageDetailFragment extends Fragment {

    public static final String ARG_ITEM_ID = "item_id";

    private DummyContent.DummyItem mItem;

    public WebpageDetailFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments().containsKey(ARG_ITEM_ID)) {
            mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));

            Activity activity = this.getActivity();
            CollapsingToolbarLayout appBarLayout = (CollapsingToolbarLayout) activity.findViewById(R.id.toolbar_layout);
            if (appBarLayout != null) {
                appBarLayout.setTitle(mItem.item_name);
            }
        }
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.webpage_detail, container, false);

        // Show the dummy content as text in a TextView.
        if (mItem != null) {
            ((WebView) rootView.findViewById(R.id.detail_area)).loadUrl(mItem.url);
        }
        return rootView;
    }
}

我有另一个问题,当我添加您的代码时,相同的页面在 WebView 中加载,重定向到浏览器的问题已解决,但它没有加载所需的页面。这是我的代码:-

DummyContent.java

public class DummyContent {

    public static final List<DummyItem> ITEMS = new ArrayList<>();

    public static final Map<String, DummyItem> ITEM_MAP = new HashMap<>();

    static {
       addItem(new DummyItem("1", "Home", "http://techonaclick.com/"));
        addItem(new DummyItem("2", "About Us", "http://techonaclick.com/about-2/"));
        addItem(new DummyItem("3", "Contact Us", "http://techonaclick.com/contact-us/"));
        addItem(new DummyItem("4", "Disclaimer", "http://techonaclick.com/disclaimer/"));
        addItem(new DummyItem("5", "Privacy Policy", "http://techonaclick.com/privacy-policy/"));
    }

    private static void addItem(DummyItem item) {
        ITEMS.add(item);
        ITEM_MAP.put(item.id, item);
    }

    public static class DummyItem {
        public final String id;
        public final String item_name;
        public final String url;

        public DummyItem(String id, String item_name, String url) {
            this.id = id;
            this.item_name = item_name;
            this.url = url;
        }

        @Override
        public String toString() {
            return item_name;
        }
    }
}

The place where i inserted your code

添加一个新的 WebViewClient() 到您的主 WebView 以处理所有链接。

来自documentation

When the user clicks a link from a web page in your WebView, the default behavior is for Android to launch an application that handles URLs. Usually, the default web browser opens and loads the destination URL. However, you can override this behavior for your WebView, so links open within your WebView. You can then allow the user to navigate backward and forward through their web page history that's maintained by your WebView.


示例:

myWebView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
});

你的情况:

在您的 Fragment class:

中的任意位置添加 此代码段
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    WebView webView = (WebView) view.getRootView().findViewById(R.id.detail_area);
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });
}