tabs in viewpager not responding in a sequence android - android

In my android app I am using a tab swipe view having 4 tabs first tab displays data on the UI and is calling a WS via AsyncTask, data returned by the WS is populating a pojo and this pojo is passed to other tab(fragments)
The problem I face is Tabs are not executed in a sequence when I click on first tab, first and 2 tab are executing before calling WS due to this strange behaviour I cant get data in my second tab
Activity:
public class TabSwipeActivity extends FragmentActivity implements ActionBar.TabListener, DetailsFoeFragment.ExistingFeatureDataInt
{
ActionBar actionbar;
ViewPager viewpager;
FragmentPageAdapter ft;
ExistingData existingData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabswipe);
ArrayList<String> detailList =this.getIntent().getStringArrayListExtra("FeatureData");
viewpager = (ViewPager) findViewById(R.id.pager1);
ft = new FragmentPageAdapter(getSupportFragmentManager());
if(detailList!=null)
ft.setDetailList(detailList);
// ft.setExistingData(getExistingData());
actionbar = getActionBar();
viewpager.setAdapter(ft);
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
actionbar.addTab(actionbar.newTab().setText("Details").setTabListener(this),0,true);
actionbar.addTab(actionbar.newTab().setText("Action Taken").setTabListener(this),1,false);
actionbar.addTab(actionbar.newTab().setText("Before Photos").setTabListener(this),2,false);
actionbar.addTab(actionbar.newTab().setText("After Photos").setTabListener(this),3,false);
viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
public void onPageSelected(int arg0) {
actionbar.setSelectedNavigationItem(arg0);
}
});
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewpager.setCurrentItem(tab.getPosition());
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
////
Adapter:
/////
public class FragmentPageAdapter extends FragmentPagerAdapter {
Bundle bundle= new Bundle();
ExistingData existingData = new ExistingData();;
public FragmentPageAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
Fragment fragment;
#Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
switch (arg0) {
case 0:
fragment = new DetailsFoeFragment();
bundle.putStringArrayList("details",detailList);
fragment.setArguments(this.bundle);
return fragment;
case 1:
fragment= new ActionFoeFragment();
bundle.putParcelableArrayList("actions",(ArrayList)existingData.getActionList());
// bundle.putParcelableArrayList("actions",(ArrayList)existingData.getActionList());
// bundle.putString("featureId",detailList.get(2));
fragment.setArguments(this.bundle);
return fragment;
case 2:
fragment = new PhotoBeforeFoeFragment();
// bundle.putStringArrayList("beforeImage",null);
bundle.putStringArrayList("beforeImage",(ArrayList<String>)existingData.getBeforeImagePath());
fragment.setArguments(this.bundle);
return fragment;
case 3:
fragment = new PhotoAfterFoeFragment();
bundle.putStringArrayList("afterImage",(ArrayList<String>)existingData.getAfterImagePath());
fragment.setArguments(this.bundle);
return fragment;
default:
break;
}
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}
ArrayList<String> detailList;
public void setDetailList(ArrayList<String> detailList) {
this.detailList = detailList;
}
public void setExistingData(ExistingData existingData) {
this.existingData = existingData;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
Please provide inputs
thanks

One solution:
In your activity, run WS with Asynctask, and then call the fragments that need data to update view
class yourActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
... ...
//set up your tabs with fragments
... ...
//WS: Your asynctask requesting data from server
(new AsyncTask<... ...>() {
... ...
#Override
protected void onPostExecute(dataSet) {
//here you have got your data set from server then inform tabs to update
YourFragment frag = (YourFragment)getSupportFragmentManager().findFragmentById(R.id.your_fragment);
frag.updateView(dataSet);
}
}).execute();
}
}
In each tab/fragment that needs shared data set, define a method to update view with the data set
class YourFragment extebds Fragment{
... ...
public void updateView(dataSet)
{
//update this fragment view with dataSet
//and stop your waiting dialog
}
}
Hope this solution works for you.

Related

how can I change the color of the tabBar in tab fragment (Viewpager)

I tried many times but it's not OK.
I try to change actionBar in MainActivity but the TabBar doesn't change, so I don't no how to change it.
This is my code :
MainActivivty
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private ActionBar actionBar;
private TabsPagerAdapter mAdapter;
private String[] tabs = { "Bài Hát", "Yêu Thích"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar ac = getActionBar();
ac.setBackgroundDrawable(new ColorDrawable(Color.rgb(72, 209, 204)));
//ac.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#E64260")));
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
if (position == 1) {
TabYeuThich frag = (TabYeuThich) mAdapter
.getFragmentTabYeuThich(1);
frag.resetPage();
} else {
TabBaiHat frag = (TabBaiHat) mAdapter
.getFragmentTabBaiHat(0);
frag.resetPage();
}
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
This is class : TabsPagerAdapter
public class TabsPagerAdapter extends FragmentPagerAdapter {
FragmentManager fm;
TabYeuThich fragYeuThich;
TabBaiHat fragBaiHat;
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
this.fm = fm;
}
#Override
public Fragment getItem(int index) {
// TODO Auto-generated method stub
switch (index) {
case 0:
fragBaiHat = new TabBaiHat();
return fragBaiHat;
case 1:
fragYeuThich = new TabYeuThich();
return fragYeuThich;
}
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
public Fragment getFragmentTabYeuThich(int post) {
return fragYeuThich;
}
public Fragment getFragmentTabBaiHat(int post) {
return fragBaiHat;
}
Please help me ! Thanks all !
Useally I set up the action bar from themes.xml
I recommend you follow up the both tutorials
developer android
action bar explanation

Android - Swiping without Tabs

NoteActivity Code:
public class NoteActivity extends FragmentActivity implements ActionBar.TabListener
{
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = {"Note", "Note Info"};
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note);
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));
}
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(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
TabsPageAdapter.Java
public class TabsPagerAdapter extends FragmentPagerAdapter
{
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index)
{
case 0:
return new NoteFragment();
case 1:
return new NoteInfoFragment();
}
return null;
}
#Override
public int getCount()
{
// get item count - equal to number of tabs
return 2;
}
}
So basically, I used an example of Tabs View and got the Tabs working and it looks like this:
What would I have to do to make it just swipeable without the Tabs showing. An example is like Snapchat. It definitely uses the Swipe view control but the tabs are hidden. Can someone please show me how to get this done?
Since you already have a ViewPager in your code, all you have to do is remove the code that creates the ActionBar tabs (under the comment // Adding tabs), as well as the code that synchronizes the tab selection with the current page (start with the ViewPager.OnPageChangeListener and the ActionBar.TabListener callbacks and see if anything breaks).

Play with fragments in onTabSelected

Dears,
I searched for this issue for more than a day but with no luck.
I implement exactly the code posted here:
Adding Navigation Tabs
My code for onTabSelected look like:
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Check if the fragment is already initialized
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(R.id.alert_fragment_container, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
// prepare adapter for ExpandableListView
Log.i("After Adapter Created", "Passed");
final ExpandableListAdapter expListAdapter = new AlertsAdapter(
mActivity, myAlerts, violations);
Log.i("After Adapter Initialized", "Passed");
((MyCustomFragment)mFragment).violations.setAdapter(expListAdapter);
}
The code is working fine till last line, where I need to set the adapter for public static list initialized in MyCustomFragment in onCreateView, here my code for fragment:
public class MyCustomFragment extends Fragment {
public MyCustomFragment() {
}
public static ExpandableListView violations;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_alerts_poi, container, false);
violations = (ExpandableListView) rootView.findViewById(R.id.POIAlertList);
Log.i("onCreateView POI", "Called");
return rootView;
}
}
It give Null pointer error. With my debugging logs, I notice that this log Log.i("onCreateView POI", "Called"); appears after this Log.i("After Adapter Initialized", "Passed");. This means that I'm trying to set the adapter for a fragment isn't initialized yet.
This is the exact problem I'm face, I need to fed the ExpandableListView with data based on Tab selection in onTabSelected.
What I'm doing wrong? What is the best solution?
Regards,
It seems that you need a ViewPager, I just implemented a navigation tabs few days ago, here is my code, it navigates between 4 fragments:
public class MainActivity extends FragmentActivity implements ActionBar.TabListener{
private ActionBar actionBar;
private ViewPager mViewPager;
private AppSectionsPagerAdapter mAppSectionsPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
actionBar=getActionBar();
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
actionBar.addTab(actionBar.newTab().setIcon(R.drawable.icon1).setTabListener(this));
actionBar.addTab(actionBar.newTab().setIcon(R.drawable.icon2).setTabListener(this));
actionBar.addTab(actionBar.newTab().setIcon(R.drawable.icon3).setTabListener(this));
actionBar.addTab(actionBar.newTab().setIcon(R.drawable.icon4).setTabListener(this));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.action_menu, menu);
return true;
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
And here is the adapter:
public class AppSectionsPagerAdapter extends FragmentPagerAdapter {
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return new Fragment1();
case 1:
return new Fragment2();
case 2:
return new Fragment3();
case 3:
return new Fragment4();
}
return null;
}
#Override
public int getCount() {
return 4;
}
}
Have a look # this Tablayout.onTabselected for latest API updates. ActionBar.TabListener is a old implementation.

Unable to start activity component info (nullpointer)

im trying to make a basic ui in android with tabs. however i keep getting a nullpointer exception whenever i try to run it. the error originates from initializing viewpager. (i dont know what else to type and i keep receiving an error whenever i try to post.)
public class MainActivity extends FragmentActivity implements TabListener {
ActionBar actionBar;
ViewPager viewPager;
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.activity_main);
viewPager=(ViewPager) viewPager.findViewById(R.id.tabs);
viewPager.setAdapter(new adapter(getSupportFragmentManager()));
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab stocks = actionBar.newTab();
stocks.setText("Stocks");
stocks.setTabListener(this);
ActionBar.Tab market = actionBar.newTab();
market.setText("Market");
market.setTabListener(this);
ActionBar.Tab portfolio = actionBar.newTab();
portfolio.setText("Portfolio");
portfolio.setTabListener(this);
actionBar.addTab(stocks);
actionBar.addTab(market);
actionBar.addTab(portfolio);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mainsearch, menu);
return true;
}
#Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
class adapter extends FragmentPagerAdapter {
public adapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
public Fragment getItem(int arg0) {
Fragment fragment = null;
if(arg0==0) {
fragment = new StocksFragment();
}
if (arg0==1){
fragment = new MarketFragment();
}
if (arg0==2){
fragment = new PortfolioFragment();
}
return fragment;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
}
Of course this line will give a NPE
viewPager=(ViewPager) viewPager.findViewById(R.id.tabs);
you are calling a method on an object that you are trying to initialize (calling findViewById() on null viewPager)
If tabs is in activity_main.xml then just remove viewPager
viewPager=(ViewPager) findViewById(R.id.tabs);
findViewById is a method of the activity, not of the view. So it should be:
viewPager=(ViewPager) this.findViewById(R.id.tabs);

Showing progress bar in splashscreen while data is getting downloaded from the internet

I need to know how to parse data from online xml in the background and show it in listview.
Presently I have a class which is extending FragmentActivity. Below is the layout of the class
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
I have declared Action bar tabs and view pager in the class like below
actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.setDisplayShowTitleEnabled(false);
actionbar.setDisplayUseLogoEnabled(true);
viewPager=(ViewPager)findViewById(R.id.pager);
viewPagerAdapter=new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(viewPagerAdapter);
viewPager.setOffscreenPageLimit(6);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
getActionBar().setSelectedNavigationItem(position);
}
#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
}
});
ActionBar.Tab TabOne = actionbar.newTab().setText("TabOne ");
ActionBar.Tab TabTwo = actionbar.newTab().setText("TabTwo ");
ActionBar.Tab TabThree = actionbar.newTab().setText("TabThree ");
ActionBar.Tab TabFour = actionbar.newTab().setText("TabFour ");
ActionBar.Tab TabFive = actionbar.newTab().setText("TabFive ");
ActionBar.Tab TabSix = actionbar.newTab().setText("TabSix ");
TabOneActivity one = new TabOneActivity ();
TabTwoActivity two= new TabTwoActivity ();
TabThreeActivity three= new TabThreeActivity ();
TabFourActivity four= new TabFourActivity ();
TabOFiveActivity five= new TabOFiveActivity ();
TabSixActivity six=new TabSixActivity ();
TabOne .setTabListener(new MyTabsListener(one));
TabTwo .setTabListener(new MyTabsListener(two));
TabThree .setTabListener(new MyTabsListener(three));
TabFour .setTabListener(new MyTabsListener(four));
TabFive .setTabListener(new MyTabsListener(five));
TabSix .setTabListener(new MyTabsListener(six));
actionbar.addTab(TabOne);
actionbar.addTab(TabTwo);
actionbar.addTab(TabThree);
actionbar.addTab(TabFour);
actionbar.addTab(TabFive);
actionbar.addTab(TabSix);
There are two inner classes MyTabListener and ViewPageAdapter . Below is the MyTabListener class
public class MyTabsListener implements ActionBar.TabListener {
protected TabOneActivity oneContext;
protected TabTwoActivity twoContext;
protected TabThreeActivity threeContext;
protected TabFourActivity fourContext;
protected TabFiveActivity fiveContext;
protected TabSixActivity sixcontext;
public MyTabsListener(TabOneActivity onetab) {
// TODO Auto-generated constructor stub
this.oneContext=onetab;
}
public MyTabsListener(TabTwoActivity twoTab) {
// TODO Auto-generated constructor stub
this.twoContext=twoTab;
}
public MyTabsListener(TabThreeActivity threeTab) {
// TODO Auto-generated constructor stub
this.threeContext=threeTab;
}
public MyTabsListener(TabFourActivity fourTab) {
this.fourContext= fourTab;
}
public MyTabsListener(TabFiveActivity fiveTab) {
// TODO Auto-generated constructor stub
this.fiveContext=fiveTab;
}
public MyTabsListener(TabSixActivity sixTab) {
// TODO Auto-generated constructor stub
this.sixContext=sixTab;
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
Below is the ViewPagerAdapter class
public class ViewPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 6;
public ViewPagerAdapter(FragmentManager supportFragmentManager) {
// TODO Auto-generated constructor stub
super(supportFragmentManager);
}
#Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
switch(position){
case 0:
return TabOneActivity.create(position);
case 1:
return TabTwoActivity.create(position);
case 2:
return TabThreeActivity.create(position);
case 3:
return TabFourActivity.create(position);
case 4:
return TabFiveActivity.create(position);
case 5:
return TabSixActivity.create(position);
default :
return TabOneActivity.create(position);
}
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return PAGE_COUNT;
}
}
Each Activity is extending fragment and hitting and parsing online xml using AsyncTask and showing the result in a listview.
Before data gets populated to the listview I am showing a progressbar.
What I need to do is to show a progress bar on the splash screen which in the background should hit and parses the online data and one post execute show the data in a listview.
This will make UI look good, since user don't need to wait for data to get load in the main screen instead it is getting downlaoded in the spalsh screen.
Kindly suggest how to achieve this. Since I am using a FragmentActivity which has six different fragments hitting six different URLs

Categories

Resources