Advertisement
karthi123

Untitled

Mar 14th, 2013
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.99 KB | None | 0 0
  1. public class EventsActivity extends SherlockFragmentActivity {
  2.  
  3.  
  4. ViewPager mViewPager;
  5. TabsAdapter mTabsAdapter;
  6.  
  7. @Override
  8. public void onCreate(Bundle savedInstanceState) {
  9. requestWindowFeature(com.actionbarsherlock.view.Window.FEATURE_NO_TITLE);
  10. super.onCreate(savedInstanceState);
  11.  
  12.  
  13. mViewPager = new ViewPager(this);
  14. mViewPager.setId(R.id.pager);
  15.  
  16. setContentView(mViewPager);
  17. setTitle("Events");
  18.  
  19.  
  20. ActionBar bar = getSupportActionBar();
  21. bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  22.  
  23. mTabsAdapter = new TabsAdapter(this, mViewPager);
  24.  
  25. mTabsAdapter.addTab(bar.newTab().setText("Featured"),
  26. EventsFeatured.class, null);
  27. mTabsAdapter.addTab(bar.newTab().setText("All"), EventsAll.class, null);
  28. mTabsAdapter.addTab(bar.newTab().setText("Nearby"), EventsNearBy.class,
  29. null);
  30.  
  31. }
  32.  
  33. public static class TabsAdapter extends FragmentPagerAdapter implements
  34. ActionBar.TabListener, ViewPager.OnPageChangeListener {
  35. private final Context mContext;
  36. private final ActionBar mActionBar;
  37. private final ViewPager mViewPager;
  38. private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
  39.  
  40. static final class TabInfo {
  41. private final Class<?> clss;
  42. private final Bundle args;
  43.  
  44. TabInfo(Class<?> _class, Bundle _args) {
  45. clss = _class;
  46. args = _args;
  47. }
  48. }
  49.  
  50. public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
  51. super(activity.getSupportFragmentManager());
  52. mContext = activity;
  53. mActionBar = activity.getSupportActionBar();
  54. mViewPager = pager;
  55. mViewPager.setAdapter(this);
  56. mViewPager.setOnPageChangeListener(this);
  57.  
  58. }
  59.  
  60. public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
  61. TabInfo info = new TabInfo(clss, args);
  62. tab.setTag(info);
  63. tab.setTabListener(this);
  64. mTabs.add(info);
  65. mActionBar.addTab(tab);
  66. notifyDataSetChanged();
  67. }
  68.  
  69. @Override
  70. public int getCount() {
  71. return mTabs.size();
  72. }
  73.  
  74. @Override
  75. public Fragment getItem(int position) {
  76. TabInfo info = mTabs.get(position);
  77. return Fragment.instantiate(mContext, info.clss.getName(),
  78. info.args);
  79. }
  80.  
  81. public void onPageScrolled(int position, float positionOffset,
  82. int positionOffsetPixels) {
  83. }
  84.  
  85. public void onPageSelected(int position) {
  86. mActionBar.setSelectedNavigationItem(position);
  87. }
  88.  
  89. public void onPageScrollStateChanged(int state) {
  90. }
  91.  
  92. public void onTabSelected(Tab tab, FragmentTransaction ft) {
  93. Object tag = tab.getTag();
  94. for (int i = 0; i < mTabs.size(); i++) {
  95. if (mTabs.get(i) == tag) {
  96. mViewPager.setCurrentItem(i);
  97. }
  98. }
  99. }
  100.  
  101. public void onTabUnselected(Tab tab, FragmentTransaction ft) {
  102. }
  103.  
  104. public void onTabReselected(Tab tab, FragmentTransaction ft) {
  105. }
  106. }
  107. EVENTS FEATURED CLASS:
  108. public class EventsFeatured extends SherlockFragment {
  109. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  110. Bundle savedInstanceState) {
  111. View view = inflater
  112. .inflate(R.layout.events_featured, container, false);
  113. Button b = (Button) view.findViewById(R.id.buttonA);
  114. b.setOnClickListener(new View.OnClickListener() {
  115.  
  116. @Override
  117. public void onClick(View v) {
  118. // TODO Auto-generated method stub
  119. Toast.makeText(getSherlockActivity(), "Button Clicked", 0)
  120. .show();
  121. }
  122. });
  123. return view;
  124. }
  125. }
  126. EVENTS ALL CLASS:
  127. public class EventsAll extends SherlockFragment {
  128. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  129. Bundle savedInstanceState) {
  130. View view = inflater.inflate(R.layout.events_all, container, false);
  131. Button b = (Button) view.findViewById(R.id.buttonA);
  132. b.setOnClickListener(new View.OnClickListener() {
  133.  
  134. @Override
  135. public void onClick(View v) {
  136. // TODO Auto-generated method stub
  137. Toast.makeText(getSherlockActivity(), "Button Clicked", 0)
  138. .show();
  139. }
  140. });
  141. return view;
  142. }
  143. }
  144. EVENTS NEARBY CLASS:
  145. public class EventsNearBy extends SherlockFragment {
  146. @Override
  147. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  148. Bundle savedInstanceState) {
  149. View view = inflater.inflate(R.layout.events_nearby, container, false);
  150. return view;
  151. }
  152. }
  153. events.xml:
  154. <?xml version="1.0" encoding="utf-8"?>
  155. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  156. android:layout_width="fill_parent"
  157. android:layout_height="wrap_content"
  158. android:orientation="vertical" >
  159.  
  160. <android.support.v4.view.ViewPager
  161. android:id="@+id/pager"
  162. android:layout_width="match_parent"
  163. android:layout_height="wrap_content" >
  164. </android.support.v4.view.ViewPager>
  165.  
  166.  
  167. </LinearLayout>
  168. events_featured.xml:
  169. <?xml version="1.0" encoding="utf-8"?>
  170. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  171. android:layout_width="match_parent"
  172. android:layout_height="match_parent" >
  173.  
  174. <ScrollView
  175. android:id="@+id/scrollView1"
  176. android:layout_width="match_parent"
  177. android:layout_height="wrap_content"
  178. android:layout_alignParentLeft="true"
  179. android:layout_alignParentTop="true" >
  180.  
  181. <LinearLayout
  182. android:layout_width="match_parent"
  183. android:layout_height="match_parent"
  184. android:orientation="vertical" >
  185.  
  186. <TextView
  187. android:id="@+id/textView1"
  188. android:layout_width="wrap_content"
  189. android:layout_height="wrap_content"
  190. android:layout_centerHorizontal="true"
  191. android:layout_centerVertical="true"
  192. android:text="Featured"
  193. android:textAppearance="?android:attr/textAppearanceLarge" />
  194.  
  195. <ProgressBar
  196. android:id="@+id/progressBar1"
  197. style="?android:attr/progressBarStyleLarge"
  198. android:layout_width="wrap_content"
  199. android:layout_height="wrap_content"
  200. android:layout_alignLeft="@+id/textView1"
  201. android:layout_below="@+id/textView1"
  202. android:layout_marginLeft="26dp"
  203. android:layout_marginTop="42dp" />
  204.  
  205. <Button
  206. android:id="@+id/buttonA"
  207. android:layout_width="wrap_content"
  208. android:layout_height="wrap_content"
  209. android:layout_above="@+id/textView1"
  210. android:layout_alignLeft="@+id/textView1"
  211. android:layout_marginBottom="59dp"
  212. android:text="Button" />
  213.  
  214. </LinearLayout>
  215. </ScrollView>
  216.  
  217. </RelativeLayout>
  218. events_all.xml:
  219. <?xml version="1.0" encoding="utf-8"?>
  220. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  221. android:layout_width="match_parent"
  222. android:layout_height="match_parent" >
  223.  
  224. <TextView
  225. android:id="@+id/textView1"
  226. android:layout_width="wrap_content"
  227. android:layout_height="wrap_content"
  228. android:layout_centerHorizontal="true"
  229. android:layout_centerVertical="true"
  230. android:text="Featured All"
  231. android:textAppearance="?android:attr/textAppearanceLarge" />
  232.  
  233. <Button
  234. android:id="@+id/buttonA"
  235. android:layout_width="wrap_content"
  236. android:layout_height="wrap_content"
  237. android:layout_above="@+id/textView1"
  238. android:layout_centerHorizontal="true"
  239. android:layout_marginBottom="21dp"
  240. android:text="Two" />
  241.  
  242. </RelativeLayout>
  243.  
  244. events_nearby.xml:
  245. <?xml version="1.0" encoding="utf-8"?>
  246. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  247. android:layout_width="match_parent"
  248. android:layout_height="match_parent" >
  249.  
  250. <TextView
  251. android:id="@+id/textView1"
  252. android:layout_width="wrap_content"
  253. android:layout_height="wrap_content"
  254. android:layout_centerHorizontal="true"
  255. android:layout_centerVertical="true"
  256. android:text="Near By"
  257. android:textAppearance="?android:attr/textAppearanceLarge" />
  258.  
  259. </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement