【问题标题】:Fragment keeps on adding when the button is clicked单击按钮时片段继续添加
【发布时间】:2017-11-10 04:07:35
【问题描述】:
ImageButton imageButton3 =(ImageButton)view.findViewById(R.id.item_two_timer_id);

imageButton3.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         getFragmentManager().beginTransaction()
                    .replace(R.id.container2_main , new TimerFragment2())
                              .addToBackStack(null)
                              .commit();

【问题讨论】:

  • 欢迎来到 StackOverflow,你能解释一下你真正的问题是什么吗?
  • 你的意思是说,当你一直按 imageButton 时,你的 Fragment 添加到了容器中,对吧?
  • 是的,你是对的..

标签: android fragment


【解决方案1】:

在添加片段时添加标签以识别它。通过标签获取片段并查看片段是否存在。如果不存在则创建一个新实例并添加它。

ImageButton imageButton3 = (ImageButton) view.findViewById(R.id.item_two_timer_id);

imageButton3.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        TimerFragment2 timerFragment2;
        timerFragment2 = (TimerFragment2) getFragmentManager().findFragmentByTag(TimerFragment2.class.getSimpleName());
        if(timerFragment2==null){
            timerFragment2=new TimerFragment2();
            getFragmentManager().beginTransaction()
                        .replace(R.id.container2_main,  timerFragment2,TimerFragment2.class.getSimpleName())
                        .addToBackStack(TimerFragment2.class.getSimpleName())
                        .commit();
        }else {
            //Dont create fragment again
        }

    }
});

【讨论】:

  • 欢迎您。请投票并接受这个答案。它对我有很大帮助:)
【解决方案2】:

如果您要设置一个片段,请尝试此代码

private void initFragment() {
    SelectTypeFragment fragment = new SelectTypeFragment();
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right); //if you want to set animation
    fragmentTransaction.replace(R.id.fl_addvehicle, fragment);
    fragmentTransaction.commit();
}

如果您将一个片段替换为另一个片段。

 void goToNextFragment() {
    ((CreateOfferActivity) this.getActivity()).setCreateOfferModel(createOfferModel);
    SelectVehicleFragment fragment = new SelectVehicleFragment();
    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right);
    fragmentTransaction.replace(R.id.fl_create_offer, fragment).
            addToBackStack("select_vehicle");
    fragmentTransaction.commit();
}

【讨论】:

  • 我会执行看看..谢谢
【解决方案3】:

您可以在 Fragment 中检查它,但首先,

你需要创建一个Fragment对象TimerFragment2并在timerFragment.isVisible()中检查它是否可见

    ImageButton imageButton3 =(ImageButton)view.findViewById(R.id.item_two_timer_id);
TimerFragment2 timerFragment = new TimerFragment2();

imageButton3.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
        if(!timerFragment.isVisible())
           getFragmentManager().beginTransaction()
                    .replace(R.id.container2_main , timerFragment)
                              .addToBackStack(null)
                              .commit();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 2014-01-04
    相关资源
    最近更新 更多