如何从 main activity 调用 ActionBar Tab Fragment 中存在的方法

How to call a method exist in ActionBar Tab Fragment from main activity

我有一个应用程序使用 ActionBar Tab Fragment 对不同选项卡中的数据进行分类。当我从 main activity( tab activity) 按下按钮时,我需要从 Tab Fragment 调用一个方法。我试过下面的代码

相机详情Activity

public class CameraDetails extends Activity {
ActionBar.Tab networkTab, userTab;
Fragment networkFragmentTab = new NetworkFragmentTab();
Fragment userFragmentTab = new UserFragmentTab();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cameradetails);

    // Asking for the default ActionBar element that our platform supports.
    ActionBar actionBar = getActionBar();

    // Screen handling while hiding ActionBar icon.
    actionBar.setDisplayShowHomeEnabled(false);

    // Screen handling while hiding Actionbar title.
    actionBar.setDisplayShowTitleEnabled(false);

    // Creating ActionBar tabs.
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Setting custom tab icons.
    networkTab = actionBar.newTab().setText("Network"); //.setIcon(R.drawable.bmw_logo);
    userTab = actionBar.newTab().setText("User Account");

    // Setting tab listeners.
    networkTab.setTabListener(new TabListener(networkFragmentTab));
    userTab.setTabListener(new TabListener(userFragmentTab));

    // Adding tabs to the ActionBar.
    actionBar.addTab(networkTab);
    actionBar.addTab(userTab);

    actionBar.setSelectedNavigationItem(1);
    }

public void onClick(View v){
  // call method validate from NetworkFragment like networkfragment.validate();
    }
}
}   

网络片段

public class NetworkFragmentTab extends Fragment {
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.network_layout, container, false);
    return rootView;
}

public boolean Validate(){
    EditText etIpAddress = (EditText)rootView.findViewById(R.id.cd_ip_address);
    Toast.makeText(getActivity(), etIpAddress.getText().toString(), Toast.LENGTH_LONG).show();
    return true;
}
 }

我喜欢从 onClick 调用 NetworkFragmentTab 的方法 validate()

传统的方式是在你的Activity中声明一个回调接口,让你的Fragment实现它,然后将它们连接在一起。以下是指南:https://developer.android.com/guide/components/fragments.html#EventCallbacks.

更好的方法是使用消息总线之类的东西来消除 Fragment 和 Activity 之间的强依赖性。有很多文章都在谈论这个。

如果你想更好地构建你的应用程序并摆脱UI 东西通信中的头痛,我建议你采用模型-视图-Presenter 框架。这是一个例子:http://robo-creative.github.io/mvp.

尝试使用以下代码通过 Interface 进行回调。您必须创建一个 Interface,并且 Interface 将具有一个方法,该方法将用于从您的 Activity 调用 Fragment 的方法。试试下面的代码方法,它会解决你的问题。

class MyActivity extends Activity {
    private MyInterface mInterface;

    public interface MyInterface {
        void theMethodOfInterface();
    }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cameradetails);
    ...
  }

  public void onClick(View v) {
    mInterface.theMethodOfInterface();
  }

  public void setMyListener(MyInterface listner) {
    this.mInterface = listener;
  }
}

片段如下:

class MyFragment extends Fragment implements MyInterface {
    ...

      @Override
  public void onCreateView(Bundle savedInstanceState) {
    ...// your code
    ((MyActivity)getActivity()).setMyListener(this);
    ...// your code
  }

    public void someMethodOfFragment() {
        ...
        // your code for method of fragment here
    }

    @Override
    public void theMethodOfInterface() {
        someMethodOfFragment();
    }

}