2017-07-31 57 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

該應用程序啓動正常,但是當我選擇一個項目來加載另一個片段我得到強制關閉。你能寫一個簡單的代碼(或者我自己的代碼)嗎?! –

相關問題