如何调用一个方法存在于主要活动的ActionBar Tab片段
问题描述:
我有一个应用程序使用ActionBar Tab Fragment来对不同标签中的数据进行分类。我需要从Tab Fragment调用一个方法,当我按下主活动(选项卡活动)中的按钮时。我尝试下面的代码如何调用一个方法存在于主要活动的ActionBar Tab片段
相机详细活动
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;
}
}
我喜欢称之为方法的validate()NetworkFragmentTab的从的onClick。
答
请尝试下面的代码,通过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();
}
}
答
传统的方法是在你的Activity中声明一个回调接口,并让你的Fragment实现它,然后将它们连接在一起。这是指导方针:https://developer.android.com/guide/components/fragments.html#EventCallbacks。
更好的方法是使用诸如Message Bus之类的东西去除Fragment和Activity之间的强依赖关系。有很多文章都在说这个。
如果你想更好地构建你的应用程序,并摆脱UI stuffs沟通的头痛,我建议你采用Model-View-Presenter框架。这里是一个例子:http://robo-creative.github.io/mvp。
你有什么错误? –
@SathishKumarJ没有错误上面的onClick代码只显示每个标签名称。请参阅我编辑的问题 – Riskhan