Android ViewPager Swipe Tabs – Fragments With ListViews

Read-only properties in JavaFX 8

Android ViewPager Swipe Tabs – Fragments With ListViews







Hello guys today we discuss how to create Fragments with ListViews and Swipeable Tabs.The listviews shall be simple and have some data.We shall be able to swipe from left and right and vice versa,with help of ViewPager. Cheers.

SECTION 1 : Our MainActivity

package com.tutorials.viewpagertabs;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.SimpleOnPageChangeListener;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
  ActionBar ab;
  ViewPager vp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      //ACTIONBAR
        ab=getActionBar();
        ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ab.setDisplayShowTitleEnabled(true);
        vp=(ViewPager) findViewById(R.id.pager);
        FragmentManager fm=getSupportFragmentManager();
        //CREATE LISTENER
       ViewPager.SimpleOnPageChangeListener listener=new SimpleOnPageChangeListener()
       {
    	   @Override
    	public void onPageSelected(int position) {
    		// TODO Auto-generated method stub
    		super.onPageSelected(position);
    		ab.setSelectedNavigationItem(position);
    	}
       };
     //ASSIGN LISTENET
       vp.setOnPageChangeListener(listener);
     //ADAPTER
       MyAdapter adapter=new MyAdapter(fm);
       vp.setAdapter(adapter);
     //TAB LSITENER
       ActionBar.TabListener tabListener=new TabListener() {
    @Override
    public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
      // TODO Auto-generated method stub
    }
    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
      // TODO Auto-generated method stub
      vp.setCurrentItem(tab.getPosition());
    }
    @Override
    public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
      // TODO Auto-generated method stub
    }
  };
  //CRETAE TABS AND SET LISTENER
  Tab t=ab.newTab().setText("ASIA").setIcon(R.drawable.alert).setTabListener(tabListener);
  ab.addTab(t);
  t=ab.newTab().setText("AFRICA").setIcon(R.drawable.chart).setTabListener(tabListener);
  ab.addTab(t);
  t=ab.newTab().setText("EUROPE").setIcon(R.drawable.interact).setTabListener(tabListener);
  ab.addTab(t);
    }
}

 

SECTION 2 : Our FragmentPagerAdapter class

package com.tutorials.viewpagertabs;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyAdapter extends FragmentPagerAdapter {
  final int pageCount=3;
  Bundle info;
  public MyAdapter(FragmentManager fm) {
    super(fm);
    // TODO Auto-generated constructor stub
  }
  @Override
  public Fragment getItem(int pos) {
    // TODO Auto-generated method stub
    info=new Bundle();
    //CHECK SELECTED TAB
    switch (pos) {
    case 0:
      Asia asia=new Asia();
      info.putInt("currentPage", pos++);
      asia.setArguments(info);
      return asia;
    case 1:
      Africa africa=new Africa();
      info.putInt("currentPage", pos++);
      africa.setArguments(info);
      return africa;
    case 2:
      Europe eu=new Europe();
      info.putInt("currentPage", pos++);
      eu.setArguments(info);
      return eu;
    }
    return null;
  }
  @Override
  public int getCount() {
    // TODO Auto-generated method stub
    return pageCount;
  }
}

 

SECTION 3 : Our Fragment Classes

package com.tutorials.viewpagertabs;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Africa extends ListFragment {
  String[] countries={"Kenya","Nigeria","Rwanda","South Africa","Egypt","Uganda"};
  ArrayAdapter<String> adapter;
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    adapter=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_multiple_choice, countries);
    setListAdapter(adapter);
    return super.onCreateView(inflater, container, savedInstanceState);
  }
  @Override
  public void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    getListView().setBackgroundColor(new Color().parseColor("#f36e4b"));
  }
}
package com.tutorials.viewpagertabs;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Europe extends ListFragment{
  String[] countries={"England","Germany","Spain","Belgium","France","Italy"};
  ArrayAdapter<String> adapter;
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    adapter=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_multiple_choice,countries);
    setListAdapter(adapter);
    return super.onCreateView(inflater, container, savedInstanceState);
  }
  @Override
  public void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    getListView().setBackgroundColor(new Color().parseColor("#0092ff"));
  }
}
package com.tutorials.viewpagertabs;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Asia extends ListFragment{
  String[] countries={"India","Pakistan","Malaysia","Bangladesh","Singapore"};
  ArrayAdapter<String> adapter;
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    adapter=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_multiple_choice, countries);
    setListAdapter(adapter);
    // TODO Auto-generated method stub
    return super.onCreateView(inflater, container, savedInstanceState);
  }
  @Override
  public void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
  }
}

GitHub Project : https://github.com/Oclemy/wizards_ViewpagerListView/tree/master/ViewPagerTabs



    Leave a Reply

    Your email address will not be published. Required fields are marked *

    19 + 16 =

    COMMENTS