您的位置:首页 > 移动开发 > Android开发

Activty与Fragment交互通信

2015-12-03 00:16 489 查看
首先,如果你想在android3.0及以下版本使用fragment,你必须引用android-support-v4.jar这个包

然后你写的activity不能再继承自Activity类了,而是要继承android.support.v4.app.FragmentActivity,一些其他的父类也有相应的变化.

这里的包一定要统一否则会不兼容报错要么同一import android.app.Fragment;这个包

由于在android的实现机制中fragment和activity会被分别实例化为两个不相干的对象,他们之间的联系由activity的一个成员对象fragmentmanager来维护

要实现Activty参数传递给Fragment方法有很多,这里介绍通过Bundle对象传递参数给Fragment

具体代码如下

Fragment的Java代码

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.xhsc.layout.relativelayout.R;

public class ParamsFragment extends Fragment {
String mMassage;

public ParamsFragment() {
// Required empty public constructor
}

public static ParamsFragment newInstance(String message) {

Bundle args = new Bundle();

args.putString("MESSAGE",message);
ParamsFragment fragment = new ParamsFragment();
fragment.setArguments(args);
return fragment;
}

@Override//官方推荐在onCreate()中接收参数
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getArguments()!=null){
Bundle bundle = getArguments();
mMassage = bundle.getString("MESSAGE");
}

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view =inflater.inflate(R.layout.fragment_params_layout, container, false);
TextView textView = (TextView) view.findViewById(R.id.fragment_params_show_textview);
textView.setText(mMassage);
return view;
}

}

Fragment的布局Xml代码

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.xhsc.fragment.fragmentlife.paramters.ParamsFragment">

<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/fragment_params_show_textview"
android:textColor="@android:color/holo_purple"
android:textSize="20sp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"

/>

</FrameLayout>

Activity的Java代码

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.xhsc.layout.relativelayout.R;

public class FragmentParamsActvity extends Activity {

EditText mEditText;
Button mSendFragmentMsgBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_activity_params_actvity_layout);
mSendFragmentMsgBtn = (Button) findViewById(R.id.fragment_params_send_button);
mEditText = (EditText) findViewById(R.id.fragment_params_editText);
mSendFragmentMsgBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
<span style="white-space:pre">		</span>//需要注意是通过FragmentManager对象来管理Fragment的,<span style="font-family: Arial; font-size: 14px; line-height: 25px;">android-support-v4包情况下是getSupportFragmentManager()</span>
getFragmentManager().beginTransaction().add(R.id.fragment_params_framelayout,ParamsFragment.newInstance(mEditText.getText().toString())).commit();
}
});
}

}


Activity的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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.xhsc.fragment.fragmentlife.paramters.FragmentParamsActvity">

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fragment_params_editText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送消息"
android:id="@+id/fragment_params_send_button"
and
4000
roid:layout_marginTop="21dp"
android:layout_below="@+id/fragment_params_editText"
android:layout_alignParentLeft="true" />
<span style="white-space:pre">	</span><--<span style="font-family: Arial; background-color: rgb(255, 0, 0);">FrameLayout</span><span style="color:#ff0000;">空布局容器占位,动态添加Fragment,需要id</span>-->
<FrameLayout
android:id="@+id/fragment_params_framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="22dp"
android:layout_below="@+id/fragment_params_send_button"
android:layout_alignParentLeft="true">

</FrameLayout>

</RelativeLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息