如何从主活动调用存在于动作栏选项卡片段中的方法



我有一个应用程序,它使用动作栏选项卡片段对不同选项卡中的数据进行分类。 当我按下主活动(选项卡活动)中的按钮时,我需要从选项卡片段调用一个方法。 我尝试了以下代码

相机详细信息活动

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()。

尝试下面的代码通过 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();
    }
}

传统的方法是在活动中声明一个回调接口,让片段实现它,然后将它们连接在一起。以下是准则:https://developer.android.com/guide/components/fragments.html#EventCallbacks。

更好的方法是使用消息总线之类的东西来删除片段和活动之间的强依赖关系。有很多文章说这个。

如果你想更好地构建你的应用程序,摆脱UI东西沟通中的麻烦,我建议你采用模型-视图-表示器框架。下面是一个示例:http://robo-creative.github.io/mvp。

最新更新