使WebView与主详细信息一起使用

时间:2017-08-20 14:20:45

标签: java android android-webview master-detail

这适用于Android Studio v2.3。我想做的就是有一个主细节流程,我点击列表中的一个项目,然后将我带到网站。在查看了所有不同的建议之后,我的应用程序仍然只是强行退出或者没有响应"每当我点击列表中的任何项目。请帮忙!

WebPageDetailFragment.java代码:

package com.example.jasperzhu.moremorelearning;

import android.app.Activity;
import android.support.design.widget.CollapsingToolbarLayout;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.jasperzhu.moremorelearning.dummy.DummyContent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * A fragment representing a single Webpage detail screen.
 * This fragment is either contained in a {@link WebpageListActivity}
 * in two-pane mode (on tablets) or a {@link WebpageDetailActivity}
 * on handsets.
 */
public class WebpageDetailFragment extends Fragment {
    /**
     * The fragment argument representing the item ID that this fragment
     * represents.
     */
    public static final String ARG_ITEM_ID = "item_id";

    /**
     * The dummy content this fragment is presenting.
     */
    private DummyContent.DummyItem mItem;

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public WebpageDetailFragment() {
    }

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

        if (getArguments().containsKey(ARG_ITEM_ID)) {
            // Load the dummy content specified by the fragment
            // arguments. In a real-world scenario, use a Loader
            // to load content from a content provider.
            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.content);
            }
        }
    }

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

        if (mItem != null) {
            WebView view = (WebView) rootView.findViewById(R.id.webpage);
            view.setWebViewClient(new WebViewClient());
            view.getSettings().setJavaScriptEnabled(true); //for some web using JavaScript like "youtube.com"
            view.loadUrl(mItem.address);
            view.setLayerType(view.LAYER_TYPE_HARDWARE, null);
        }

        return rootView;
     }
  }

DummyContent.java代码:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Helper class for providing sample content for user interfaces created by
 * Android template wizards.
 * <p>
 * TODO: Replace all uses of this class before publishing your app.
 */
public class DummyContent {

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

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

    static {
        addItem(new DummyItem("Google", "Google", "www.google.com"));
        addItem(new DummyItem("Bing", "Bing", "www.bing.com"));
        addItem(new DummyItem("Baidu", "Baidu", "www.baidu.com"));
    }

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

    private static String makeDetails(int position) {
        StringBuilder builder = new StringBuilder();
        builder.append("Details about Item: ").append(position);
        for (int i = 0; i < position; i++) {
            builder.append("\nMore details information here.");
        }
        return builder.toString();
    }

    public static class DummyItem {
        public final String id;
        public final String content;
        public String address;

        public DummyItem(String id, String content, String address) {
            this.id = id;
            this.content = content;
            this.address = address;
        }

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

再次感谢!

0 个答案:

没有答案