本文介绍简单的Fragment之间的交互
由于Fragment是依附于activity的,他们之间不能直接交互,因此他们之间要先将信息传到activity,然后再由activity传给目标Fragment
接下来演示代码
activity布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<!-- id必须要有,name表示引用的Fragment类
布局文件相当于静态,代码是动态
-->
<fragment
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/fragment_jiaohu1"
android:name="com.example.fragment_jiaohu.FirstFragment"/>
<fragment
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/fragment_jiaohu2"
android:name="com.example.fragment_jiaohu.SecondFragment"/>
<!-- 作为一个容器加载Fragment,因此必须要有id来获取这个容器 -->
<!-- <FrameLayout -->
<!-- android:layout_width="0dp" -->
<!-- android:layout_weight="1" -->
<!-- android:layout_height="match_parent" -->
<!-- android:id="@+id/frameLayout"></FrameLayout> -->
</LinearLayout>
MainActivity:
package com.example.fragment_jiaohu;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import com.example.fragment.R;
import com.example.fragment_jiaohu.FirstFragment.MyListener;
/**
* @description:
*
* @author taoZhang
* @created 2015-11-30 下午11:50:27
*
*/
public class MainActivity extends Activity implements MyListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jiaohu_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@SuppressLint("NewApi") @Override
public void changeValue(String value) {
//获取Fragment
SecondFragment fragment2 = (SecondFragment) getFragmentManager()
.findFragmentById(R.id.fragment_jiaohu2);//通过id获取Fragment
fragment2.setText(value);
}
}
Fragment布局一:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#fff"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first"
android:id="@+id/button1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second"
android:id="@+id/button2"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="firstTextView"
android:id="@+id/textView_jiaohu1"/>
</LinearLayout>
对应的类:
<pre name="code" class="java">package com.example.fragment_jiaohu;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import com.example.fragment.R;
/**
* @description: fragment可以通过getActivity()来获得activity实例,
* 此实例是通过onAttach来获取依附的activity,
* activity可以从FragmentManager获得Fragment的一个引用,findFragmentById()或findFragmentByTag(),
* 从而调用Fragment的方法
*
* @author taoZhang
* @created 2015-11-30 下午11:51:43
*
*/
@SuppressLint("NewApi")
public class FirstFragment extends Fragment implements OnClickListener{
private MyListener listener = null;
/**
* 添加Fragment的布局文件,并返回The root View of the inflated hierarchy
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_jiaohu_first, container, false);
Button button1 = (Button) view .findViewById(R.id.button1);
Button button2 = (Button) view .findViewById(R.id.button2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
return view;
}
@Override
public void onAttach(Activity activity) {//Fragment附加的activity
super.onAttach(activity);
listener = (MyListener)activity;
}
@Override
public void onClick(View v) {//当按钮点击,告诉activity更换哪一个Fragment
int id = v.getId();
switch(id){
case R.id.button1:
listener.changeValue("one");
break;
case R.id.button2:
listener.changeValue("two");
break;
default:
break;
}
}
/**
* 1.定义一个接口
* 2.activity实现这个接口
* 3.onAttach()获取activity
*/
public interface MyListener{
public void changeValue(String value);
}
}
Fragment布局二:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#fff">>
<TextView
android:id="@+id/textView_jiaohu2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="jiaohuFragment_second"/>
</LinearLayout>
对应的类:
package com.example.fragment_jiaohu;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.fragment.R;
@SuppressLint("NewApi")
public class SecondFragment extends Fragment{
private TextView textView = null;
/**
* 获取Fragment对应的布局文件
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_jiaohu_second, container, false);
textView = (TextView) view.findViewById(R.id.textView_jiaohu2);
return view;
}
public void setText(String value){
textView.setText(value);
}
}
接下来,我大致讲解一下
在firstFragment中,有两个按钮,用来向SecondFragment中传递信息,one和two,而Fragment之间是不能直接传递的,需要中间桥梁activity。因此,要在activity中获取传来的信息,然后设置到SecondFragment中。
那么,就要在Fragment中获取activity,在activity中获取Fragment。
Fragment中获取activity的两种方式:
1.利用onAttach()方法,获取该Fragment依附的activity
2.利用getActivity()来获取
然后在Activity中设置方法来获取参数,本例中是调用changeValue(String str);
Activity中获取Fragment:
首先获取FragmentManager,然后通过调用
findFragmentById()或findFragmentByTag()
来找到目标的Fragment对象,当然也可以像之前那篇那样,利用单例模式获取,然后向他传值
效果图如下,左右是两个Fragment(界面比较丑,请谅解):