这次我们试水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>
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;
}
}
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();
}
}