我有一个片段,其中包含一个PercentRelativeLayout,我想用点按钮上的另一个片段替换它。这是外部片段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>
这是片段Java文件中的onClick方法
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();
}
});
我想将ID为displayWeekOne的PercentRelativeLayout替换为片段,这里是我希望替换的片段
<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>
这是该片段的Java
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);
}
}
我在文档中读到我可以使用replace()来替换另一个片段,但是我想在可能的情况下替换PercentRelativeLayout,或者将片段添加到外部片段xml
答案 0 :(得分:0)
FragmentManager manager = getFragmentManager();
manager.beginTransaction().replace(R.id.displayWeekOne, dropDownWeekFragment).commit();
您可以使用replace()
将一个Fragment
替换为另一个Fragment
,将Layout
替换为Fragment
。
没有测试过。希望它有效。