Fragment与Activity的通信

本文介绍了一个简单的Fragment间通信实现方式,通过定义接口并在Activity中实现该接口,当ListFragment中的列表项被点击时,能够传递信息给ContentFragment并更新显示内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.zdsoft.activityfragment1205;

import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity implements ListFragment.OnItemSelectListener {

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

    @Override
    public void OnSelect(int index) {
        String str = "";
        //获得frament
        ContentFragment fragment = (ContentFragment) getSupportFragmentManager().findFragmentById(R.id.f_content);
        switch (index) {
            case 0:
                str = "你好,我是Android";
                fragment.showMessage(str);
                break;
            case 1:
                str = "你好,我是IOS";
                fragment.showMessage(str);
                break;
            case 2:
                str = "你好,我是WP";
                fragment.showMessage(str);
                break;

            default:
                break;
        }
    }
}

package com.zdsoft.activityfragment1205;


import android.content.Context;
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.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;


/**
 * A simple {@link Fragment} subclass.
 */
public class ListFragment extends Fragment {
    private ListView lv_show;
    private String[] array = {"android", "IOS", "WP"};
    OnItemSelectListener listener;


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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_list, container, false);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, array);
        lv_show = (ListView) view.findViewById(R.id.lv_show);
        lv_show.setAdapter(adapter);
        lv_show.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //调用接口内方法
                listener.OnSelect(position);
            }
        });

        return view;
    }

    /**
     * 定义接口
     */
    public interface OnItemSelectListener {
        public void OnSelect(int index);
    }

    /**
     * 重写onAttach方法实例化listener;
     *
     * @param context
     */

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        listener = (OnItemSelectListener) context;

    }
}

package com.zdsoft.activityfragment1205;


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;


/**
 * A simple {@link Fragment} subclass.
 */
public class ContentFragment extends Fragment {
    private TextView tv_show;


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


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

    }

    public void showMessage(String msg) {
        tv_show.setText(msg);
    }

}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <fragment
        android:id="@+id/f_list"
        android:name="com.zdsoft.activityfragment1205.ListFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/f_content"
        android:name="com.zdsoft.activityfragment1205.ContentFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3" />
</LinearLayout>

<LinearLayout 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:background="@color/colorAccent"
    android:orientation="vertical">

    <!-- TODO: Update blank fragment layout -->
    <ListView
        android:id="@+id/lv_show"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

<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.zdsoft.activityfragment1205.ContentFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/tv_show"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值