【问题标题】:How to execute an method in Fragment B from a Fragment A如何从片段 A 执行片段 B 中的方法
【发布时间】:2013-12-23 23:29:18
【问题描述】:

我是开发Android的新手,现在在Fragment之间调用方法时遇到问题。让我描述一下,希望大家能帮我解决。

片段 A

public class A extends Fragment implements OnItemClickListener {
   .........
   .........
   .........
   public void showContent(int pSelectedIndex, int pSelectedSubIndex) {
    // Create fragment and give it an argument specifying the article it
    RelativeLayout thisTopLayout = (RelativeLayout)getActivity().findViewById(R.id.directoryTopRelativeLayout);
    thisTopLayout.setVisibility(LinearLayout.GONE);
    RelativeLayout thisBodyLayout = (RelativeLayout)getActivity().findViewById(R.id.directoryBodyRelativeLayout);
    thisBodyLayout.setVisibility(LinearLayout.GONE);

    FragmentTransaction transaction = getFragmentManager()
            .beginTransaction();
    if (pSelectedIndex == 1) {
        BusinessView thisItem = new BusinessView();
        transaction.replace(R.id.directoryLayout, thisItem);
        thisItem.DetectContentType(pSelectedSubIndex, this.getActivity());
    }
      }
}

片段 B

public class B extends Fragment implements OnItemClickListener {
    @SuppressWarnings("deprecation")
public void DetectContentType(int selectedType, Activity pActivity){
    if (selectedType != 1) {
        AlertDialog alertDialog = new AlertDialog.Builder(pActivity)
                .create();
        alertDialog.setTitle("EXAMPLE");
        alertDialog
                .setMessage("SHOW MESSAGE");
        alertDialog.setButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(final DialogInterface dialog,
                    final int which) {
                // here you can add functions
            }
        });
        alertDialog.setButton2("NO", new DialogInterface.OnClickListener() {
            public void onClick(final DialogInterface dialog,
                    final int which) {
                // here you can add functions
            }
        });
        alertDialog.show();
    } else {
        showContent(selectedType);
    }
}

public void showContent(int pSelectedIndex) {
    // Create fragment and give it an argument specifying the article it
    RelativeLayout thisTopLayout = (RelativeLayout) this.getActivity().findViewById(R.id.businessTopRelativeLayout);
    thisTopLayout.setVisibility(LinearLayout.GONE);
    RelativeLayout thisBodyLayout = (RelativeLayout) this.getActivity()
            .findViewById(R.id.businessBodyRelativeLayout);
    thisBodyLayout.setVisibility(LinearLayout.GONE);

    FragmentTransaction transaction = getFragmentManager()
            .beginTransaction();
    BusinessItemView thisItem = new BusinessItemView(pSelectedIndex);
    transaction.replace(R.id.businessLayout, thisItem);
    // Commit the transaction
    transaction.commit();
}
}

这是我的类,我想在片段A中执行片段B中的方法“showContent”,但我只能显示Alert,但方法“showContent”总是出现崩溃错误。

请告诉我你对这个案子的想法。 非常感谢。

【问题讨论】:

  • 添加你的 logcat 输出。

标签: android view fragment


【解决方案1】:

在片段 A 中,您需要稍微改变您的方法调用;

FragmentTransaction transaction = getFragmentManager()
        .beginTransaction();
if (pSelectedIndex == 1) {
    BusinessView thisItem = new BusinessView();
    transaction.replace(R.id.directoryLayout, thisItem);
    transaction.commit();
    thisItem.DetectContentType(pSelectedSubIndex, this.getActivity());
}

片段 B 上的 DetectContentType 方法无法对 AlertDialogs 执行任何操作,直到通过事务通过调用提交附加片段。

在您的特定情况下,尽管提供了用于 AlertDialog 构建器的活动,这对您的情况应该没问题,尽管如果提交您在 pSelectedSubIndex 中传递的事务则不需要,但不知道从在您的示例中,您可能会在片段 B 上点击 showContent(),这肯定会失败,因为在您开始片段生命周期之前您无法附加视图。

【讨论】:

  • 感谢您的回答马库斯。这对我来说是非常有用的。但不幸的是,即使我设置了“提交”来完成该事务,我仍然无法运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多