fragment基本应用

fragment高级进阶

![在这里插入图片描述](https://img-blog.csdnimg.cn/201908071936186.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NDgwOTM4,size_16,color_FFFFFF,t_70
fragment的生命周期在这里插入图片描述

可以Activity传值给fragment

package com.example.myday04rikao;


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.Button;
import android.widget.TextView;
import android.widget.Toast;


/**
 * A simple {@link Fragment} subclass.
 */
public class libai extends Fragment {
    private Context context;

    public libai(Context context) {
        this.context = context;
    }

    public libai() {
        // 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_libai, container, false);
        TextView textView=inflate.findViewById(R.id.lb_text);
        textView.setText("我是李白");
        Button button=inflate.findViewById(R.id.lb_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "我是李白", Toast.LENGTH_SHORT).show();
            }
        });
        return inflate;
    }

}

package com.example.myday04rikao;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private RadioButton button,button1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=findViewById(R.id.radio_hx);
        button1=findViewById(R.id.radio_lb);
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.frame_layout,new hanxin(MainActivity.this));
        fragmentTransaction.commit();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager supportFragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();

                hanxin hanxin = new hanxin(MainActivity.this);

                LayoutInflater layoutInflater = getLayoutInflater();
                View inflate = layoutInflater.inflate(R.layout.fragment_hanxin, null);
                Button hh=inflate.findViewById(R.id.hx_button);
                hh.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this, "我是韩信...", Toast.LENGTH_SHORT).show();
                    }
                });
                fragmentTransaction.replace(R.id.frame_layout,hanxin);
                fragmentTransaction.commit();
            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager supportFragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();

                libai libai = new libai(MainActivity.this);
                LayoutInflater layoutInflater = getLayoutInflater();
                View inflate = layoutInflater.inflate(R.layout.fragment_libai, null);
                Button ll=inflate.findViewById(R.id.lb_button);
                ll.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this, "我是李白...", Toast.LENGTH_SHORT).show();
                    }
                });
                fragmentTransaction.replace(R.id.frame_layout,libai);
                fragmentTransaction.commit();
            }
        });

    }
}

fragment 传值给Activity

package com.example.myday05;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.io.BufferedReader;

public class MainActivity extends AppCompatActivity implements BlankFragment.AAA {
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=findViewById(R.id.m_text);
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.frame_layour,new BlankFragment());

        fragmentTransaction.commit();

    }

    @Override
    public void show(String s) {
        textView.setText(s);
    }
}


package com.example.myday05;


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.Button;
import android.widget.EditText;
import android.widget.TextView;


/**
 * A simple {@link Fragment} subclass.
 */
public class BlankFragment extends Fragment {
    private AAA aaa;
    private EditText text;
    public BlankFragment() {
        // Required empty public constructor
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_blank, container, false);
         text = inflate.findViewById(R.id.two_text);
        Button button=inflate.findViewById(R.id.two_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                aaa.show(text.getText().toString());
            }
        });
        return inflate;
    }
   public interface AAA{
        void show(String s);
    }


}

fragment传值给fragment

package com.example.myday05lainxi02;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
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 Left extends Fragment {
    private EditText editText;
    private AAA aaa;

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

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

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        aaa= (AAA) 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);
         editText=inflate.findViewById(R.id.left_text);
        Button button=inflate.findViewById(R.id.left_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = editText.getText().toString();
                aaa.show(s);
            }
        });

        return inflate;
    }
    public interface AAA{
        void show(String s);
    }

}

package com.example.myday05lainxi02;


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 Right extends Fragment implements Left.AAA {

    private TextView textView;
    public Right() {
        // 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);
         textView=inflate.findViewById(R.id.right_text);


        return inflate;
    }

    @Override
    public void show(String s) {
        textView.setText(s);
    }
}


package com.example.myday05lainxi02;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        Left left = new Left();
        Right right = new Right();
        fragmentTransaction.add(R.id.left_frame,left,"left");
        fragmentTransaction.add(R.id.right_frame,right,"right");
        fragmentTransaction.commit();


    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值