2017-07-31 58 views
0

我在这里寻找一些方法来做我想做的事,我尝试了一些代码,但是没有成功。我有一个应用程序底部酒吧(3 itens),我有一个片段为每一个。这3个片段用webview加载一个布局(当然是3个不同的链接)。我想要的是在片段之间切换,并将其保留在后台,因为当我将片段1留给片段2,并且我回到片段1时,片段1再次加载,我只想保留它,其他片段太。我怎样才能做到这一点 ? 感谢您的帮助!当调用它时加载片段并在切换到另一个片段/活动时保持

活动主XML

<LinearLayout 
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:id="@+id/container" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context="bandeira.thalisson.appExample.MainActivity"> 

<FrameLayout 
    android:id="@+id/content" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1"> 
</FrameLayout> 

<android.support.design.widget.BottomNavigationView 
    android:id="@+id/navigation" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom" 
    android:background="?android:attr/windowBackground" 
    app:menu="@menu/navigation"/> 

主要活动的Java

public class MainActivity extends AppCompatActivity { 

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener 
     = new BottomNavigationView.OnNavigationItemSelectedListener() { 

    @Override 
    public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
     Fragment selectedFragment = null; 
     switch (item.getItemId()) { 
      case R.id.navigation_item1: 
       selectedFragment = Fragment1.newInstance(); 
       getSupportFragmentManager().beginTransaction().replace(R.id.content, selectedFragment).commit(); 
       return true; 
      case R.id.navigation_item2: 
       selectedFragment = Fragment2.newInstance(); 
       getSupportFragmentManager().beginTransaction().replace(R.id.content, selectedFragment).commit(); 
       return true; 
      case R.id.navigation_item3: 
       selectedFragment = Fragment3.newInstance(); 
       getSupportFragmentManager().beginTransaction().replace(R.id.content, selectedFragment).commit(); 
       return true; 
     } 
     return false; 
    } 

}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    getSupportActionBar().hide(); 

    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); 
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); 

    //*Iniciar um fragmento junto com o aplicativo 
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
    transaction.replace(R.id.content, Fragment1.newInstance()); 
    transaction.commit(); 
} 

片段实施例的Java

public class Google extends Fragment { 

public WebView myWebView; 


public static Google newInstance() { 
    Googlefragment = new Google(); 
    return fragment; 
} 

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.webview, container, false); 
    myWebView = (WebView) rootView.findViewById(R.id.WebViewLayout); 
    myWebView.loadUrl("http://google.com/"); 

    //*Ativar JavaScript 
    WebSettings webSettings = myWebView.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 


    //*Forçar links para abrir no WebView ao invés do navegador 
    myWebView.setWebViewClient(new WebViewClient()); 

    return rootView; 
} 

片段XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<WebView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/WebViewLayout"/> 

回答

0

而不是使用取代每次在你onNavigationItemSelected的,使用显示隐藏

编程创建大概所有的碎片在你的onCreate,并在您的项目选择的方法,而不是这个

getSupportFragmentManager().beginTransaction().replace(R.id.content, selectedFragment).commit(); 

你做

getSupportFragmentManager().beginTransaction().show(R.id.content, selectedFragment).commit(); 

,同样隐藏

getSupportFragmentManager().beginTransaction().hide(R.id.content, selectedFragment).commit(); 

在这种情况下,它会创建只有一次(第一次),并随后将只显示和隐藏(想想在可见GONE不是说其同,但类似的术语)

+0

该应用程序启动正常,但是当我选择一个项目来加载另一个片段我得到强制关闭。你能写一个简单的代码(或者我自己的代码)吗?! –

相关问题