1.top_fragment.xml
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TopFragment的按钮" />
2.bottom_fragment.xml
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TopFragment的按钮" />
3.TopFragment.java
package com.gst.user.application;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import java.io.FileDescriptor;
import java.io.PrintWriter;
/**
* Created by user on 2/1/16.
*/
public class TopFragment extends Fragment implements View.OnClickListener{
private onTopButtonClickedListener listener;
@Override
public void onClick(View v) {
if (listener!=null){
listener.onClick("Top Fragment Demo");
Toast.makeText(v.getContext(),"Top Fragment Demo Click",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onAttach(Activity activity) {
if (getActivity() instanceof onTopButtonClickedListener) {
listener= (onTopButtonClickedListener) getActivity();
}
super.onAttach(activity);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root=inflater.inflate(R.layout.top_fragment,null);
root.setOnClickListener(this);
return root;
}
public interface onTopButtonClickedListener{
public void onClick(String name);
}
}
4.BottomFragment.java
package com.gst.user.application;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
/**
* Created by user on 2/1/16.
*/
public class BottomFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater
.inflate(R.layout.bottom_fragment, container, false);
return view;
}
public void updateText(String value){
EditText editText = (EditText)getView();
editText.setText(value);
}
}
5.content_temp.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_temp" tools:context="com.gst.user.application.TempActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="344dp"
android:name="com.gst.user.application.MyFragment"
android:id="@+id/fragment"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="50dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:onClick="onClick_SetFragmentField"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="设置Fragment中的字段" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取Fragment的字段"
android:id="@+id/button2"
android:onClick="onClick_GetFragmentField"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp" />
</RelativeLayout>
6.TempActivity.java
package com.gst.user.application;
import android.app.FragmentManager;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class TempActivity extends AppCompatActivity {
private static final String TAG = "TempActivity";
private MyFragment fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_temp);
FragmentManager fragmentManager=getFragmentManager();
if (savedInstanceState!=null){
fragment= (MyFragment) fragmentManager.getFragment(savedInstanceState,"fragment");
}
if (fragment==null){
fragment= (MyFragment) fragmentManager.findFragmentById(R.id.fragment);
}
}
public void onClick_SetFragmentField(View view) {
if (fragment != null) {
fragment.name= "name:新的字段值";
fragment.getArguments().putString("name", "arg:新设置的值");
Toast.makeText(this, "成功为name字段赋值", Toast.LENGTH_LONG).show();
}
}
public void onClick_GetFragmentField(View view) {
if (fragment != null) {
Toast.makeText(this, fragment.name, Toast.LENGTH_LONG).show();
Toast.makeText(this, fragment.getArguments().getString("name"),
Toast.LENGTH_LONG).show(); }
}
@Override
public void onSaveInstanceState(Bundle outState) {
if (fragment != null) {
getFragmentManager().putFragment(outState,"fragment",fragment);
}
super.onSaveInstanceState(outState);
}
}