通过ListView使用webView的片段中的多个html文件

时间:2017-08-20 08:55:37

标签: android listview webview fragment

我在片段中创建了一个带有listview的webview应用程序。 我按照这个链接: Opening multiple local html files using webView via ListView 当我点击该项目时,它应该将我重定向到webview项目的详细信息。 现在我尝试在片段中实现它但是只有一个项目成功,而其他项目没有指向我应该是的webview详细信息。 我一直在寻找解决方案,但仍然没有运气。 这是我的代码

MainActivity:

package com.listviewfragment.withsublist;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Main_listFragment main_listFragment = new Main_listFragment();
    FragmentTransaction fragmentTransaction =  getSupportFragmentManager().beginTransaction();
      fragmentTransaction.replace(R.id.content, main_listFragment);
      fragmentTransaction.commit();
    }
 }

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout   xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.listviewfragment.withsublist.MainActivity">

<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>

</android.support.constraint.ConstraintLayout>

Main_listFragment.java

package com.listviewfragment.withsublist;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;


/**
 * A simple {@link Fragment} subclass.
 */
public class Main_listFragment extends Fragment {
ListView listView;


public Main_listFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main_list, container, false);
    // Inflate the layout for this fragment
    listView = (ListView) rootView.findViewById(R.id.list);
    String[] values = new String[] {
            "Detail Webview 1",
            "Detail Webview 2",
            "Detail Webview 3",
    };

    ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, values);

    // Assign adapter to ListView
    listView.setAdapter(adapter);

    // ListView Item Click Listener
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // TODO Auto-generated method stub

            // ListView Clicked item value
            listView.getItemAtPosition(position);
            if (position == 0) {
                Detail_webview_mainFragment dlm = new Detail_webview_mainFragment ();
                getFragmentManager().beginTransaction().replace(R.id.content, dlm).addToBackStack(null).commit();
            }
            else if (position == 1) {
                Detail_webview_mainFragment dlm = new Detail_webview_mainFragment ();
                getFragmentManager().beginTransaction().replace(R.id.content, dlm).addToBackStack(null).commit();
            }
            else if (position == 2) {
                Detail_webview_mainFragment dlm = new Detail_webview_mainFragment ();;
                getFragmentManager().beginTransaction().replace(R.id.content, dlm).addToBackStack(null).commit();
            }
        }

    });
    return rootView;
 }

}

fragment_main_list.xml

<FrameLayout 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="com.listviewfragment.withsublist.Main_listFragment">


    <ListView
       android:id="@+id/list"
       android:layout_width="match_parent"
       android:layout_height="match_parent">

    </ListView>

</FrameLayout>

Detail_webview_mainFragment.java

package com.listviewfragment.withsublist;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


/**
* A simple {@link Fragment} subclass.
*/
public class Detail_webview_mainFragment extends Fragment {
WebView view;

public Detail_webview_mainFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    //getArguments().getString("key");
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_detail_webview_main, container, false);

    view = (WebView) rootView.findViewById(R.id.webview);
    // Enable Javascript
    WebSettings webSettings = view.getSettings();
    webSettings.setJavaScriptEnabled(true);
    view.setWebViewClient(new WebViewClient());

    int pos = getActivity().getIntent().getIntExtra("key", 0);
    if (pos == 0) {
        view.loadUrl("file:///android_asset/html/webview1.html");
    } else if (pos == 1) {
        view.loadUrl("file:///android_asset/html/webview2.html");
    } else if (pos == 2) {
        view.loadUrl("file:///android_asset/html/webview3.html");
    }
    return rootView;

   }
}

fragment_detail_webview_main

<FrameLayout 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="com.listviewfragment.withsublist.Detail_webview_mainFragment">

<!-- TODO: Update blank fragment layout -->
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"></WebView>

</FrameLayout>

1 个答案:

答案 0 :(得分:0)

您对所有位置0,1,2使用相同的详细信息片段。就这样做:

Detail_webview_mainFragment dlm = new Detail_webview_mainFragment ();
Bundle args = new Bundle();
args.putInt("key", pos);
dlm.setArguments(args);

而不是

int pos = getActivity().getIntent().getIntExtra("key", 0);

详细片段:

int pos=0;
if (getArguments() != null) {       
 pos= getArguments().getInt("key");             
}