I try to enable swipe views in my android project.
I have create the MainActivity that enable Action Bar Tabs and the Listener :
public class MainActivity extends FragmentActivity {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager)findViewById(R.id.pager);
Tab.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position);
}
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
//Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener(){
@Override
public void onTabReselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
int categId = Integer.valueOf((String) tab.getTag());
Tab.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}};
}
}
And an Adapter :
public class TabPagerAdapter extends FragmentStatePagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int i) {
return new ArticleList();
}
@Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return POSITION_NONE;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 7; //No of Tabs
}
}
And I created my Fragment :
public class ArticleList extends Fragment {
String categ_id;
int test;
View android;
@Override
public void onCreate(Bundle savedInstanceState) {
ActionBar actbar = getActivity().getActionBar();
Tab actbartab = actbar.getSelectedTab();
test = actbartab.getPosition();
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View android = inflater
.inflate(R.layout.android_frag, container, false);
TextView tw = (TextView) android.findViewById(R.id.textView);
Log.d("test", String.valueOf(test));
tw.setText(String.valueOf(test));
return android;
}
}
The problem is that, in the LogCat, i have the good position of tabs, but in my TextView, i have not the same value...
Exactly, i have the good number in LogCat (Log.d("test")) but, in the textView, i have the latest number of Tab...
For example, when the application launch, i have 0 in the LogCat and 0 in the TextView. When I swipe or change tab to the second , I have 1 in the LogCat and 0 in my textView. If I'm on the fifth tab, and switch to the third, I have 3 in my LogCat and 5 in my TextView..
see you the problem ?
Thanks for your help.
0 comments:
Post a Comment