I am trying to load items to the spinner inside my MainActivity
toolbar.Inside MainActivity
there is a fragment called HomeFragment
. And that fragment contains SlidingTabLayout
and once I click different tabs I need to load different data sets to the above mentioned spinner.
Here is how it looks like :
I have implemented method inside MainActivity
to add data to the spinner :
public void addItemsToSpinner(final Collection<String> collection) {
ArrayList<String> list = new ArrayList<>();
list.addAll(collection);
CustomSpinnerAdapter spinAdapter = new CustomSpinnerAdapter(getApplicationContext(), list);
subCategories_spinner.setAdapter(spinAdapter);
subCategories_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapter, View v, int position, long id) {
// On selecting a spinner item
String item = adapter.getItemAtPosition(position).toString();
// Showing selected spinner item
// Toast.makeText(getApplicationContext(), "Selected : " + item,
// Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
And inside my HomeActivity
I have set OnPageChangeLister
to SlidingTabLayout
like below :
mTabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (firstScrolled) {
// Here log-cat gives me I/Position: On page scrolled - 0 which means on stating app this line executed. But data not set to the spinner.
// Log.i("Position", "On page scrolled - " + position);
volleySubCatFilter("women");
((MainActivity) getActivity()).addItemsToSpinner(subs);//debug console subs (ArrayList) size given as 0 here
firstScrolled = false;
}
}
@Override
public void onPageSelected(int position) {
if (position == 0) {
// Log.i("Position", "On page selected - " + position);
volleySubCatFilter("women");
((MainActivity) getActivity()).addItemsToSpinner(subs);
}
if (position == 1) {
// Log.i("Position", "On page selected - " + position);
volleySubCatFilter("men");
((MainActivity) getActivity()).addItemsToSpinner(subs);
}
if (position == 2) {
// Log.i("Position", "On page selected - " + position);
volleySubCatFilter("household%20goods");
((MainActivity) getActivity()).addItemsToSpinner(subs);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
With use of volleySubCatFilter("category_here");
method I retrieve data from volley request to the server and it is working fine.
The problem I have is first time loading the application WOMEN tab is selected and but no data loaded to the spinner.But I tried with Log.i("Position", "On page scrolled - " + position);
and as soon as the app loaded it gives me I/Position: On page scrolled - 0
in my log-cat.But data doesnt load.
Debuging line ((MainActivity) getActivity()).addItemsToSpinner(subs)
tells 0 items.Once I press another tab then it starts loading data which belongs to previous tab. Example :- Pressing MEN tab right after app loaded getting data belongs to WOMEN tab.Then I click on any tab it start loading data belongs to WOMEN tab.
If I brief this problem data loading to the spinner is one step behind.
It would be grateful if anyone suggest me a way to get rid of this bug.Thanks in advance.
Please go through following steps :
public interface Updateable { public void update(); }
implement above interface in your ViewPager Fragments.
In your PagerAdapter class, override the
getItemPosition(Object object)
`public int getItemPosition(Object object) {
Now in your fragment's update() method, provide the logic for updating the spinner data in toolbar. It can be done by providing the
invalidateOptionMenu()
in update method andsetHasOptionsMenu(true)
in OncreateView of your fragment. Toolbar spinner data updation can be done by implementingonPrepareOptionsMenu(Menu menu)
override method.In you tabs page scroll logic, notify the adapter :
@Override public void onPageScrollStateChanged(int state) { yourAdapter.notifyDataSetChanged(); }
I am not sure it's a bug or feature that initially pagechangelistener is not being called in case of app start-up. It gets called once you switch the tab. I would like you to make some changes in the code:
Write the functionality you wish to perform in a method in the Activity and then call this in the onPageSelected method.
And then right after calling
in the Activity, add the following if statement
You need to provide the sequential steps as your data is being loaded in the
background thread
. When you select a page, you have two options.1. Initiate either a
Synchronous Volley call
and update the spinner adapter after this call2. Register a
BroadcastReceiver
in yourActivity
and thensendBroadcast
throughLocalBroadcastManager
onVolleyResponse
. and update the spinner items in onReceive of BroadcastReceiverHope this helps.