Fragment高级进阶

Fragment 回退栈

 radioGroupId.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.person_id:
                        manager = getSupportFragmentManager();
                        fragmentTransaction = manager.beginTransaction();
//                        fragmentTransaction.replace(R.id.frame_layout_id,new FristFragment());
                        fragmentTransaction.add(R.id.frame_layout_id,new FristFragment());
						//入栈
                        fragmentTransaction.addToBackStack("11");
                        fragmentTransaction.commit();
                        break;
                    case R.id.message_id:
                        manager = getSupportFragmentManager();
						
						//出栈
                        manager.popBackStack(); 
                        SelectActivity.this.fragmentTransaction = manager.beginTransaction();
                        SelectActivity.this.fragmentTransaction.replace(R.id.frame_layout_id,new SecondFragment());
//                        fragmentTransaction.addToBackStack(null);
                        SelectActivity.this.fragmentTransaction.commit();
                        break;

                    default:
                        break;
                }
            }
        });
        case R.id.message_id:
    manager = getSupportFragmentManager();
//  manager.popBackStack();
    fragmentTransaction = manager.beginTransaction();
    fragmentTransaction.replace(R.id.frame_layout_id,new SecondFragment());
    //添加了返回栈,点击back的时候,不会退出activity,而会返回到之前的fragment界面.
//  fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
     break;

activity 给 fragment传值

package com.example.day004;

import android.icu.util.BuddhistCalendar;
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.View;
import android.widget.EditText;

import com.example.day004.fragment.ShowContextFragment;

import java.io.BufferedReader;

/**
 * activity 向 fragment 传值
 */
public class A2FActivity extends AppCompatActivity {
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a_2_f);

        //0,取到edit的控件和值
        editText =  findViewById(R.id.et_id);

        //1,在这里动态添加一个fragment
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        //注意这个布局文件,是R.layout.activity_a_2_f xml文件里的线性布局
        fragmentTransaction.add(R.id.linear_layout_id,new ShowContextFragment());
        fragmentTransaction.commit();
    }

    public void click(View view) {
        //取到输入的值
        String string = editText.getText().toString();
        //创建fragment对象
        ShowContextFragment showContextFragment = new ShowContextFragment();
        //创建bundle
        Bundle bundle = new Bundle();
        bundle.putString("key",string);
        //给fragment对象赋值
        showContextFragment.setArguments(bundle);

        //动态修改fragment
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.linear_layout_id,showContextFragment);
        fragmentTransaction.commit();
    }
}

fragment 给 fragment 传值

package com.example.day005.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.day005.R;

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

    private EditText editText;
    private Button send_button;

    public LeftFragment() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_left, container, false);
        editText = inflate.findViewById(R.id.left_textView_id);
        send_button = inflate.findViewById(R.id.left_button_id);

        send_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String string = editText.getText().toString();
                
                **了解一种即可,不重点**
                //1 fragment传值方式1
                // 通过id找的指定fragment,调用他的方法,给他的组件赋值.
                FragmentManager fragmentManager = getFragmentManager(); //注意,这个manager是v4包下的.
                RightFragment rightFragment=(RightFragment) fragmentManager.findFragmentById(R.id.right_fragment_id);
                rightFragment.setTextView(string);

                //2 通过id找的指定fragment,得到他所有的view.在通过findViewById 找到组件.赋值
                TextView textView = getFragmentManager().findFragmentById(R.id.right_fragment_id).getView().findViewById(R.id.right_textview_id);
                textView.setText(string);

                //3
                //因为两个fragment在一个activity里面,所有可以通过拿到activity里面所有的指定组件.
                TextView textView = (TextView) getActivity().findViewById(R.id.right_textview_id);
                textView.setText(string);

            }
        });
        return inflate;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值