试水Fragment(5)-5min实现Fragment之间的通信

Fragment间通信实战
本文介绍如何使用Android中的Fragment并通过Activity作为桥梁实现Fragment间的通信。通过一个示例项目演示了如何在一个Fragment中通过按钮点击更新另一个Fragment中的文本内容。

这次我们试水Fragment,借助Tag和Fragment之间共同的桥梁Activity进行Fragment之间的通信,在一个Fragment中点击Button从而改变另一个Fragment中TextView的内容。

下面我们一步步来实现它,说好的5min,但3min应该就能搞定:

首先写好主布局文件,一个线性布局里包含2个线性布局,2个子线性布局用于存放Fragment,源代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/left_ll_two"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight = "1"
        android:orientation="vertical"
        >
    </LinearLayout>
    <LinearLayout
        android:id="@+id/right_ll_two"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight = "1"
        android:orientation="vertical"
        >
    </LinearLayout>

</LinearLayout>
然后写2个Fragment的布局,主要就是一个Button一个TextView,源代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/update_btn_left"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="update-Left"
        android:textSize="22sp"
        />

</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/content_tv_right"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="content-Right"
        android:textSize="22sp"
        android:gravity="center"
        />

</LinearLayout>


写好布局文件后,我们开始写Fragment,先看稍简单的右Fragment,onCreateView初始化操作,并定义公共的updateText方法以供调用:

package com.quan.car.fragmenttest;

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.TextView;

/**
 * Created by 权兴权意 on 2016/9/1.
 */
public class FragmentRight extends Fragment {

    private TextView content_tv_right;

    //Fragment build.gradle-defaultConfig-minSdkVersion>11 android.app.Fragment原生包
    //兼容低版本,libs-v4包 android.support.v4.app.Fragment 部分方法不一样,统一包
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_right,null);

        content_tv_right = (TextView) view.findViewById(R.id.content_tv_right);

        return view;
    }

    public void updateText(String s){
        content_tv_right.setText(s);
    }

}


再看左边的Fragment方法,onCreateView初始化操作,为Button设置点击事件,这里我定义了一个flag标志位进行切换。

关键是通过Tag找到右Fragment,从而实现通信,然后调用updateText进行修改操作。

                FragmentRight fRight = (FragmentRight) getActivity().getFragmentManager().findFragmentByTag("Right");

package com.quan.car.fragmenttest;

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;

/**
 * Created by 权兴权意 on 2016/9/1.
 */
public class FragmentLeft extends Fragment {

    private Boolean flag = true;

    //Fragment build.gradle-defaultConfig-minSdkVersion>11 android.app.Fragment原生包
    //兼容低版本,libs-v4包 android.support.v4.app.Fragment 部分方法不一样,统一包
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_left,null);

        view.findViewById(R.id.update_btn_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //公共桥梁Activity
                FragmentRight fRight = (FragmentRight) getActivity().getFragmentManager().findFragmentByTag("Right");
                if (flag){
                    fRight.updateText("TRUE");
                    flag = false;
                }else {
                    fRight.updateText("FALSE");
                    flag = true;
                }
            }
        });

        return view;
    }
}


那么Tag标记是如何定义的呢?回到最核心的Activity桥梁,答案在此,基本思路还是同以前,FragmentManager-FragmentTransaction -replace-commit,只不过我们在replace中添加了一个Tag参数,从而方便接下来的Fragment通信。

    public abstract FragmentTransaction replace(int var1, Fragment var2, String var3);
    public abstract Fragment findFragmentByTag(String var1);

package com.quan.car.fragmenttest;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;

/**
 * Created by 权兴权意 on 2016/9/3.
 */
public class MainTwo extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.two_main);

        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();

        transaction.replace(R.id.left_ll_two,new FragmentLeft(),"Left");
        transaction.replace(R.id.right_ll_two,new FragmentRight(),"Right");

        transaction.commit();
    }
}


怎么样,3min看完博客动手coding绰绰有余吧,这样我们就实现了Fragment之间的通信,看一下效果:










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值