Calling a Fragment from a main fragment - android

I have to call a fragment (Page1) from here...
protected void onPostExecute(Void args) {
Intent intentx;
intentx = new Intent(HomeFragment.this.getActivity(),Main.class);
intentx.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentx);
}
I my program i have Main class which looks like this
public class Main extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Express(1 Day)", "Premium (2 Day)", "Normal (3+ Days)" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_tab);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
//actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
This class will invoke Page1,Page2,Page3
Inside Page 1 the code is this
public class Page1 extends Fragment {
public static ArrayList<String> strArr;
public static ArrayList<ArrayList<String>> content;
public static ArrayAdapter<String> adapter;
LinearLayout ll;
View format;
float days_to_del;
int[] no_of_count= new int[20];
int count;
int x;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.page1, container, false);
// super.onResume();
//View rootView = inflater.inflate(R.layout.page1, container, false);
for(int j=0;j<10;j++)
{
days_to_del=Float.parseFloat(""+Lib.result[j][0].toString());
if(days_to_del==2.0)
{
no_of_count[x]=j;
x++;
count++;
}
}
for(int m=0;m<count;m++)
{
ll = (LinearLayout) rootView.findViewById(R.id.parent);
LayoutInflater vi = (LayoutInflater) Page1.this.getActivity().getApplicationContext().getSystemService(Page1.this.getActivity().LAYOUT_INFLATER_SERVICE);
format = inflater.inflate(R.layout.page1, null);
ll.addView(format);
//Lib.ShowAlertMessage(Page1.this.getActivity().getApplicationContext(), "", ""+format.getId());
if(Lib.result[no_of_count[m]][2].toString().equals("First Flight Couriers"))
{
ImageView imageView1=(ImageView)rootView.findViewById(R.id.imageView1);
String uri = "#drawable/first_flight";
int imageResource = getResources().getIdentifier(uri, null, Page1.this.getActivity().getApplicationContext().getPackageName());
Drawable res = getResources().getDrawable(imageResource);
imageView1.setImageDrawable(res);
}
else if(Lib.result[no_of_count[m]][2].toString().equals("SkyNet WorldWide Express"))
{
ImageView imageView1=(ImageView)rootView.findViewById(R.id.imageView1);
String uri = "#drawable/skynet";
int imageResource = getResources().getIdentifier(uri, null, Page1.this.getActivity().getApplicationContext().getPackageName());
Drawable res = getResources().getDrawable(imageResource);
imageView1.setImageDrawable(res);
}
else if(Lib.result[no_of_count[m]][2].toString().equals("The Professional Couriers"))
{
ImageView imageView1=(ImageView)rootView.findViewById(R.id.imageView1);
String uri = "#drawable/professional";
int imageResource = getResources().getIdentifier(uri, null, Page1.this.getActivity().getApplicationContext().getPackageName());
Drawable res = getResources().getDrawable(imageResource);
imageView1.setImageDrawable(res);
}
else if(Lib.result[no_of_count[m]][2].toString().equals("The Professional Couriers"))
{
ImageView imageView1=(ImageView)rootView.findViewById(R.id.imageView1);
String uri = "#drawable/professional";
int imageResource = getResources().getIdentifier(uri, null, Page1.this.getActivity().getApplicationContext().getPackageName());
Drawable res = getResources().getDrawable(imageResource);
imageView1.setImageDrawable(res);
}
TextView textView2=(TextView)rootView.findViewById(R.id.textView2);
textView2.setText("Rupees");
}
if(format.getId()==1)
{
ll.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intentx;
intentx = new Intent(Page1.this.getActivity(), From.class); //mContext is a Context variable.
intentx.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
});
}
if(format.getId()==2)
{
ll.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intentx;
intentx = new Intent(Page1.this.getActivity(), To.class);
intentx.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
});
}
return rootView;
}
public void onResume(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
}
}
My Issue
1. I use these variables(Lib.results[][]) in the PostExecute They are available ( I dont require here)
2. In the fragment Page1 i am not able access the Lib.results[][] , as soon as it enters the Page1 the value of result[][] is null as the value is not updated(this is what I guessed would have hapened)
3. The results based on the values will go to respective fragments (Page1,Page2,Page3)
Question
Am i calling the fragment activity the rightway?? does intent work this way or is there any other method.

I figured that I had used the array as String but changing it to strArray made it work

Related

Reusing ListFragment with ViewPager

I have a 7 tabs and their shared fragment whose data depends on XML received from url. The problem is if I set setOffscreenPageLimit(6) and for first tab, everything is fine for tab number 1 but tab number 2 to 7 shows the data from url that supposed to show in last tab.
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager(), this, negeri);
viewPager.setAdapter(mAdapter);
viewPager.setOffscreenPageLimit(6);
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle("List");
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
FragmentStatePagerAdapter;
public class TabsPagerAdapter extends FragmentStatePagerAdapter {
static Context context;
static Resources res = null;
static String[] CONTENT = null;
public TabsPagerAdapter(FragmentManager fm, Context c, String negeri) {
super(fm);
context = c;
res = context.getResources();
CONTENT = res.getStringArray(R.array.values);
}
#Override
public ListFragment getItem(int index) {
return DaerahFragment.newInstance(CONTENT[index]);
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return CONTENT.length;
}
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
ListFragment;
public static ListFragment newInstance(String daerahVal) {
// TODO Auto-generated method stub
Bundle args = new Bundle();
args.putString(ARG_DAERAH, daerahVal);
DaerahFragment fragment = new DaerahFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Bundle args = getArguments();
//String daerah=args.getString("daerah");
daerah = getArguments().getString(ARG_DAERAH);
Toast.makeText(getActivity(), "daerah= " + daerah , Toast.LENGTH_SHORT).show();
URL = "http://www.url.com/android/daerahMarker.php?daerah="+daerah;
setRetainInstance(true);
if (mListViewScrollPos != null && adapter != null) {
getListView().onRestoreInstanceState(mListViewScrollPos);
} else {
myTask = new TalkToServer();
myTask.execute();
}
}
TQ
i solved this
just remove static
static String URL = "";
to
String URL = "";
:)

how to display toast messages for each tab in swipe tab in android?

when i run this code,the toast in two different fragments are displayed on one tab. when i swipe to next tab nothing is displayed.
this is is my main tab activity:
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Offers", "Distance", "Happy Hours","Shop List" };
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
this is adapter class :
public class TabsPagerAdapter extends FragmentStatePagerAdapter {
Bundle bundle = new Bundle();
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
Fragment of = new OfferFragment();
bundle.putString("OfferFragment", "OfferFragment");
of.setArguments(bundle);
return of;
case 1:
Fragment df = new DistanceFragment();
bundle.putString("DistanceFragment", "DistanceFragment");
df.setArguments(bundle);
return df;
case 2:
Fragment hf = new HappyHoursFragment();
bundle.putString("HappyHoursFragment", "HappyHoursFragment");
hf.setArguments(bundle);
return hf;
case 3:
Fragment sf = new ShoplistFragment();
bundle.putString("ShoplistFragment", "ShoplistFragment");
sf.setArguments(bundle);
return sf;
}
return null;
}
#Override
public int getCount() {
return 4;
}
}
this is my first fragment :
public class OfferFragment extends Fragment {
private String name;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// public static final String ARG_OBJECT = "object";
View rootView = inflater.inflate(R.layout.offerfragment, container,
false);
Bundle args = getArguments();
TextView txtview = (TextView) rootView.findViewById(R.id.offer);
name = args.getString("OfferFragment");
txtview.setText(args.getString("OfferFragment"));
display();
return rootView;
}
private void display() {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), name, Toast.LENGTH_SHORT).show();
}
}
this is my second fragment :
public class DistanceFragment extends Fragment {
private String name;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.distancefragment, container, false);
Bundle args = getArguments();
TextView txtview = (TextView) rootView.findViewById(R.id.distance);
name = args.getString("DistanceFragment");
txtview.setText(args.getString("DistanceFragment"));
display();
return rootView;
}
private void display() {
// TODO Auto-generated method stub
Toast.makeText(getActivity(), name, Toast.LENGTH_SHORT).show();
}
}
any suggestions are appreciated. am stuck with this.
This is because next fragment is created by pager
before showing toast check if that fragment is visible or not
if(fragmentName.this.isVisible())
{
// do your stuff here
}
Write onResume() in every fragment class.
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
//Display Toast message here
}
}

How to make a slider in android like website?

I want to design the android application with slider(like website) when the user swipes from left to right or right to left I want to change the background not the whole view. I need a design like below. Please help me with native code samples
see this Library its may helps you
https://github.com/JakeWharton/Android-ViewPagerIndicator/
You can use View pager.
public class ImageDownloader extends Fragment {
public ImageDownloader() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mView = inflater.inflate(R.layout.frag_image, null);
Bundle bundle = getArguments();
String imageUrl = bundle.getString("imageUrl");
ImageView mView2 = (ImageView) mView.findViewById(R.id.image);
ImageLoader mImageLoader = new ImageLoader(getActivity());
mImageLoader.DisplayImage(imageUrl, mView2);
return mView;
}
}
public class FragmentAdapter extends FragmentStatePagerAdapter {
public FragmentAdapter(FragmentManager fm) {
super(fm);
}
#Override
public CharSequence getPageTitle(int position) {
return mdataStrings[position];
}
#Override
public Fragment getItem(int position) {
Bundle bundle = new Bundle();
bundle.putString("imageUrl", mdataStrings[position]);
ImageDownloader mDownloader = new ImageDownloader();
mDownloader.setArguments(bundle);
return mDownloader;
}
#Override
public int getCount() {
return mdataStrings.length;
}
}
And Use Like this :
mdataStrings = GlobalVars.stories.get(pos)
.getUpload_images().split(",");
mViewPager = (ViewPager) findViewById(R.id.viewpager);
FragmentAdapter titleAdapter = new FragmentAdapter(
getSupportFragmentManager());
mViewPager.setAdapter(titleAdapter);
for (int i = 0; i < mdataStrings.length; i++) {
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.indicator);
ImageView mImageView = new ImageView(
StoriesDetails.this);
mImageView.setTag(i + "");
if (i == 0) {
mImageView
.setBackgroundResource(R.drawable.black_circle);
} else {
mImageView
.setBackgroundResource(R.drawable.white_circle);
}
mLinearLayout.addView(mImageView);
}
mViewPager
.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int page) {
// TODO Auto-generated method stub
System.out.println("Page : " + page);
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.indicator);
mLinearLayout.getChildAt(page)
.setBackgroundResource(
R.drawable.black_circle);
for (int i = 0; i < mdataStrings.length; i++) {
if (page != i) {
mLinearLayout
.getChildAt(i)
.setBackgroundResource(
R.drawable.white_circle);
}
}
}
#Override
public void onPageScrolled(int arg0,
float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int page) {
// TODO Auto-generated method stub
}
});

Android - Pass the parameters between fragments

I developed an android application, as you see in this picture,
I used sherlock action bar for searching, the only problem is that when the user search something in web tab if he/she wants to see the image result, he should type and search again in image tab, is there any way to pass the edittext parameter to other tabs and do searching when user changed the tabs?
MainActivity:
public class MainActivity extends SherlockFragmentActivity implements
TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private com.actionbarsherlock.app.ActionBar actionBar;
private String[] tabs = { "Sound", "Image", "Web" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
actionBar.setHomeButtonEnabled(false);
viewPager.setAdapter(mAdapter);
viewPager.setOffscreenPageLimit(3);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText(tabs[0])
.setTabListener(this), false);
actionBar.addTab(actionBar.newTab().setText(tabs[1])
.setTabListener(this), false);
actionBar.addTab(actionBar.newTab().setText(tabs[2])
.setTabListener(this), true);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabSelected(com.actionbarsherlock.app.ActionBar.Tab tab,
android.support.v4.app.FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(com.actionbarsherlock.app.ActionBar.Tab tab,
android.support.v4.app.FragmentTransaction ft) {
}
#Override
public void onTabReselected(com.actionbarsherlock.app.ActionBar.Tab tab,
android.support.v4.app.FragmentTransaction ft) {
}
}
Adapter:
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new Sound();
case 1:
return new Image();
case 2:
return new Search();
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
Sound Frgment:
public class Sound extends Fragment {
private String URL_ADDRESS, Servlet;
private ListView listview;
private ListViewAdapter adapter;
private List<Spanned> numberlist = new ArrayList<Spanned>();
private List<Ava2> avaList = new ArrayList<Ava2>();
private Boolean isInternetPresent = false;
private ConnectionDetector detector;
private View rootView;
private RelativeLayout loading, layout;
private int counter = 10;
private EditText editText;
private String a = "http://***";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.ava, container, false);
ImageButton voiceSearch = (ImageButton) rootView
.findViewById(R.id.imageButton2);
ImageButton buttonSearch = (ImageButton) rootView
.findViewById(R.id.imageButton1);
listview = (ListView) rootView.findViewById(R.id.listview);
loading = (RelativeLayout) rootView.findViewById(R.id.loading);
layout = (RelativeLayout) rootView.findViewById(R.id.layout);
editText = (EditText) rootView.findViewById(R.id.editText1);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
search();
return true;
}
return false;
}
});
buttonSearch.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
search();
}
});
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long l) {
try {
Intent i = new Intent(getActivity(), Play.class);
startActivity(i);
} catch (Exception e) {
}
}
}
});
return rootView;
}
First,you could write a interface:
public interface OnProfileDataPass {
public String getCurrentSearchword();
public void setCurrentSearchword(String searchword);
}
Then in your fragments:
public class WebFragment extends Fragment {
private OnProfileDataPass dataPasser;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
dataPasser = (OnProfileDataPass) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnProfileDataPass");
}
searchword = dataPasser.getCurrentSearchword();
}
}
In your activity:
public class MainActivity implements OnProfileDataPass {
String currentSearchword;
#Override
public String getCurrentSearchword() {
return currentSearchword;
}
#Override
public String setCurrentSearchword(String searchword) {
this.currentSearchword = searchword;
}
}
That's my solution.When you change the tap,you could listen the proper event and set the word with getActivity.setCurrentSearchword(String newword),then you could get the newword in the fragment's dataPasser variable's method getCurrentSearchword() to get the search word. I have test it in my app. Wish that helps you a little.
you should save the value of the editText in your activity and then pass it as an argument when you call your fragment, using the same technique as the one described here: http://developer.android.com/reference/android/app/Fragment.html

Handle threads inside fragments

Currently I have made 3 tabs. On each tab (fragment) there is a thread to fetch content and display it with a listview through an ArrayAdapter.
Now when you scroll through the tabs the thread gets executed again and again (not necessary).
Is there a way to stop this ? Or do I need to write the content to external storage for faster access ?
Also sometimes I see the same data on different tabs , how to avoid this ?
Thanks in advance
This is some code that I use inside my fragment which that is called multiple times when swiping :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView;
rootView = inflater.inflate(R.layout.leadlist, container, false);
profielen = new ArrayList<Profile>();
new getLeaderboards().execute(url);
lv = (ListView) rootView.findViewById(android.R.id.list);
lbadapter = new LeaderBoardAdapter(this.getActivity(), R.layout.item_layout, profielen);
return rootView;
}
public static class getLeaderboards extends AsyncTask<String, Void, JsonObject> {
#Override
protected JsonObject doInBackground(String... urls) {
JsonObject x = parseURL.GetJSON(urls[0]);
return x;
}
#Override
protected void onPostExecute(JsonObject obj) {
Gson gson = new Gson();
System.out.println(obj.toString());
JsonArray arr = obj.get("rows").getAsJsonArray();
for (int i=0;i<10;i++) {
Profile pat = gson.fromJson( arr.get(i) , Profile.class);
profielen.add(pat);
}
lv.setAdapter(lbadapter);
}
}
My FragmentActivity :
public class LeaderboardsActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private String[] tabs = { "Tab1", "Tab2", "Tab3" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.leaderboard);
Intent i = getIntent();
String region = i.getStringExtra("region");
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setDisplayShowHomeEnabled(false);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager(),region);
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}

Categories

Resources