Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends FragmentActivity {
- /**
- * The {@link android.support.v4.view.PagerAdapter} that will provide
- * fragments for each of the sections. We use a
- * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
- * will keep every loaded fragment in memory. If this becomes too memory
- * intensive, it may be best to switch to a
- * {@link android.support.v4.app.FragmentStatePagerAdapter}.
- */
- SectionsPagerAdapter mSectionsPagerAdapter;
- /**
- * The {@link ViewPager} that will host the section contents.
- */
- ViewPager mViewPager;
- TextView tw;
- ActionBar myActionBar;
- ArrayList<Category> ListOfCategory;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- myActionBar = getActionBar();
- myActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
- ListOfCategory = new ArrayList<Category>();
- try {
- List<String> categList = new ExecuteJsonLinkTask().execute("http://www.76actu.fr/json.php?d=categories&a=list&key=8smA5YjLG1132zbz301tM94jZO30B7dW").get();
- for (String item : categList) {
- JSONArray jArray = new JSONArray(item);
- for(int i=0;i<jArray.length();i++){
- JSONObject json_data = jArray.getJSONObject(i);
- Category categ = new Category();
- categ.setCategIdSite(json_data.getInt("term_id"));
- categ.setName(json_data.getString("name"));
- categ.setMoreArticle(true);
- categ.setArticleAvailable(false);
- if(json_data.getInt("term_id") == 89871){
- categ.setActive(false);
- } else categ.setActive(true);
- Tab tab = myActionBar.newTab();
- tab.setText(json_data.getString("name"));
- Object obj = new Object();
- obj = String.valueOf(json_data.getInt("term_id"));
- tab.setTag(obj);
- tab.setTabListener(new MyTabListener(getBaseContext()));
- // tab.setTabListener(new MyTabListener(getBaseContext()));
- myActionBar.addTab(tab);
- ListOfCategory.add(categ);
- }
- }
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ExecutionException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- // Create the adapter that will return a fragment for each of the three
- // primary sections of the app.
- mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
- // Set up the ViewPager with the sections adapter.
- mViewPager = (ViewPager) findViewById(R.id.pager);
- mViewPager.setAdapter(mSectionsPagerAdapter);
- mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
- @Override
- public void onPageSelected(int arg0) {
- myActionBar.setSelectedNavigationItem(arg0);
- }
- @Override
- public void onPageScrolled(int arg0, float arg1, int arg2) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onPageScrollStateChanged(int arg0) {
- // TODO Auto-generated method stub
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- /**
- * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
- * one of the sections/tabs/pages.
- */
- public class SectionsPagerAdapter extends FragmentPagerAdapter {
- private ArrayList<Category> ListOfCategory = new ArrayList<Category>();
- Fragment fragment;
- ActionBar myActionBar;
- public SectionsPagerAdapter(FragmentManager fm){
- super(fm);
- myActionBar = getActionBar();
- try {
- List<String> categList = new ExecuteJsonLinkTask().execute("[API_LINK]").get();
- for (String item : categList) {
- JSONArray jArray = new JSONArray(item);
- for(int i=0;i<jArray.length();i++){
- JSONObject json_data = jArray.getJSONObject(i);
- Category categ = new Category();
- categ.setCategIdSite(json_data.getInt("term_id"));
- categ.setName(json_data.getString("name"));
- categ.setMoreArticle(true);
- categ.setArticleAvailable(false);
- if(json_data.getInt("term_id") == 89871){
- categ.setActive(false);
- } else categ.setActive(true);
- ListOfCategory.add(categ);
- }
- }
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ExecutionException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- @Override
- public Fragment getItem(int position) {
- // getItem is called to instantiate the fragment for the given page.
- // Return a DummySectionFragment (defined as a static inner class
- // below) with the page number as its lone argument.
- fragment = new DummySectionFragment();
- Bundle args = new Bundle();
- args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position);
- args.putInt("categId", ListOfCategory.get(position).getCategIdSite());
- fragment.setArguments(args);
- return fragment;
- }
- @Override
- public int getCount() {
- // Show total pages.
- return ListOfCategory.size();
- }
- @Override
- public CharSequence getPageTitle(int position) {
- return ListOfCategory.get(position).getName();
- }
- }
- /**
- * A dummy fragment representing a section of the app, but that simply
- * displays dummy text.
- */
- public static class DummySectionFragment extends Fragment {
- /**
- * The fragment argument representing the section number for this
- * fragment.
- */
- TextView dummyTextView;
- public static final String ARG_SECTION_NUMBER = "section_number";
- public DummySectionFragment() {
- }
- @Override
- public void onActivityCreated(Bundle savedInstanceState) {
- super.onActivityCreated(savedInstanceState);
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
- dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
- int position = getArguments().getInt(ARG_SECTION_NUMBER);
- dummyTextView.setText(Integer.toString(getArguments().getInt("categId")));
- return rootView;
- }
- }
- public class MyTabListener implements TabListener{
- public Context context;
- MyTabListener(Context context) {
- this.context = context;
- }
- @Override
- public void onTabReselected(Tab tab, FragmentTransaction ft) {
- }
- @Override
- public void onTabSelected(Tab tab, FragmentTransaction ft) {
- // HERE
- }
- @Override
- public void onTabUnselected(Tab tab, FragmentTransaction ft) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement