Hope you guys can help, I have a activity that has tabs, each tab is a fragment.
I am using the Sherlockfragment library and trying to work out how to handle buttons in the layouts of the fragments.
My code currently is:
Add the tab to the tabs
mTabsAdapter.addTab(mTabHost.newTabSpec("1").setIndicator("Page 1"),1.Fragment.class, null);
fragment class:
public class 1 extends SherlockFragmentActivity {
public static class Fragment extends SherlockFragment {
Fragment newInstance(int num) {
Fragment f = new Fragment();
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.1, container, false);
return v;
}
}
}
Then the layout is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content">
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:text="@string/page1" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
<ImageButton
android:id="@+id/btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="@drawable/bt1" />
</RelativeLayout>
I couldn’t find any useful samples of this in the Sherlock samples, but if there is a sample that I have missed let me know.
Inside the onCreateView() of the fragment, you can access the different views in your fragment layout using findViewById(), and assign the different listeners to them calling the corresponding methods, for instance, setOnClickListener(). You can also store references to the different views as private variables inside the fragment, for later use.