DialogFragment和Fragment之间的通信

时间:2019-11-27 00:35:11

标签: java android android-fragments android-alertdialog

我正在尝试将警报对话框中的信息传递给我的第二个片段(我使用的是选项卡式活动布局)。

当我单击ImageView时,我希望将警报对话框中的信息传递给片段,但是直到在MainActivity.java中实现我的界面之前,我的应用始终崩溃。我的主要任务是打开包含几个按钮的警报对话框。当我单击第一个按钮时,我要打印“ Test br 3”,但在我的Fragment中不起作用,它仅在MainActivity.java中起作用,其中方法将打印“ Test” br 2“。

我的主要活动

public class MainActivity extends AppCompatActivity implements ExercisesAlertDialog.DataTransfer {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
        ViewPager viewPager = findViewById(R.id.view_pager);
        viewPager.setAdapter(sectionsPagerAdapter);
        TabLayout tabs = findViewById(R.id.tabs);
        tabs.setupWithViewPager(viewPager);



    }

    @Override
    public void ApplyData() {
       System.out.println("Test br 2");
    }

我的片段

public class Frag2 extends Fragment implements ExercisesAlertDialog.DataTransfer {


      ImageView plusbtn;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.frag2, container, false);

        plusbtn = (ImageView) v.findViewById(R.id.plusbtn);


        plusbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            ExercisesDialog();
            }
        });



        return v;
    }

public void ExercisesDialog()
{
    ExercisesAlertDialog exercisesAlertDialog = new ExercisesAlertDialog();
    exercisesAlertDialog.show(getFragmentManager(), "Exercises Dialog");
}

    @Override
    public void ApplyData() {
        System.out.println("Test br 3");
    }

我的警报对话框

public class ExercisesAlertDialog extends AppCompatDialogFragment {

    ImageButton one, two;
    private DataTransfer listener;
    public int first;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View v = inflater.inflate(R.layout.workout_custom_menu, null);
        builder.setView(v);
        builder.setTitle("Choose your Workout");
        builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
               dismiss();
            }
        });

        one = (ImageButton) v.findViewById(R.id.first);
        two = (ImageButton) v.findViewById(R.id.second);

one.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        first = 1;
        if(first ==1)
        {
listener.ApplyData();
        }


    }
});


        return builder.create();
    }


    public interface DataTransfer
    {
        void ApplyData();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        try {
            listener = (DataTransfer) context;
        }
        catch (ClassCastException e)
        {
            throw new ClassCastException(toString() + "Must implement DataTransfer");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我在show()调用中更改了getChildFragmentManager()而不是getFragmentManager()。第二件事是onAttach()到监听者=(DataTransfer)getParentFragment();而不是上下文。