Android-Fragment和Activity的传值通信

Android-Fragment和Activity的传值通信


Activity向Fragment发送数据


布局文件:

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

    <LinearLayout
        android:id="@+id/id_frame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        >

        <include
            android:id="@+id/id_one"
            layout="@layout/btn_layout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            >include>

        <include
            android:id="@+id/id_two"
            layout="@layout/btn_layout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            >include>

        <include
            android:id="@+id/id_three"
            layout="@layout/btn_layout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            >include>

        <include
            android:id="@+id/id_four"
            layout="@layout/btn_layout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            >include>


    LinearLayout>


RelativeLayout>

按钮的布局:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:gravity="center_horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <ImageButton
        android:id="@+id/id_btn"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/btn_bg"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:id="@+id/id_text"
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="静态加载"/>

LinearLayout>

主活动:
第四个按钮

package com.xieth.as.againfragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageButton btn1 = null;
    private TextView tv1 = null;
    private ImageButton btn2 = null;
    private TextView tv2 = null;
    private ImageButton btn3 = null;
    private TextView tv3 = null;
    private ImageButton btn4 = null;
    private TextView tv4 = null;

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

    private void initViews() {
        btn1 = (ImageButton) findViewById(R.id.id_one).findViewById(R.id.id_btn);
        btn1.setTag(1);
        tv1 = (TextView)findViewById(R.id.id_one).findViewById(R.id.id_text);
        tv1.setText("静态加载");
        btn2 = (ImageButton) findViewById(R.id.id_two).findViewById(R.id.id_btn);
        btn2.setTag(2);
        tv2 = (TextView)findViewById(R.id.id_two).findViewById(R.id.id_text);
        tv2.setText("动态加载");
        btn3 = (ImageButton) findViewById(R.id.id_three).findViewById(R.id.id_btn);
        btn3.setTag(3);
        tv3 = (TextView)findViewById(R.id.id_three).findViewById(R.id.id_text);
        tv3.setText("生命周期");
        btn4 = (ImageButton) findViewById(R.id.id_four).findViewById(R.id.id_btn);
        btn4.setTag(4);
        tv4 = (TextView)findViewById(R.id.id_four).findViewById(R.id.id_text);
        tv4.setText("传值通信");
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = (int) v.getTag();
        switch (id) {
            case 1:
                Toast.makeText(this, "1", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                MyFragmentOne myFragmentOne = new MyFragmentOne();
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.id_frame, myFragmentOne);
                fragmentTransaction.commit();
                break;
            case 3:
                Toast.makeText(this, "3", Toast.LENGTH_SHORT).show();
                break;
            case 4:
                Intent it = new Intent(this, MainActivity2.class);
                startActivity(it);
                break;
            default:
                break;
        }
    }
}

MyFragmentTwo.java

package com.xieth.as.againfragment;

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

/**
 * Created by YR on 2016/04/02.
 */
public class MyFragmentTwo extends Fragment{

    private View view = null;
    private TextView tv = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.flayout_two, container, false);
        }
        tv = (TextView) view.findViewById(R.id.id_text2);
        Bundle bundle = getArguments();
        String text = bundle.getString("key");
        tv.setText("数据:" + text);
        Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
        return view;
    }
}

运行效果:
Android-Fragment和Activity的传值通信_第1张图片


Fragment向Activity传递数据
在MyFragmentTwo里面自定义接口。

 private FragmentListener listener = null;

    public interface FragmentListener {
        void thank(String text);
    }

    @Override
    public void onAttach(Activity activity) {
        listener = (FragmentListener) activity;
        super.onAttach(activity);
    }

MyFragmentTwo.java

package com.xieth.as.againfragment;

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

/**
 * Created by YR on 2016/04/02.
 */
public class MyFragmentTwo extends Fragment{

    private View view = null;
    private TextView tv = null;

    private FragmentListener listener = null;

    public interface FragmentListener {
        void thank(String text);
    }

    @Override
    public void onAttach(Activity activity) {
        listener = (FragmentListener) activity;
        super.onAttach(activity);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.flayout_two, container, false);
        }
        tv = (TextView) view.findViewById(R.id.id_text2);
        Bundle bundle = getArguments();
        String text = bundle.getString("key");
        tv.setText("数据:" + text);
        Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
        listener.thank("thanks, Activity!");
        return view;
    }
}

MainActivity2.java
实现这个接口

public class MainActivity2 extends AppCompatActivity implements MyFragmentTwo.FragmentListener

并且重写该方法,即可收到数据

@Override
    public void thank(String text) {
        Toast.makeText(this, "Fragment返回的数据:" + text, Toast.LENGTH_SHORT).show();
    }

运行:
Android-Fragment和Activity的传值通信_第2张图片


完整代码
AndroidStudio开发的

你可能感兴趣的:(Android,复习基础笔记)