繁体   English   中英

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

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

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

相机详细信息活动

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

更好的方法是使用诸如Message Bus之类的东西来消除Fragment和Activity之间的强依赖性。 关于此的文章很多。

如果您想更好地构建应用程序结构并摆脱UI内容通信中的麻烦,我建议您采用Model-View-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();
    }

}

暂无
暂无

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

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