Advertisement
Guest User

Untitled

a guest
Sep 16th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.42 KB | None | 0 0
  1. public class MainActivity extends FragmentActivity {
  2.  
  3. /**
  4. * The {@link android.support.v4.view.PagerAdapter} that will provide
  5. * fragments for each of the sections. We use a
  6. * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
  7. * will keep every loaded fragment in memory. If this becomes too memory
  8. * intensive, it may be best to switch to a
  9. * {@link android.support.v4.app.FragmentStatePagerAdapter}.
  10. */
  11. SectionsPagerAdapter mSectionsPagerAdapter;
  12.  
  13. /**
  14. * The {@link ViewPager} that will host the section contents.
  15. */
  16. ViewPager mViewPager;
  17. TextView tw;
  18. ActionBar myActionBar;
  19. ArrayList<Category> ListOfCategory;
  20.  
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25.  
  26. myActionBar = getActionBar();
  27. myActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  28.  
  29. ListOfCategory = new ArrayList<Category>();
  30.  
  31. try {
  32. List<String> categList = new ExecuteJsonLinkTask().execute("http://www.76actu.fr/json.php?d=categories&a=list&key=8smA5YjLG1132zbz301tM94jZO30B7dW").get();
  33.  
  34. for (String item : categList) {
  35.  
  36. JSONArray jArray = new JSONArray(item);
  37.  
  38. for(int i=0;i<jArray.length();i++){
  39.  
  40. JSONObject json_data = jArray.getJSONObject(i);
  41.  
  42. Category categ = new Category();
  43. categ.setCategIdSite(json_data.getInt("term_id"));
  44. categ.setName(json_data.getString("name"));
  45. categ.setMoreArticle(true);
  46. categ.setArticleAvailable(false);
  47.  
  48. if(json_data.getInt("term_id") == 89871){
  49. categ.setActive(false);
  50. } else categ.setActive(true);
  51.  
  52. Tab tab = myActionBar.newTab();
  53. tab.setText(json_data.getString("name"));
  54. Object obj = new Object();
  55. obj = String.valueOf(json_data.getInt("term_id"));
  56. tab.setTag(obj);
  57. tab.setTabListener(new MyTabListener(getBaseContext()));
  58. // tab.setTabListener(new MyTabListener(getBaseContext()));
  59. myActionBar.addTab(tab);
  60.  
  61. ListOfCategory.add(categ);
  62. }
  63. }
  64. } catch (InterruptedException e) {
  65. // TODO Auto-generated catch block
  66. e.printStackTrace();
  67. } catch (ExecutionException e) {
  68. // TODO Auto-generated catch block
  69. e.printStackTrace();
  70. } catch (JSONException e) {
  71. // TODO Auto-generated catch block
  72. e.printStackTrace();
  73. }
  74.  
  75. // Create the adapter that will return a fragment for each of the three
  76. // primary sections of the app.
  77. mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
  78.  
  79. // Set up the ViewPager with the sections adapter.
  80. mViewPager = (ViewPager) findViewById(R.id.pager);
  81. mViewPager.setAdapter(mSectionsPagerAdapter);
  82. mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
  83.  
  84. @Override
  85. public void onPageSelected(int arg0) {
  86. myActionBar.setSelectedNavigationItem(arg0);
  87.  
  88. }
  89.  
  90. @Override
  91. public void onPageScrolled(int arg0, float arg1, int arg2) {
  92. // TODO Auto-generated method stub
  93.  
  94. }
  95.  
  96. @Override
  97. public void onPageScrollStateChanged(int arg0) {
  98. // TODO Auto-generated method stub
  99.  
  100. }
  101. });
  102.  
  103.  
  104.  
  105. }
  106.  
  107. @Override
  108. public boolean onCreateOptionsMenu(Menu menu) {
  109. // Inflate the menu; this adds items to the action bar if it is present.
  110. getMenuInflater().inflate(R.menu.main, menu);
  111. return true;
  112. }
  113.  
  114. /**
  115. * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
  116. * one of the sections/tabs/pages.
  117. */
  118. public class SectionsPagerAdapter extends FragmentPagerAdapter {
  119.  
  120. private ArrayList<Category> ListOfCategory = new ArrayList<Category>();
  121. Fragment fragment;
  122. ActionBar myActionBar;
  123.  
  124. public SectionsPagerAdapter(FragmentManager fm){
  125. super(fm);
  126.  
  127. myActionBar = getActionBar();
  128.  
  129. try {
  130. List<String> categList = new ExecuteJsonLinkTask().execute("[API_LINK]").get();
  131.  
  132. for (String item : categList) {
  133.  
  134. JSONArray jArray = new JSONArray(item);
  135.  
  136. for(int i=0;i<jArray.length();i++){
  137.  
  138. JSONObject json_data = jArray.getJSONObject(i);
  139.  
  140. Category categ = new Category();
  141. categ.setCategIdSite(json_data.getInt("term_id"));
  142. categ.setName(json_data.getString("name"));
  143. categ.setMoreArticle(true);
  144. categ.setArticleAvailable(false);
  145.  
  146. if(json_data.getInt("term_id") == 89871){
  147. categ.setActive(false);
  148. } else categ.setActive(true);
  149.  
  150. ListOfCategory.add(categ);
  151. }
  152. }
  153. } catch (InterruptedException e) {
  154. // TODO Auto-generated catch block
  155. e.printStackTrace();
  156. } catch (ExecutionException e) {
  157. // TODO Auto-generated catch block
  158. e.printStackTrace();
  159. } catch (JSONException e) {
  160. // TODO Auto-generated catch block
  161. e.printStackTrace();
  162. }
  163.  
  164. }
  165.  
  166. @Override
  167. public Fragment getItem(int position) {
  168. // getItem is called to instantiate the fragment for the given page.
  169. // Return a DummySectionFragment (defined as a static inner class
  170. // below) with the page number as its lone argument.
  171. fragment = new DummySectionFragment();
  172. Bundle args = new Bundle();
  173. args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position);
  174. args.putInt("categId", ListOfCategory.get(position).getCategIdSite());
  175. fragment.setArguments(args);
  176. return fragment;
  177. }
  178.  
  179. @Override
  180. public int getCount() {
  181. // Show total pages.
  182. return ListOfCategory.size();
  183. }
  184.  
  185. @Override
  186. public CharSequence getPageTitle(int position) {
  187.  
  188. return ListOfCategory.get(position).getName();
  189. }
  190.  
  191. }
  192.  
  193. /**
  194. * A dummy fragment representing a section of the app, but that simply
  195. * displays dummy text.
  196. */
  197. public static class DummySectionFragment extends Fragment {
  198. /**
  199. * The fragment argument representing the section number for this
  200. * fragment.
  201. */
  202. TextView dummyTextView;
  203.  
  204. public static final String ARG_SECTION_NUMBER = "section_number";
  205.  
  206. public DummySectionFragment() {
  207. }
  208.  
  209. @Override
  210. public void onActivityCreated(Bundle savedInstanceState) {
  211. super.onActivityCreated(savedInstanceState);
  212. }
  213.  
  214. @Override
  215. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  216. View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
  217. dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
  218. int position = getArguments().getInt(ARG_SECTION_NUMBER);
  219. dummyTextView.setText(Integer.toString(getArguments().getInt("categId")));
  220. return rootView;
  221. }
  222.  
  223. }
  224. public class MyTabListener implements TabListener{
  225.  
  226. public Context context;
  227.  
  228. MyTabListener(Context context) {
  229. this.context = context;
  230. }
  231.  
  232. @Override
  233. public void onTabReselected(Tab tab, FragmentTransaction ft) {
  234. }
  235.  
  236. @Override
  237. public void onTabSelected(Tab tab, FragmentTransaction ft) {
  238. // HERE
  239. }
  240.  
  241. @Override
  242. public void onTabUnselected(Tab tab, FragmentTransaction ft) {
  243.  
  244. }
  245. }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement