好吧标题说明了一切,我不知道如何在启动时隐藏它。我试着搜索,但没有任何事情发生在我希望它发生的方式。
所以我想要发生的是我希望在启动时隐藏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);
}
}
答案 0 :(得分:3)
开始隐藏片段,如下所示:
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
Fragment bottomFragment = manager.findFragmentById(R.id.bottomFragment);
ft.hide(bottomFragment);
ft.commit();
然后你可以再次展示它:
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
Fragment bottomFragment = manager.findFragmentById(R.id.bottomFragment);
ft.show(bottom);
ft.commit();