简体   繁体   English

主要活动的ActionBar选项卡片段中如何调用方法

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

I have an application which uses ActionBar Tab Fragment for categorizing data in different tabs. 我有一个使用ActionBar选项卡片段对不同选项卡中的数据进行分类的应用程序。 I need to call a method from Tab Fragment when i presses a button from main activity( tab activity). 当我从主要活动(标签活动)中按下按钮时,我需要从标签片段中调用方法。 I tried below code 我尝试下面的代码

Camera Details 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();
    }
}
}   

NetworkFragment 网络片段

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

I like to call method validate() of NetworkFragmentTab from onClick. 我喜欢从onClick调用NetworkFragmentTab的validate()方法。

The traditional way is to declare a callback interface in your Activity and let your Fragment implement it, then connect them together. 传统方法是在Activity中声明一个回调接口,然后让Fragment实现它,然后将它们连接在一起。 Here is the guidelines: https://developer.android.com/guide/components/fragments.html#EventCallbacks . 以下是准则: https : //developer.android.com/guide/components/fragments.html#EventCallbacks

The better way is to use something like Message Bus to remove strong dependencies between your Fragment and Activity. 更好的方法是使用诸如Message Bus之类的东西来消除Fragment和Activity之间的强依赖性。 There are plenty of articles saying about this. 关于此的文章很多。

If you want to structure your application better and get rid of the headache in UI stuffs' communication, I recommend you to adopt a Model-View-Presenter framework. 如果您想更好地构建应用程序结构并摆脱UI内容通信中的麻烦,我建议您采用Model-View-Presenter框架。 Here is an example: http://robo-creative.github.io/mvp . 这是一个示例: http : //robo-creative.github.io/mvp

Try below code for callback via Interface . 尝试下面的代码通过Interface进行回调。 You have to create one Interface and that Interface will have one method which will be used to call the method of Fragment from your Activity . 您必须创建一个Interface ,该Interface将具有一个方法,该方法将用于从Activity调用Fragment方法。 Try below code approach, it will solve your problem. 请尝试以下代码方法,它将解决您的问题。

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

The Fragment is as below: 片段如下:

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM