重新加载选项卡重新加载的片段中的Webview

时间:2017-11-29 12:41:41

标签: android android-fragments android-webview android-tabs

我有一个带有4个底部标签的应用程序,点击任意标签,加载了一个带有webview的新片段。我希望webviews只加载一次,并且在复制相同标签时webview sholud打开而不重新加载..怎么做??

我有一个带有4个底部标签的应用程序,点击任意标签,加载了一个带有webview的新片段。我希望webviews只加载一次,并且在复制相同标签时webview sholud打开而不重新加载..怎么做??

public class NavBarActivity extends Activity {
int flag;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.actionnav);
    flag=0;

    RadioButton radioButton;
    radioButton = (RadioButton) findViewById(R.id.btnAll);
    radioButton.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
    radioButton = (RadioButton) findViewById(R.id.btnPicture);
    radioButton.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
    radioButton = (RadioButton) findViewById(R.id.btnVideo);
    radioButton.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
    radioButton = (RadioButton) findViewById(R.id.btnFile);
    radioButton.setOnCheckedChangeListener(btnNavBarOnCheckedChangeListener);
}

private CompoundButton.OnCheckedChangeListener btnNavBarOnCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            Toast.makeText(NavBarActivity.this, buttonView.getText(), Toast.LENGTH_SHORT).show();
            if(flag==0){
                loadFragment(new HomeFragment());
                flag++;

            }
            else if(flag==1){
                loadFragment(new Bfragment());
                flag--;
            }
        }
    }
};


    private void loadFragment(Fragment fragment) {
// create a FragmentManager
        FragmentManager fm = getFragmentManager();
// create a FragmentTransaction to begin the transaction and replace the 
Fragment
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
// replace the FrameLayout with new Fragment
        fragmentTransaction.replace(R.id.frameLayout, fragment);
        fragmentTransaction.commit(); // save the changes
    }
}

我的片段就像​​这样

public class HomeFragment extends Fragment {


    View view;


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

        WebView webView= view.findViewById(R.id.wv);
       if(webView!=null) {
            webView.getSettings().setJavaScriptEnabled(true);

  webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);

webView.loadUrl("url");

    }
  // get the reference of Button



        return view;
    }


}

0 个答案:

没有答案