fragment的开发记录

来源:互联网 发布:mysql忘记密码怎么办 编辑:程序博客网 时间:2024/06/06 04:04

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">最新由于开发过程中,产品不断的提出新的界面改动,导致安卓开发的代码一直改动。不说多的上图。</span>



这个做成两个fragment,用fragmentmanager开启事物,进行

FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();transaction.replace(R.id.framelayout,fragment1);transaction.commit;

这种做法就是第一幅图片也做成fragment,用fragmentManager进行切换,但是这个界面做成fragment之后,第一个界面是刷卡的界面,需要加载后台的service初始化,还有需要绑定service,需要在activity里面写一个onnewintent方法用于nfc后台的service读取数据,fragment里面无法写这些方法,还有service需要在activity销毁的时候解除绑定,这就是说第一个界面需要在activity里面写入,于是乎将第二图做成fragment,点击上面的有卡认证和无卡认证进行颜色修改,和fragment的现实和隐藏

case R.id.ll_card:                tv_card.setTextColor(Color.parseColor("#0174e1"));                view_card.setVisibility(View.VISIBLE);                tv_nocard.setTextColor(Color.parseColor("#000000"));                view_nocard.setVisibility(View.GONE);                transaction=getSupportFragmentManager().beginTransaction();                transaction.hide(nocardFragment);                transaction.commit();                break;            case R.id.ll_nocard:                transaction=getSupportFragmentManager().beginTransaction();                tv_nocard.setTextColor(Color.parseColor("#0174e1"));                view_nocard.setVisibility(View.VISIBLE);                tv_card.setTextColor(Color.parseColor("#000000"));                view_card.setVisibility(View.GONE);                if(nocardFragment.isHidden()){                    transaction.show(nocardFragment);                }                transaction.replace(R.id.frame_auth,nocardFragment);                transaction.commit();                break;

这里view_nocard和view_card是那块3dp高的绿色部分,这里第一次加载fragment需要在用

case R.id.ll_card:    tv_card.setTextColor(Color.parseColor("#0174e1"));    view_card.setVisibility(View.VISIBLE);    tv_nocard.setTextColor(Color.parseColor("#000000"));    view_nocard.setVisibility(View.GONE);    transaction=getSupportFragmentManager().beginTransaction();    transaction.hide(nocardFragment);    transaction.commit();    break;case R.id.ll_nocard:    transaction=getSupportFragmentManager().beginTransaction();    tv_nocard.setTextColor(Color.parseColor("#0174e1"));    view_nocard.setVisibility(View.VISIBLE);    tv_card.setTextColor(Color.parseColor("#000000"));    view_card.setVisibility(View.GONE);    if(nocardFragment.isHidden()){        transaction.show(nocardFragment);    }    transaction.replace(R.id.frame_auth,nocardFragment);    transaction.commit();    break;
这里view_nocard和view_card是那块3dp高的绿色部分,这里第一次加载fragment需要在用

  transaction.replace(R.id.frame_auth,nocardFragment);
加载这个fragment覆盖帧布局下面的,然后点击有卡认证需要隐藏该fragment,当再次点击显示该fragment,不能继续用replace方法,这个方法无效因为隐藏了就无法现实。

于是本人试了用add(fragment),和remove方法,但是这个方法在用户点击两次无卡认证的时候会add两次直接导致崩溃。需要判断该fragment是否显示is hidden表示如果该fragment隐藏就show显示,然后就可以实现该fragment和activy一部分界面之间的切换。


楼主在以为已经全美实现该功能的时候,测试过程中发现了一个bug,就是点击上面的fragment的空白处,出点到下面覆盖掉的activity的checkbox控件,为了解决这个问题,当然可以把下面布局的checkbox位置修改,或者把上面fragment的空白加上图片之类的有控件就不会点击到下面的页面,但是改方法明显治标不治本。

后来查看资料,需要在fragment里面给整个fragment设置点击事件,来组织事件向下传递。


需要在fragment的onCreateView方法中添加

view.setOnClickListener(null);
这个方法就可以阻止点击到下面的布局。



0 0