【问题标题】:how to replace the fragment1 from fragment2 from a button click of a dialog[NOT DIALOG FRAGMENT] (which is called by fragment1)如何通过单击对话框[NOT DIALOG FRAGMENT](由fragment1调用)的按钮从fragment2替换fragment1
【发布时间】:2021-06-24 11:59:03
【问题描述】:

我正面临一个问题,我从 fragment1 打开一个对话框(对话框 1),并且 Dialog1 上有一个按钮(更改),如果我点击该按钮:Dialog1 应该被关闭,并且在同一个 fragment1 上应该被替换片段2(另一个片段)

    public class PedigreeAnalysis extends Fragment //My Fragment1 
    
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
                         showDialog();
        }

    // SHOW DIALOG FUNCTION IS SHOWN BELOW ` 
       
         void showDialog() { //Function to show the dialog starts...
        
            Dialog dialog= new Dialog(getActivity());
            Button btn = dialog.findViewById(R.id.button);
            btn.setOnClickListener(new View.OnClickListener() {
              @Override
            public void onClick(View view) {
            //Code for opening new fragment and dismissing the dialog.
                   getActivity().getSupportFragmentManager().beginTransaction()
                      .replace(R.id.fragment1, new Fragment2()).commit();

              dialog.dismiss();
                        }
                    });
        }//Function Ends Here....

我什至尝试过(先关闭对话框,然后用函数替换它,但它也不起作用)的相反逻辑

                 dialog.dismiss();//First dismissing the dialog
                 getActivity().getSupportFragmentManager().beginTransaction()
                      .replace(R.id.fragment1, new Fragment2()).commit();//Replacing the fragment

             

【问题讨论】:

  • 那么问题出在哪里? fragment1是怎么添加的?
  • 我会建议您为片段交易使用导航视图和导航图。它更容易并且更适合您想要实现的内容

标签: java android android-fragments android-dialogfragment customdialog


【解决方案1】:

按照这个...

片段1:

public class Fragment1 extends Fragment {
    private ItemClickListener itemClickListener;
    private Button addButton;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // all the views are initialized here...
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        addButton.setOnClickListener(v -> {
            if (itemClickListener != null) onItemClicked.onClicked(new Plan());
        });
    }

    public Fragment1 setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
        return this;
    }
    public interface ItemClickListener {
        void onItemClicked(Plan plan);
    }
}

在父 Activity 类中

public class PlanActivity extends AppCompatActivity {
    private Fragment1 fragment1;
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plan);
        
        fragment1 = new Fragment1()
                .setOnAddButtonClicked(this::openFragmentEditPlan);

        openFragment1();
    }

    private void openFragment1() {
        getSupportFragmentManager().beginTransaction()
                //frameLayout_PlanActivity is the container of both fragments
                .add(R.id.frameLayout_PlanActivity, fragment1)
                .commit();
    }

    public void openFragmentEditPlan(Plan plan) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.frameLayout_PlanActivity, FragmentEditPlan.newInstance(plan))
                .addToBackStack("Fragment Edit Plan")
                .commit();
    }
}

【讨论】:

    猜你喜欢
    • 2019-02-25
    • 1970-01-01
    • 2015-01-20
    • 2012-01-16
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多