Android Studio 1-5 Fragment 基础知识与高级进阶

一、fragment 给 activty传值

1、MainActivty的代码

package com.example.day05ex;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements ShowFragment.MyListener {

    private TextView aTv;

    @Override
    public void sendMessage(String string) {
        aTv.setText(string);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        aTv = (TextView) findViewById(R.id.a_tv);

        //事务管理
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        //创建事务
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        ShowFragment showFragment=new ShowFragment();
        //添加事务
        fragmentTransaction.add(R.id.a_fm, showFragment);
        //提交事务
        fragmentTransaction.commit();

    }

}

2、MainActivty的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/LL"
    tools:context=".MainActivity"
    >

    <TextView
        android:id="@+id/a_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="展示文本"
        />

    <fragment
        android:id="@+id/a_fm"
        android:name="com.example.day05ex.ShowFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </fragment>

</LinearLayout>

3 、Frament的布局

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
    tools:context=".ShowFragment">

    <EditText
        android:id="@+id/f_et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="写点东西"
        />

    <Button
        android:id="@+id/f_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送一下"
        />

</LinearLayout>

4、Frament的代码

package com.example.day05ex;


import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


/**
 * A simple {@link Fragment} subclass.
 */
public class ShowFragment extends Fragment {

    private EditText fEt;
    private Button fBtn;

    private MyListener myListener;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        myListener = (MyListener) getActivity();
    }

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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)  {
        // Inflate the layout for this fragment

        View inflate = inflater.inflate(R.layout.fragment_show, container, false);

        fEt = (EditText) inflate.findViewById(R.id.f_et);
        fBtn = (Button) inflate.findViewById(R.id.f_btn);

        fBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String string = fEt.getText().toString();
                if (string != null){
                    myListener.sendMessage(string);
                }
            }
        });

        return inflate;
    }

    public interface MyListener {
        void  sendMessage(String string);
    }
}

二、fragment 给 fragment 传值

1、activty布局与代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.doubleF_Activity"
    >

    <LinearLayout
        android:id="@+id/left"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <FrameLayout
        android:id="@+id/right"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>
package com.example.day05ex.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;

import com.example.day05ex.R;
import com.example.day05ex.fragment.LeftFragment;
import com.example.day05ex.fragment.RightFragment;

public class doubleF_Activity extends AppCompatActivity {

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

        FragmentManager supportFragmentManager = getSupportFragmentManager();

        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();

        LeftFragment leftFragment = new LeftFragment();
        RightFragment rightFragment = new RightFragment();
        fragmentTransaction.add(R.id.left,leftFragment);
        fragmentTransaction.add(R.id.right,rightFragment,"right");

        fragmentTransaction.commit();
    }
}

2、 fragment布局和代码

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
    tools:context=".fragment.LeftFragment">

    <EditText
        android:id="@+id/ed_left_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="输入内容传到右边"
        />

    <Button
        android:id="@+id/btn_left_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击按钮"
        />
</LinearLayout>
package com.example.day05ex.fragment;


import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.day05ex.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class LeftFragment extends Fragment {


    private EditText edLeftEdit;
    private Button btnLeftSend;

    private MyListener myListener;

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

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        myListener = (MyListener) getActivity().getSupportFragmentManager().findFragmentByTag("right");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_left, container, false);

        edLeftEdit = (EditText) inflate.findViewById(R.id.ed_left_edit);
        btnLeftSend = (Button) inflate.findViewById(R.id.btn_left_send);

        btnLeftSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String text = edLeftEdit.getText().toString();
                myListener.sendMessage(text);
            }
        });

        return inflate;
    }

    public interface MyListener {
        void sendMessage(String string);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<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=".fragment.RightFragment">

    <TextView
        android:id="@+id/tv_right_show"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="显示文本"
         />

</FrameLayout>
package com.example.day05ex.fragment;


import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.day05ex.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class RightFragment extends Fragment implements LeftFragment.MyListener {

    private TextView tvRightShow;

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_right, container, false);

        tvRightShow = (TextView) inflate.findViewById(R.id.tv_right_show);

        return inflate;
    }

    @Override
    public void sendMessage(String string) {
        tvRightShow.setText(string);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值