想要在每个列表项中单击打开WebView吗?

时间:2016-06-20 12:46:53

标签: java android xml android-layout listview

我想在每次单击List项目时打开一个静态HTML文件,但是当我调用listview时,它就消失了。请帮我解决这个问题。 我的Tab1Fragment.java代码就是这个

 public View onCreateView(LayoutInflater inflater, ViewGroup container,  
                             Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_tab1, container, false);

    Resources res = getResources();
    mainTopic = res.getStringArray(R.array.MainTopic);
    mainDescription = res.getStringArray(R.array.DescriptionContent);
    listView = (ListView) view.findViewById(R.id.listView);
    MyAdapter myAdapter = new MyAdapter(getActivity(), mainTopic, image1,                
 mainDescription);
        listView.setAdapter(myAdapter);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                switch (position) {
                    case 0:
                        wb=(WebView)view.findViewById(R.id.webView);
                        wb.loadUrl("file:///assets/ActivePassive.html");
                        wb.getSettings().setJavaScriptEnabled(true);
                        wb.setWebViewClient(new WebViewClient());
                }
            }
        });

        return view;


    }
}

我的fragment_tab1.xml代码是: -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="androidthirst.company.abhi.totalenglish.Tab1Fragment">

    <!-- TODO: Update blank fragment layout -->

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />



</RelativeLayout>

我的单独listView代码是这样的 single_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#1A237E"
        android:gravity="center"
        android:textStyle="bold"
        android:textSize="30sp"
        android:layout_margin="9dp"
        android:textColor="#ffffff"
        android:id="@+id/imageView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"

        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/imageView"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/textView2"
        android:layout_toRightOf="@+id/imageView"
        android:layout_below="@+id/textView"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignBottom="@+id/imageView" />
    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView">

    </WebView>


</RelativeLayout>

Here you can see the image i just want to open thml file in first list Item Click

3 个答案:

答案 0 :(得分:1)

尝试Bellow代码:

list_Id.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View v,
                                        int position, long id) {
                    list_Id.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View v,
                                                int position, long id) {
                            WebView webView = new WebView(v.getContext());
                            String[] urls = getResources().getStringArray(R.array.bookmark_urls);
                            webView.loadUrl(urls[position]);
                        }
                    });
                }
            });

string-array.xml内创建一个文件values,并将所有网址设置如下:

<resources>
    <string-array name="bookmark_urls">
        <item>http://www.google.com</item>
        <item>http://www.android.com/</item>
        <item>http://www.facebook.com/</item>
        <item>http://www.android.com/</item>
        <item>http://www.android.com/</item>
    </string-array>
</resources>

答案 1 :(得分:0)

您的布局是&#34; fragment_tab1&#34;

View view = inflater.inflate(R.layout.fragment_tab1, container, false);

当你想要布局名称&#34;我的 tab1_fragment.xml 代码是: - &#34;

请检查确切的布局?

你的Webview在Row文件中,用于适配器类..(single_row.xml) 你试图加载活动..

谢谢你。

答案 2 :(得分:0)

您可以使用以下内容

  String url = "http://www.example.com";
  Intent i = new Intent(Intent.ACTION_VIEW);
  i.setData(Uri.parse(url));
  startActivity(i);