gpt4 book ai didi

java - DialogFragment与Fragment之间的通信

转载 作者:行者123 更新时间:2023-12-01 19:53:47 24 4
gpt4 key购买 nike

我正在尝试将信息从警报对话框传递到第二个 fragment (我正在使用选项卡式 Activity 布局)。

当我单击 ImageView 时,我想将信息从警报对话框传递到 fragment ,但我的应用程序一直崩溃,直到我在 MainActivity.java 中实现界面。我在这里的主要任务是打开包含几个按钮的警报对话框。当我单击第一个按钮时,我想打印“Test br 3”,但它在我的 fragment 中不起作用,它只在我的 MainActivity.java 中起作用,其中方法打印“Test br 2”。

我的主要 Activity

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");
}

我的 fragment

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");
}
}
}

最佳答案

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

关于java - DialogFragment与Fragment之间的通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59061406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com