• Fragment的隐藏与显示


    复制代码
    package com.bwie.helpdemo;
    
    import com.bwie.fragment.BillFrag;
    import com.bwie.fragment.IndexFrag;
    import com.bwie.fragment.TaskFrag;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.SharedPreferences;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class IndexActivity extends FragmentActivity implements OnClickListener {
    
        private Button index_bill;
        private TextView index_home;
        private TextView index_task;
    
        private IndexFrag indexFrag;
        private BillFrag billFrag;
        private TaskFrag taskFrag;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_index);
            
            // 找控件
            findView();
    
            // 为布局添加fragment,开启事物
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction tran = fm.beginTransaction();
    
            indexFrag = new IndexFrag();
            billFrag = new BillFrag();
            taskFrag = new TaskFrag();
            //R.id.relative为布局
            tran.add(R.id.relative, indexFrag, "index").show(indexFrag)
            .add(R.id.relative, billFrag, "bill").hide(billFrag)
            .add(R.id.relative, taskFrag, "task").hide(taskFrag);
    
            tran.commit();
    
            // 为按钮设置点击监听
            index_bill.setOnClickListener(this);
            index_home.setOnClickListener(this);
            index_task.setOnClickListener(this);
        }
    
        private void findView() {
            index_bill = (Button) findViewById(R.id.index_bill);
            index_home = (TextView) findViewById(R.id.index_home);
            index_task = (TextView) findViewById(R.id.index_task);
    
        }
    
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
            case R.id.index_bill:
                // 获取事物
                FragmentManager fm_1 = getSupportFragmentManager();
                FragmentTransaction tran_0 = fm_1.beginTransaction();
                tran_0.show(billFrag).hide(indexFrag).hide(taskFrag).commit();
    
                break;
    
            case R.id.index_home:
                FragmentManager fm_2 = getSupportFragmentManager();
                FragmentTransaction tran_1 = fm_2.beginTransaction();
                tran_1.show(indexFrag).hide(billFrag).hide(taskFrag).commit();
    
                break;
            case R.id.index_task:
                FragmentManager fm_3 = getSupportFragmentManager();
                FragmentTransaction tran_2 = fm_3.beginTransaction();
                tran_2.show(taskFrag).hide(billFrag).hide(indexFrag).commit();
                break;
            }
    
        }
    }
    复制代码
  • 相关阅读:
    hdu5608 function
    Codeforces Round #535 (Div. 3) 解题报告
    HDU4746 Mophues
    HDU5663 Hillan and the girl
    AtCoder Beginner Contest 117 解题报告
    GDOI2018D2T1 谈笑风生
    BZOJ4018: 小Q的幻想之乡
    牛客寒假算法基础集训营6 解题报告
    win32拖拽编程
    项目开发中的贝塞尔曲线
  • 原文地址:https://www.cnblogs.com/wbp0818/p/5453024.html
Copyright © 2020-2023  润新知