day4

day4

  • Fragment
    • Fragment创建
    • 在Fragment进行增删改
    • 自定义输出内容

Fragment

Fragment创建

		//创建
 		FragmentManager manager = getSupportFragmentManager();
        final FragmentTransaction transaction = manager.beginTransaction();
         //提交
        transaction.commit();

在Fragment进行增删改

//创建
        FragmentManager manager = getSupportFragmentManager();
        final FragmentTransaction transaction = manager.beginTransaction();
        oneFragment = new oneFragment();
        blankFragment = new BlankFragment();
        //添加
        transaction.add(R.id.aaa,oneFragment);
        transaction.add(R.id.bbb,blankFragment);
        //删除
        transaction.remove(oneFragment);
        //修改
        BlankFragment blankFragment1 = new BlankFragment();
        transaction.replace(R.id.aaa,blankFragment1);
        //提交
        transaction.commit();
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) { 
                switch (checkedId){ 
                    case R.id.one:
                        FragmentManager manager1 = getSupportFragmentManager();
                         //回收站
                        transaction1.addToBackStack(null);
                        FragmentTransaction transaction1 = manager1.beginTransaction();
                        transaction1.show(oneFragment);
                        transaction1.hide(Main2Activity.this.blankFragment);
                        transaction1.commit();
                        break;
                    case R.id.two:
                        FragmentManager manager2 = getSupportFragmentManager();
                        FragmentTransaction transaction2 = manager2.beginTransaction();
                        transaction2.show(Main2Activity.this.blankFragment);
                        transaction2.hide(oneFragment);
                        transaction2.commit();
                        break;
                    /*case R.id.three: FragmentManager manager3 = getSupportFragmentManager(); FragmentTransaction transaction3 = manager3.beginTransaction(); transaction3.replace(R.id.aaa,blankFragment); transaction3.commit(); break;*/
                }
            }
        });

自定义输出内容

fragment类中:
View inflate = inflater.inflate(R.layout.fragment_one2, container, false);
        tex = (TextView) inflate.findViewById(R.id.tex);
        Bundle arguments = getArguments();
        if (arguments!=null){ 
            String name = arguments.getString("name");
            tex.setText(name);
        }
        return inflate;
 主方法中:
 public class Main3Activity extends AppCompatActivity { 
    private EditText edit;
    private LinearLayout ll;
    @Override
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        edit = (EditText) findViewById(R.id.edit);
        ll = (LinearLayout) findViewById(R.id.ll);
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        one one = new one();
        transaction.add(R.id.ll,one);
        transaction.commit();
    }
    public void btn(View view) { 
        String s = edit.getText().toString();
        one two = new one();
        Bundle bundle = new Bundle();
        bundle.putString("name",s);
        two.setArguments(bundle);

        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();

        transaction.replace(R.id.ll,two);
        transaction.commit();
    }
}

本文地址:https://blog.csdn.net/weixin_52360287/article/details/109639562

(0)
上一篇 2022年3月22日
下一篇 2022年3月22日

相关推荐