• 团队冲刺2.3


    1.今天学习了Fragment,明白了可能之前的项目不是Fragment所以无法跳转

    今天学习Fragment

    public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    
        //UI Object
        private TextView txt_topbar;
        private TextView txt_channel;
        private TextView txt_message;
        private TextView txt_better;
        private TextView txt_setting;
        private FrameLayout ly_content;
    
        //Fragment Object
        private MyFragment fg1,fg2,fg3,fg4;
        private FragmentManager fManager;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_main);
            fManager = getFragmentManager();
            bindViews();
            txt_channel.performClick();   //模拟一次点击,既进去后选择第一项
        }
    
        //UI组件初始化与事件绑定
        private void bindViews() {
            txt_topbar = (TextView) findViewById(R.id.txt_topbar);
            txt_channel = (TextView) findViewById(R.id.txt_channel);
            txt_message = (TextView) findViewById(R.id.txt_message);
            txt_better = (TextView) findViewById(R.id.txt_better);
            txt_setting = (TextView) findViewById(R.id.txt_setting);
            ly_content = (FrameLayout) findViewById(R.id.ly_content);
    
            txt_channel.setOnClickListener(this);
            txt_message.setOnClickListener(this);
            txt_better.setOnClickListener(this);
            txt_setting.setOnClickListener(this);
        }
    
        //重置所有文本的选中状态
        private void setSelected(){
            txt_channel.setSelected(false);
            txt_message.setSelected(false);
            txt_better.setSelected(false);
            txt_setting.setSelected(false);
        }
    
        //隐藏所有Fragment
        private void hideAllFragment(FragmentTransaction fragmentTransaction){
            if(fg1 != null)fragmentTransaction.hide(fg1);
            if(fg2 != null)fragmentTransaction.hide(fg2);
            if(fg3 != null)fragmentTransaction.hide(fg3);
            if(fg4 != null)fragmentTransaction.hide(fg4);
        }
    
    
        @Override
        public void onClick(View v) {
            FragmentTransaction fTransaction = fManager.beginTransaction();
            hideAllFragment(fTransaction);
            switch (v.getId()){
                case R.id.txt_channel:
                    setSelected();
                    txt_channel.setSelected(true);
                    if(fg1 == null){
                        fg1 = new MyFragment("第一个Fragment");
                        fTransaction.add(R.id.ly_content,fg1);
                    }else{
                        fTransaction.show(fg1);
                    }
                    break;
                case R.id.txt_message:
                    setSelected();
                    txt_message.setSelected(true);
                    if(fg2 == null){
                        fg2 = new MyFragment("第二个Fragment");
                        fTransaction.add(R.id.ly_content,fg2);
                    }else{
                        fTransaction.show(fg2);
                    }
                    break;
                case R.id.txt_better:
                    setSelected();
                    txt_better.setSelected(true);if(fg3 ==null){
                        fg3 =newMyFragment("第三个Fragment");
                        fTransaction.add(R.id.ly_content,fg3);}else{
                        fTransaction.show(fg3);}break;case R.id.txt_setting:
                    setSelected();
                    txt_setting.setSelected(true);if(fg4 ==null){
                        fg4 =newMyFragment("第四个Fragment");
                        fTransaction.add(R.id.ly_content,fg4);}else{
                        fTransaction.show(fg4);}break;}
            fTransaction.commit();}
  • 相关阅读:
    css的一些记录——“Unexpected missing generic font family”
    idea右键java文件找到对应的class文件路径以及查看class文件
    redis的一些记录
    【转】解决IDEA2020控制台乱码的方法
    【转】java去除html代码中含有的html、js、css标签,获取文字内容
    linux 中 AGE的含义
    控制反转容器比较:TinyIoC vs Autofac
    C# 异步方法,尽量避免使用async void而是要用async Task
    xamarin 实现选择文件功能
    xamarin手写签名
  • 原文地址:https://www.cnblogs.com/chenghaixiang/p/14914625.html
Copyright © 2020-2023  润新知