• 一手遮天 Android


    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

    一手遮天 Android - Fragment: Fragment 与 Activity 的交互

    示例如下:

    /fragment/FragmentDemo3.java

    /**
     * Fragment 与 Activity 的交互
     *
     * activity 向 fragment 传值
     * 1、实例化 fragment 之后,通过 setArguments() 设置参数,之后可通过 getArguments() 获取参数
     * 2、直接调用 fragment 对象中的方法传值
     * 3、直接获取 fragment 对象中的组件
     *
     * fragment 向 activity 传值(fragment 可通过 getActivity() 获取父 activity 对象)
     * 1、通过回调的方式实现,推荐
     * 2、直接调用父 activity 对象中的方法传值(因为 fragment 可能会被不同的 activity 引用,所以不推荐这种方式)
     * 3、直接获取父 activity 对象中的组件(因为 fragment 可能会被不同的 activity 引用,所以不推荐这种方式)
     */
    
    package com.webabcd.androiddemo.fragment;
    
    import androidx.fragment.app.Fragment;
    import androidx.fragment.app.FragmentManager;
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    import com.webabcd.androiddemo.R;
    
    public class FragmentDemo3 extends AppCompatActivity implements Fragment3_1.OnFragmentInteractionListener {
    
        private TextView mTextView1;
        private Button mButton1;
        private Button mButton2;
        private Button mButton3;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_fragment_fragmentdemo3);
    
            mTextView1 = findViewById(R.id.textView1);
            mButton1 = findViewById(R.id.button1);
            mButton2 = findViewById(R.id.button2);
            mButton3 = findViewById(R.id.button3);
    
            sample();
        }
    
        private void sample() {
            mButton1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FragmentManager fragmentManager = getSupportFragmentManager();
                    Fragment fragment = fragmentManager.findFragmentByTag("myTag");
                    if (fragment == null) {
                        fragmentManager
                                .beginTransaction()
                                // 添加一个 fragment 对象,并在构造 fragment 对象时为其传值
                                // 实例化 fragment 对象后,通过其 setArguments() 设置参数,之后可以通过 getArguments() 获取参数
                                .add(R.id.container, Fragment3_1.newInstance("aaaaaa"), "myTag")
                                .commit();
                    }
                }
            });
    
            mButton2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FragmentManager fragmentManager = getSupportFragmentManager();
                    Fragment3_1 fragment = (Fragment3_1)fragmentManager.findFragmentByTag("myTag");
                    if (fragment != null) {
                        // 直接调用 fragment 对象的方法为其传值
                        fragment.setParam("bbbbbb");
                    }
                }
            });
    
            mButton3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FragmentManager fragmentManager = getSupportFragmentManager();
                    Fragment fragment = fragmentManager.findFragmentByTag("myTag");
                    if (fragment != null) {
                        // 获取 fragment 对象中的组件
                        TextView textView = fragment.getView().findViewById(R.id.textView1);
                        textView.append("cccccc" + "
    ");
                    }
                }
            });
        }
    
        // 用于 fragment 直接调用 activity 中的方法传值
        public void setParam(String param) {
            mTextView1.append(param + "
    ");
        }
    
        // 用于接收 fragment 回调 activity 的数据
        @Override
        public void onFragmentInteraction(String param) {
            mTextView1.append(param + "
    ");
        }
    }
    
    

    /layout/activity_fragment_fragmentdemo3.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAllCaps="false"
            android:text="添加一个 fragment 对象,并在构造 fragment 对象时为其传值"/>
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAllCaps="false"
            android:text="直接调用 fragment 对象的方法为其传值"/>
    
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAllCaps="false"
            android:text="获取 fragment 对象中的组件"/>
    
    </LinearLayout>
    
    

    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

  • 相关阅读:
    vueJs+webpack单页面应用--vue-router配置
    webstorm IDE添加Plugins----添加vue插件
    WebStorm 11、PhpStorm 10免费激活(不需要注册码)
    webpack react基础配置二 热加载
    webpack react基础配置一
    移动端页面去掉click点击 背景色变化
    css美化checkbox radio样式
    ie11媒体查询以及其他hack
    angularJS ng-grid 配置
    网络7. TCP/IP网络之网络接口层
  • 原文地址:https://www.cnblogs.com/webabcd/p/android_fragment_FragmentDemo3.html
Copyright © 2020-2023  润新知