Fragment的回调机制

本文介绍了一个使用Android Fragment进行界面布局的例子,并展示了如何通过监听器实现Fragment间的交互及数据传递。具体包括TopFragment中按钮点击事件的处理及BottomFragment中数据显示的更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值