0

我想分离/附加我的片段,但是在附加后如何设置该片段不会重新创建。

在片段中,我有 WebView;当我选择和取消选择选项卡时,webview 加载起始页。

有我的代码:

  public class MainActivity extends Activity implements OnClickListener, OnMenuItemClickListener {


    ActionBar bar;
    View v;

     public static TextView tilt;
    LayoutInflater inflater;



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

        setContentView(R.layout.main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        onAddTab();
        View v=getLayoutInflater().inflate(R.layout.action_bar, null);
         ImageButton im = (ImageButton)v.findViewById(R.id.tab_toggle);
        im.setOnClickListener(this);
        getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_layout));
        getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getActionBar().setCustomView(v);
        onToggleTabs();



        }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()){
        case R.id.tab_toggle:

            onAddTab();

            break;
        }

    }

    public void onAddTab() {
        final ActionBar bar = getActionBar();
        View v=getLayoutInflater().inflate(R.layout.layout_tab, null);
        tilt = (TextView)v.findViewById(R.id.tit_le);

        ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT); 
        lp.gravity = Gravity.CENTER;
        v.setLayoutParams(lp);
        closetab = (ImageButton)v.findViewById(R.id.close);
        closetab.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                onRemoveTab();


            }
        });

        bar.addTab(bar.newTab()
                .setCustomView(v)
                .setTabListener(new TabListener<Web>(this, "Tag A", Web.class)));

    }



    public void onRemoveTab() {
        final ActionBar bar = getActionBar();
        Tab tab = bar.getSelectedTab();

            bar.removeTab(tab);

    }

    public void onToggleTabs() {
        final ActionBar bar = getActionBar();


            bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


    }

    public void onRemoveAllTabs(View v) {
        getActionBar().removeAllTabs();
    }




    public static class TabListener<T extends Fragment>  implements ActionBar.TabListener{

       private final Activity myActivity;
       private final String myTag;
       private final Class<T> myClass;

       public TabListener(Activity activity, String tag, Class<T> cls) {
           myActivity = activity;
           myTag = tag;
           myClass = cls;
       }

 @Override
 public void onTabSelected(Tab tab, FragmentTransaction ft) {

  Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);

  // Check if the fragment is already initialized
        if (myFragment == null) {
            // If not, instantiate and add it to the activity
            myFragment = Fragment.instantiate(myActivity, myClass.getName());
            ft.add(R.id.fragment0, myFragment, myTag);
        } else {
            // If it exists, simply attach it in order to show it
            ft.attach(myFragment);
        }

 }

 @Override
 public void onTabUnselected(Tab tab, FragmentTransaction ft) {

  Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);

  if (myFragment != null) {
            // Detach the fragment, because another one is being attached
            ft.detach(myFragment);

        }

 }

 @Override
 public void onTabReselected(Tab tab, FragmentTransaction ft) {
  // TODO Auto-generated method stub

 }
    }

有片段:

public class Web extends Fragment implements OnLongClickListener, OnClickListener{


@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    c=this.getActivity();

    v = inflater.inflate(R.layout.activity_main, container, false);
    return v;
}

@SuppressWarnings("deprecation")
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
setRetainInstance(true);


wv = (WebView)v.findViewById(R.id.wv);
wv.setWebChromeClient(new WebChromeClient(){
    @Override
    public void onProgressChanged(WebView view, int progress) {
        // TODO Auto-generated method stub
        super.onProgressChanged(view, progress);

               if(progress < 100 && pr.getVisibility() == ProgressBar.GONE){
                   pr.setVisibility(ProgressBar.VISIBLE);

               }
               pr.setProgress(progress);
               if(progress == 100) {
                   pr.setVisibility(ProgressBar.GONE);

               }
            }
});
wv.setWebViewClient(new MyWebViewClient());











wv.loadUrl("http://www.google.com");
wv.setOnLongClickListener(this);}
4