[英]New to android, how do I initialize event handlers for elements inside a fragment
(Disclaimer: pretty new to android) So I am trying to make a tabbed application using Android Studio, and following examples online I have been able to use the actionbar to setup 2 tabs and fragments like so: (免责声明:android的新手)因此,我尝试使用Android Studio创建一个选项卡式应用程序,并且在网上以下示例中,我已经能够使用操作栏来设置2个选项卡和片段,如下所示:
in MainActivity.java 在MainActivity.java中
ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setTitle("My App Test");
Tab tabSearch = bar.newTab().setText("Search").setIcon(android.R.drawable.ic_menu_search);
Tab tabReport = bar.newTab().setText("Report").setIcon(android.R.drawable.ic_menu_report_image);
Fragment stfrag = new SearchTabFragment();
Fragment rptfrag = new ReportFragment();
tabSearch.setTabListener(new MyTabsListener(stfrag));
tabReport.setTabListener(new MyTabsListener(rptfrag));
bar.addTab(tabSearch);
bar.addTab(tabReport);
in ReportFragment/SearchTabFragment .java 在ReportFragment / SearchTabFragment .java中
public class SearchTabFragment extends Fragment {
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment_1,container,false);
}
}
I basically just copied the example for the ActionBar listener directly from the tutorial I found: 我基本上只是直接从发现的教程中复制了ActionBar侦听器的示例:
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment){
this.fragment = fragment;
};
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft){
ft.replace(R.id.wrap,fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft){
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft){
}
}
Now my question is, how do I attach eventHandlers to the elements defined in my fragments.xml? 现在的问题是,如何将eventHandlers附加到fragments.xml中定义的元素上? Previously in a "Single Activity" application I played with I just put it in the main java file right after the onCreate event.
以前,在我玩过的“单一活动”应用程序中,我只是将它放在onCreate事件之后的主Java文件中。 Eg:
例如:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
(TextView) tv = (TextView) findViewByid(R.id.textviewidhere);
//now I attach the handlers as I need
Thanks in advance! 提前致谢! (edited for clarity)
(为清楚起见进行了编辑)
You should probably read a little bit about fragments . 您可能应该阅读一些有关片段的信息 。 You need to override onViewCreated and "catch" views there:
您需要在其中覆盖onViewCreated和“ catch”视图:
public class MyFragment extends Fragment {
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.my_fragment, container,
false);
return rootView;
}
//and you use rootView to call findViewById
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button myButton = (Button) rootView.findViewById(R.id.mybutton);
//or you can set some other listener, or "catch" some different view -checkbox, //textview etc
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do something you want
}
});
}
Example : 范例:
public class SearchTabFragment extends Fragment {
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState){
View root = inflater.inflate(R.layout.fragment_1,container,false);
TextView text = (TextView)root.findViewById(R.id.mybutton);
return root;
}
} }
this will let use the TextView inside the fragment. 这样就可以在片段内使用TextView了。
Modifed code lifted straight from the docs : 修改后的代码直接来自docs :
Let us say you have a Fragment
with a button inside it. 假设您有一个
Fragment
,里面有一个按钮。 You would like to attach the activity as the event listener to that button inside fragment. 您想将活动作为事件侦听器附加到片段中的该按钮。 Then,
然后,
public static class ButtonFragment extends Fragment implements OnClickListener {
private OnClickListener mListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// inflate the corresponding fragment XML
View v = inflater.inflate(R.layout.example_fragment, container, false);
// grab the button and attach this fragment as its listener
((Button)v.findViewById(R.id.btn)).setOnClickListener(this);
return v;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
// once this fragment is attached to its activity, check to see
// if it implements OnClickListener
mListener = (OnClickListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " doesn't implement OnClickListener");
}
}
...
public void onClick(View v) {
if(mListener){
// once the button inside fragment is clicked,
// notify the activity about the same
mListener.onClickListener(v);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.