简体   繁体   中英

Android Studio replace a relative layout insade a framgent with another fragment

I have a fragment that contains a PercentRelativeLayout that I want to replace with another fragment on a button click. Here is the outer fragment xml

    <?xml version="1.0" encoding="utf-8"?>

<android.support.percent.PercentRelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">


    <android.support.percent.PercentRelativeLayout
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        app:layout_widthPercent="92%"
        app:layout_heightPercent="80%"
        app:layout_marginTopPercent="15%">

        <android.support.percent.PercentRelativeLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent">

            <TextView
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:text="Vecka 49"
                android:id="@+id/weekOneTextView"/>

            <android.support.percent.PercentRelativeLayout
                app:layout_widthPercent="100%"
                app:layout_heightPercent="0%"
                android:layout_below="@+id/weekOneTextView"
                android:id="@+id/displayWeekOne">

            </android.support.percent.PercentRelativeLayout>

            <TextView
                android:layout_alignParentLeft="true"
                app:layout_widthPercent="45%"
                app:layout_heightPercent="5%"
                app:layout_marginTopPercent="2%"
                android:layout_below="@+id/displayWeekOne"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Vecka 50"
                android:id="@+id/weekTwoTextView"
                android:clickable="true"/>

            <android.support.percent.PercentRelativeLayout
                app:layout_widthPercent="100%"
                app:layout_heightPercent="0%"
                android:layout_below="@+id/weekTwoTextView"
                android:id="@+id/displayWeekTwo">

            </android.support.percent.PercentRelativeLayout>

            <TextView
                android:layout_alignParentLeft="true"
                app:layout_widthPercent="45%"
                app:layout_heightPercent="5%"
                app:layout_marginTopPercent="2%"
                android:layout_below="@+id/displayWeekTwo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Vecka 51"
                android:id="@+id/weekThreeTextView"
                android:clickable="true"/>

            <android.support.percent.PercentRelativeLayout
                app:layout_widthPercent="100%"
                app:layout_heightPercent="0%"
                android:layout_below="@+id/weekThreeTextView"
                android:id="@+id/displayWeekThree">

            </android.support.percent.PercentRelativeLayout>

            <TextView
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/displayWeekThree"
                app:layout_widthPercent="45%"
                app:layout_heightPercent="5%"
                app:layout_marginTopPercent="2%"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Vecka 52"
                android:id="@+id/weekFourTextView"
                android:clickable="true"/>

            <android.support.percent.PercentRelativeLayout
                app:layout_widthPercent="100%"
                app:layout_heightPercent="0%"
                android:layout_below="@+id/weekFourTextView"
                android:id="@+id/displayWeekFour">

            </android.support.percent.PercentRelativeLayout>

        </android.support.percent.PercentRelativeLayout>

    </android.support.percent.PercentRelativeLayout>


</android.support.percent.PercentRelativeLayout>

Here is the onClick method in the fragments Java file

 private void setOnClickListeners(){
    mWeekOneTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            DropDownWeekFragment dropDownWeekFragment = new DropDownWeekFragment();
            FragmentManager manager = getFragmentManager();
            manager.beginTransaction().add(R.id.displayWeekOne, dropDownWeekFragment).commit();
        }
    });

I want to replace the PercentRelativeLayout with ID displayWeekOne with a fragment, here is the for the fragment I wish to replace with

    <android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                               xmlns:tools="http://schemas.android.com/tools"
                                               android:layout_width="match_parent"
                                               android:layout_height="wrap_content"
                                               xmlns:app="http://schemas.android.com/apk/res-auto"
                                               tools:context="layout.DropDownWeekFragment">

    <android.support.percent.PercentRelativeLayout
        app:layout_widthPercent="80%"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Måndag"
            android:id="@+id/monday"
            app:layout_marginTopPercent="2%"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tisdag"
            android:id="@+id/tuesday"
            android:layout_below="@id/monday"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/wednessday"
            android:layout_below="@id/tuesday"
            android:text="Onsdag"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/thursday"
            android:layout_below="@id/wednessday"
            android:text="Torsdag"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/friday"
            android:layout_below="@id/thursday"
            android:text="Fredag"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/saturday"
            android:layout_below="@id/friday"
            android:text="Lördag"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/sunday"
            android:layout_below="@id/saturday"
            android:text="Söndag"/>

    </android.support.percent.PercentRelativeLayout>


</android.support.percent.PercentRelativeLayout>

And here is the Java for that fragment

package layout;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.studentconsulting.mypages.R;

import static android.content.ContentValues.TAG;

public class DropDownWeekFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";


    private String mParam1;
    private String mParam2;



    private OnFragmentInteractionListener mListener;

    public DropDownWeekFragment() {
        // Required empty public constructor
    }

    public static DropDownWeekFragment newInstance(String param1, String param2) {
        DropDownWeekFragment fragment = new DropDownWeekFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
            Log.d("hej", "oncreate");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_drop_down_week, container, false);
    }

    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(Uri uri);
    }
}

I read in the documentation that I can use replace() to replace another fragment, but I want to replace the PercentRelativeLayout if that is possible, or add the fragment to the outer fragments xml

FragmentManager manager = getFragmentManager(); manager.beginTransaction().replace(R.id.displayWeekOne, dropDownWeekFragment).commit();

You can use replace() to replace one Fragment with another Fragment , as well as a Layout with a Fragment . Not tested. Hope it works.

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