Advertisement
Guest User

Untitled

a guest
Jan 25th, 2015
3,669
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.tabwithslidingdrawer;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import android.support.v7.app.ActionBar.Tab;
  7. import android.support.v7.app.ActionBar.TabListener;
  8. import android.support.v7.app.ActionBarActivity;
  9. import android.support.v7.app.ActionBar;
  10. import android.support.v4.app.ActionBarDrawerToggle;
  11. import android.support.v4.app.Fragment;
  12. import android.support.v4.app.FragmentManager;
  13. import android.support.v4.app.FragmentPagerAdapter;
  14. import android.support.v4.app.FragmentStatePagerAdapter;
  15. import android.support.v4.app.FragmentTransaction;
  16. import android.support.v4.view.GravityCompat;
  17. import android.support.v4.view.ViewPager;
  18. import android.support.v4.widget.DrawerLayout;
  19. import android.text.Html;
  20. import android.content.Context;
  21. import android.content.Intent;
  22. import android.content.res.Configuration;
  23. import android.content.res.TypedArray;
  24. import android.graphics.Color;
  25. import android.net.Uri;
  26. import android.os.Bundle;
  27. import android.util.Log;
  28. import android.view.LayoutInflater;
  29. import android.view.Menu;
  30. import android.view.MenuItem;
  31. import android.view.View;
  32. import android.view.ViewGroup;
  33. import android.widget.AdapterView;
  34. import android.widget.ListView;
  35.  
  36. public class MainActivity extends ActionBarActivity implements
  37. android.support.v7.app.ActionBar.OnNavigationListener, TabListener {
  38. String[] tabsName_thatstamil = { "1", "2", "3",
  39. "4", "5" };
  40.  
  41. ViewPager viewPager;
  42. TapFragmentManager fragmentManger;
  43.  
  44. ActionBar actionBar;
  45. String[] menutitles;
  46. TypedArray menuIcons;
  47. // nav drawer title
  48. private CharSequence mTitle;
  49. private DrawerLayout mDrawerLayout;
  50. private ListView mDrawerList;
  51. private ActionBarDrawerToggle mDrawerToggle;
  52. private List<DrawingRowItem> rowItems;
  53. private CustomAdapter adapter;
  54. int tabsize;
  55.  
  56. int selectedpositoin;
  57.  
  58. @Override
  59. protected void onCreate(Bundle savedInstanceState) {
  60. super.onCreate(savedInstanceState);
  61. setContentView(R.layout.activity_main);
  62.  
  63. viewPager = (ViewPager) findViewById(R.id.pager);
  64.  
  65. actionBar = getSupportActionBar();
  66. actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
  67.  
  68. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  69. getSupportActionBar().setHomeButtonEnabled(true);
  70.  
  71. fragmentManger = new TapFragmentManager(getSupportFragmentManager());
  72. viewPager.setAdapter(fragmentManger);
  73.  
  74. actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  75.  
  76. menutitles = getResources().getStringArray(R.array.news_Sites);
  77. mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  78. mDrawerList = (ListView) findViewById(R.id.slider_list);
  79.  
  80. rowItems = new ArrayList<DrawingRowItem>();
  81.  
  82. for (int i = 0; i < menutitles.length; i++) {
  83. DrawingRowItem items = new DrawingRowItem(menutitles[i]);
  84. rowItems.add(items);
  85. }
  86.  
  87. getSupportActionBar().setTitle(
  88. Html.fromHtml("<font color=\"black\">" + mTitle + " - "
  89. + menutitles[0] + "</font>"));
  90. // getActionBar().setTitle(mTitle +menutitles[0]);
  91. adapter = new CustomAdapter(getApplicationContext(), rowItems);
  92. mDrawerList.setAdapter(adapter);
  93. mDrawerList.setOnItemClickListener(new SlideitemListener());
  94. // enabling action bar app icon and behaving it as toggle button
  95. getActionBar().setDisplayHomeAsUpEnabled(true);
  96. getActionBar().setHomeButtonEnabled(true);
  97.  
  98. mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
  99. R.drawable.ic_drawer, R.string.app_name, R.string.app_name) {
  100. public void onDrawerClosed(View view) {
  101. // getActionBar().setTitle(mTitle
  102. // +menutitles[selectedpositoin]);
  103. getSupportActionBar().setTitle(
  104. Html.fromHtml("<font color=\"black\">" + mTitle + " - "
  105. + menutitles[selectedpositoin] + "</font>"));
  106. // calling onPrepareOptionsMenu() to show action bar icons
  107. invalidateOptionsMenu();
  108. }
  109.  
  110. public void onDrawerOpened(View drawerView) {
  111. getActionBar().setTitle(
  112. Html.fromHtml("<font color=\"grey\">"
  113. + "Select News papers" + "</font>"));
  114. // calling onPrepareOptionsMenu() to hide action bar icons
  115. invalidateOptionsMenu();
  116. }
  117. };
  118.  
  119. mDrawerLayout.setDrawerListener(mDrawerToggle);
  120.  
  121. for (String str : tabsName_thatstamil) {
  122. actionBar.addTab(actionBar.newTab().setText(str)
  123. .setTabListener(this));
  124. }
  125.  
  126. viewPager
  127. .setOnPageChangeListener(new android.support.v4.view.ViewPager.OnPageChangeListener() {
  128.  
  129. @Override
  130. public void onPageSelected(int tabPosition) {
  131. actionBar.setSelectedNavigationItem(tabPosition);
  132.  
  133. }
  134.  
  135. @Override
  136. public void onPageScrolled(int arg0, float arg1, int arg2) {
  137. // TODO Auto-generated method stub
  138.  
  139. }
  140.  
  141. @Override
  142. public void onPageScrollStateChanged(int arg0) {
  143. // TODO Auto-generated method stub
  144.  
  145. }
  146. });
  147.  
  148. }
  149.  
  150. class SlideitemListener implements ListView.OnItemClickListener {
  151. @Override
  152. public void onItemClick(AdapterView<?> parent, View view, int position,
  153. long id) {
  154.  
  155. // / Do Here your fragment things
  156.  
  157. fragmentManger = new TapFragmentManager(getSupportFragmentManager());
  158. viewPager.setAdapter(fragmentManger);
  159. mDrawerLayout.closeDrawer(mDrawerList);
  160. }
  161.  
  162. }
  163.  
  164. /**
  165. * A placeholder fragment containing a simple view.
  166. */
  167. public static class PlaceholderFragment extends Fragment {
  168.  
  169. public PlaceholderFragment() {
  170. }
  171.  
  172. @Override
  173. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  174. Bundle savedInstanceState) {
  175. View rootView = inflater.inflate(R.layout.fragment_main, container,
  176. false);
  177. return rootView;
  178. }
  179. }
  180.  
  181. @Override
  182. public boolean onNavigationItemSelected(int itemPosition, long itemId) {
  183. // TODO Auto-generated method stub
  184. return false;
  185. }
  186.  
  187. public class TapFragmentManager extends FragmentStatePagerAdapter {
  188.  
  189. public TapFragmentManager(FragmentManager fm) {
  190. super(fm);
  191.  
  192. }
  193.  
  194. @Override
  195. public SampleFragment getItem(int tab_position) {
  196. return new SampleFragment();
  197.  
  198. }
  199.  
  200. @Override
  201. public int getCount() {
  202. // TODO Auto-generated method stub
  203. return tabsName_thatstamil.length;
  204. }
  205.  
  206. }
  207.  
  208. @Override
  209. public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
  210. // TODO Auto-generated method stub
  211.  
  212. }
  213.  
  214. @Override
  215. public void onTabSelected(Tab tab, FragmentTransaction arg1) {
  216. // TODO Auto-generated method stub
  217. viewPager.setCurrentItem(tab.getPosition());
  218. // viewPager.notifyAll();
  219. }
  220.  
  221. @Override
  222. public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
  223. // TODO Auto-generated method stub
  224.  
  225. }
  226.  
  227. @Override
  228. protected void onPostCreate(Bundle savedInstanceState) {
  229. super.onPostCreate(savedInstanceState);
  230. // Sync the toggle state after onRestoreInstanceState has occurred.
  231. mDrawerToggle.syncState();
  232. }
  233.  
  234. @Override
  235. public void onConfigurationChanged(Configuration newConfig) {
  236. super.onConfigurationChanged(newConfig);
  237. // Pass any configuration change to the drawer toggles
  238. mDrawerToggle.onConfigurationChanged(newConfig);
  239. }
  240.  
  241. @Override
  242. public boolean onCreateOptionsMenu(Menu menu) {
  243. getMenuInflater().inflate(R.menu.main, menu);
  244. return true;
  245. }
  246.  
  247. @Override
  248. public boolean onOptionsItemSelected(MenuItem item) {
  249. // toggle nav drawer on selecting action bar app icon/title
  250. if (mDrawerToggle.onOptionsItemSelected(item)) {
  251. return true;
  252. }
  253. // Handle action bar actions click
  254. int id = item.getItemId();
  255. if (id == R.id.action_settings) {
  256. startActivity(new Intent(
  257. Intent.ACTION_VIEW,
  258. Uri.parse("https://play.google.com/store/apps/details?id=com.flashnews.tamilflashnews")));
  259. return true;
  260. }
  261. return super.onOptionsItemSelected(item);
  262.  
  263. }
  264.  
  265. /***
  266. * Called when invalidateOptionsMenu() is triggered
  267. */
  268. @Override
  269. public boolean onPrepareOptionsMenu(Menu menu) {
  270. // if nav drawer is opened, hide the action items
  271. boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
  272. menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
  273.  
  274. return super.onPrepareOptionsMenu(menu);
  275. }
  276.  
  277. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement