首页 > 代码库 > Fragment-向Activity中添加Fragment,并相互交互

Fragment-向Activity中添加Fragment,并相互交互

一、activity

一般书写代码时在oncreate方法中引用initview()、initdata()、initload()进行初始化

package com.ping.myfragment;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {//当activity中引入fragment时应该继承--FragmentActivity

 private Myfragment myfragment;
 private TextView activityTV;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  initview();
  initdata();
 }

 private void initdata() {
  FragmentManager fm = getSupportFragmentManager();//获得fragment的管理对象
  FragmentTransaction ft = fm.beginTransaction();
  myfragment = new Myfragment();//Fragment每次添加都要重新创建,否则因为状态不同会导致问题  

  ft.add(R.id.container, myfragment);
  ft.commit();
 }

 private void initview() {
  activityTV = (TextView) findViewById(R.id.tv_activity);
  findViewById(R.id.bt_activity).setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    myfragment.settext();
   }
  });
 }

 public void settext() {
  activityTV.setText("activity修改成功");
 }
}

二、fragment

package com.ping.myfragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

public class Myfragment extends Fragment {

 private TextView fragmentTV;

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  View inflate = inflater.inflate(R.layout.layout_myfragment, null);
  initview(inflate);
  return inflate;
 }

 private void initview(View inflate) {
  final MainActivity activity = (MainActivity) getActivity();
  fragmentTV = (TextView) inflate.findViewById(R.id.tv_fragment);
  inflate.findViewById(R.id.bt_fragment).setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    activity.settext();
   }
  });
 }

 public void settext() {
  fragmentTV.setText("fragment修改成功");
 }
}

 

三activity.xml

<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"
    tools:context=".MainActivity" >

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" >
    </FrameLayout>

    <TextView
        android:id="@+id/tv_activity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/container"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="activity中的textview" />

    <Button
        android:id="@+id/bt_activity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_activity"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="17dp"
        android:text="改变fragment中的textview" />

</RelativeLayout>

四、fragmen.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#eeeeee">

    <TextView
        android:id="@+id/tv_fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="fragment中的textview" />

    <Button
        android:id="@+id/bt_fragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_fragment"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:text="改变activity中的textview" />

</RelativeLayout>

Fragment-向Activity中添加Fragment,并相互交互