1

I will try to explain what I am thinking of doing as an Android app. But I am really confused how would I approach this problem.

When you get JSON from some web API (eg Location) and let's say that JSON has 5 different locations and you want to store each Location as a separate list item in a list view. This is simple, you use a location adapter class, and then those 5 items get stored as a list. For example, JSON updates 24h later and now there are 10 locations. No problems at all - Android handles this because of location adapter and etc. (I know all of this). Basically, what I am trying to tell that android does not need to know how many list items there will be before fetching information from JSON.

Now, the problem is that I am trying to create a swipe views which will represent each of the list items (1 Full view = 1 list item). For example, if there are 5 locations, i can only swipe 4 times and then I will reach the last tab. If there is update, and there are 10 locations, I could only swipe 9 times until I reach the end. I hope you understand idea.

My question is - how do I create dynamic swipe views where each of the list items would have its own separate window and to reach another list item you would swipe?

My main concern is how do you not tell android how many swipe views you will need and he would figure it out when he reads the JSON and knows the number of locations.

Many Thanks

2 Answers2

0

Let's say your data is like this:

{"India","Morocco","China","Russia"}

You can getLength of the JSON object.In this case it is 4.Save that in a static variable.Suppose max_swipes=4

Then in you swipe method

`if(position<=max_swipes || position==0){//code to swipe }
else
{
//cannot swipe last position
}`  
Javasamurai
  • 666
  • 7
  • 21
0

To implement such functionality you just simply can use viewPager. You can copy the code from here and here. These are two file and you just need to copy as it is. After adding these two files in your project you need to create an adapter and here is the thing which makes it dynamic to create swipe views.

I am adding code snippet hope it will help you.

public class ViewPagerAdapter extends FragmentStatePagerAdapter {

private DetailFragment page;
CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created


// Build a Constructor and assign the passed Values to appropriate values in the class
public ViewPagerAdapter(FragmentManager fm, CharSequence mTitles[], int mNumbOfTabsumb) {
    super(fm);
    this.Titles = mTitles;
    this.NumbOfTabs = mNumbOfTabsumb;
}

//This method return the fragment for the every position in the View Pager, This method is called only when we slide or change the tabs and not called upon rotating the screen
@Override
public Fragment getItem(int position) {
    if(position < NumbOfTabs) 
    {
        page= new DetailFragment();
        return page;
    }else {
        return null;
    }


}

// This method return the titles for the Tabs in the Tab Strip(in case you want to add title to each page.

@Override
public CharSequence getPageTitle(int position) {
    return Titles[position];
}

@Override
public int getCount() {
    return NumbOfTabs;
}

}

While creating the instance of this adapter you can pass the number of page you are going to require by calculating the number of items in JSON.

Hope this will help.

Henu
  • 1,622
  • 2
  • 22
  • 27