简体   繁体   中英

How can I hide a fragment on start of my MainActivity( or the application)?

Well the title says it all, I have no idea how to hide it on startup. I tried searching but nothing happened in the way that I want it to happen.

So what I want to happen is that I want the BottomFragment to be hidden on start up. I want it to show up when I click this button in the TopFragment and the TopFragment is visible at start up.

My MainActivity.xml codes:

<?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>

My TopFragment.xml codes:

<?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>

My BottomFragment.xml Codes:

<?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>

My MainActivity.java codes:

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);

    }
}

My TopFragment.java codes:

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());
            }
        });
    }
}

My BottomFragment codes:

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);
    }
}

On start hide fragment like this:

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

Then you can again show it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM