2017-08-18 50 views
2

好標題說,這一切,我不知道如何來隱藏它在啓動時。我試圖尋找,但沒有發生我想要它發生的事情。如何隱藏我的MainActivity的開始(或應用)的片段?

所以,我希望發生的是,我希望BottomFragment就啓動了被隱藏。我希望它在TopFragment中單擊此按鈕時顯示,並且TopFragment在啓動時可見。

我MainActivity.xml代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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="rjj.tutorial_fragmentsthe3rd.MainActivity"> 

    <fragment 
     class="rjj.tutorial_fragmentsthe3rd.TopFragment" 
     android:id="@+id/topFragment" 
     android:layout_width="match_parent" 
     android:layout_height="300dp" /> 
    <fragment 
     class="rjj.tutorial_fragmentsthe3rd.BottomFragment" 
     android:id="@+id/bottomFragment" 
     android:layout_width="match_parent" 
     android:layout_height="300dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentStart="true" 
     android:layout_below="@+id/topFragment" /> 

</RelativeLayout> 

我TopFragment.xml代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 

    <Spinner 
     android:id="@+id/origin" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" 
     android:entries="@array/origins"/> 

    <Spinner 
     android:id="@+id/destination" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/origin" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="41dp" 
     android:entries="@array/destinations"/> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/destination" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="80dp" 
     android:text="@string/button_caption" /> 
</RelativeLayout> 

我BottomFragment.xml代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 

    <EditText 
     android:id="@+id/flightQueryResult" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="61dp" 
     android:inputType="textPersonName" 
     android:text="Name" /> 
</RelativeLayout> 

我MainActivity.java代碼:

package rjj.tutorial_fragmentsthe3rd; 

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

public class MainActivity extends AppCompatActivity implements TopFragment.FlightSearcher{ 

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

    } 

    @Override 
    public void searchForFlights(String origin, String destination) { 
     BottomFragment bottomFragment = (BottomFragment)getSupportFragmentManager() 
       .findFragmentById(R.id.bottomFragment); 
     int randomPrice = (int)(Math.random()*200); 
     String resultString = origin + " - " + destination + " = " + randomPrice; 
     bottomFragment.displayFlightQueryResult(resultString); 

    } 
} 

我TopFragment.java代碼:

package rjj.tutorial_fragmentsthe3rd; 

import android.content.Context; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.Spinner; 

public class TopFragment extends Fragment { 

    private FlightSearcher interfaceImplementer; 
    public interface FlightSearcher{ 
     public void searchForFlights(String origin, String destination); 
    } 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     this.interfaceImplementer =(FlightSearcher)context; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.fragment_top, container, false); 
    } 

    @Override 
    public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     Button flightSearchButton = (Button)getActivity().findViewById(R.id.button); 
     flightSearchButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Spinner originSpinner = (Spinner)getActivity().findViewById(R.id.origin); 
       Spinner destinationSpinner = (Spinner)getActivity().findViewById(R.id.destination); 

       interfaceImplementer.searchForFlights(originSpinner.getSelectedItem().toString(),destinationSpinner.getSelectedItem().toString()); 
      } 
     }); 
    } 
} 

我BottomFragment代碼:

package rjj.tutorial_fragmentsthe3rd; 

import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.EditText; 


public class BottomFragment extends Fragment { 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.fragment_bottom, container, false); 
    } 

    public void displayFlightQueryResult(String result){ 
     EditText resultField = (EditText)getActivity().findViewById(R.id.flightQueryResult); 
     resultField.setText(result); 
    } 
} 

回答

1

在啓動隱藏片段是這樣的:

FragmentManager manager = getSupportFragmentManager(); 
FragmentTransaction ft = manager.beginTransaction(); 
Fragment bottomFragment = manager.findFragmentById(R.id.bottomFragment); 
ft.hide(bottom); 
ft.commit(); 

然後你就可以再次顯示它:

FragmentManager manager = getSupportFragmentManager(); 
FragmentTransaction ft = manager.beginTransaction(); 
Fragment bottomFragment = manager.findFragmentById(R.id.bottomFragment); 
ft.show(bottom); 
ft.commit(); 
+0

謝謝,但我想你忘了添加ft.commit()所需的括號。但它的作品,謝謝! –

+0

是的,你說得對,我忘了。我更新了我的答案。所以如果答案正確,請接受。 – Fori