开发者社区> 问答> 正文

fragment切换时,可以点击到之前页面上的按钮

fragment1上面有个按钮,fragment2上是空的,在fragment2页面点击按钮所在部位,就可以点进去,请问怎么修改

 //定义一个选中一个item后的处理
        public void setChioceItem(int index)
        {
                //重置选项+隐藏所有Fragment
                FragmentTransaction transaction = fManager.beginTransaction();  

                clearChioce();
                hideFragments(transaction);
                switch (index) {
                case 0:
                        ceshi_image.setImageResource(R.drawable.ceshitubiao2);  
                        ceshi_text.setTextColor(blue);
                        TextView tv = (TextView) findViewById(R.id.biaoti);
                        tv.setText("题目练习");
                        //course_layout.setBackgroundResource(R.drawable.ic_tabbar_bg_click);
            if (fg1 == null) {  
                // 如果fg1为空,则创建一个并添加到界面上  ic_tabbar_course_pressed
                fg1 = new Fragment1();  
                transaction.add(R.id.content, fg1);  
            } else {  
                // 如果MessageFragment不为空,则直接将它显示出来  
                transaction.show(fg1);  
            }  
            break;  

                case 1:
                        tongzhi_image.setImageResource(R.drawable.tongzhi2);  
                        tongzhi_text.setTextColor(blue);
                        tv = (TextView) findViewById(R.id.biaoti);
                        tv.setText("消息通知");
                        //found_layout.setBackgroundResource(R.drawable.ic_tabbar_bg_click);
            if (fg2 == null) {  
                // 如果fg2为空,则创建一个并添加到界面上  
                fg2 = new Fragment2();  
                transaction.add(R.id.content, fg2);  
            } else {  
                // 如果MessageFragment不为空,则直接将它显示出来  
                transaction.show(fg2);  
            }  
            break;      

                 case 2:
                        wo_image.setImageResource(R.drawable.wotubiao2);  
                        wo_text.setTextColor(blue);
                        //settings_layout.setBackgroundResource(R.drawable.ic_tabbar_bg_click);
                        tv = (TextView) findViewById(R.id.biaoti);
                        tv.setText("我");
            if (fg3 == null) {  
     //            如果fg3为空,则创建一个并添加到界面上  
                fg3 = new Fragment3();  
               transaction.add(R.id.content, fg3);  
            } else {  
                // 如果MessageFragment不为空,则直接将它显示出来  
                transaction.show(fg3);  
            }  
            break;                 
                }
                transaction.commit();
        }

        //隐藏所有的Fragment,避免fragment混乱
        private void hideFragments(FragmentTransaction transaction) {  
        if (fg1 != null) {  
            transaction.hide(fg1);  
        }  
        if (fg2 != null) {  
            transaction.hide(fg2);  
        }  
        if (fg3 != null) {  
            transaction.hide(fg3);  
        }  
    }  

screenshot
screenshot

展开
收起
爵霸 2016-05-30 11:32:41 2696 0
1 条回答
写回答
取消 提交回答
  • 很简单的问题,你点了按钮show出来的fragment实际上覆盖在前一个上面,R.id.content是FrameLayout。
    你只需要在show出来fragment之后,hide原来的就可以了,在同一个transaction里提交一下。

    2019-07-17 19:20:17
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载