populate each fragment in ViewPager with different JSON object without loading again

孤者浪人 提交于 2019-12-06 06:11:07

问题


i have a ViewPager that has a Fragment which contains a GridView into it
my code works fine only when i'm scrolling page with delay and each time retrieve's content's from internet again
all i want to do is just get each page content's once and use it when user scroll's a page
one of my issues is that JSON downloding simultaneously when several pages has been scrolled and i don't know how to store my gridview content's for next usage that a page called


is there a standard way to conquer this issue ?


here is PagerAdapter for my ViewPager :

    public class ViewPagerAdapter extends FragmentStatePagerAdapter {

    String[] tabName;

    public ViewPagerAdapter(FragmentManager fm, String[] tabArray) {
        super(fm);
        this.tabName = new String[tabArray.length]; 
        this.tabName = tabArray;
    }

    @Override
    public Fragment getItem(int position) {     

        TabPageFragment tabPageFragment = new TabPageFragment(position);
        return tabPageFragment;
    }

    @Override
    public int getCount() {
        return tabName.length;
    }

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

each page of ViewPager use's this class for populate Fragment :

public class TabPageFragment extends Fragment {

private GridView mgridview; // gridView inside Fragment
private int tabPosition;
private ArrayAdapterGridview arrayadapter; //adapter for my gridView

public TabPageFragment(int tabposition) {

    this.tabPosition = tabposition;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {             

    mgridview = (GridView) rootView.findViewById(R.id.gridView2);

    View rootView = inflater.inflate(R.layout.fragment_grid_view, container,false);

        DownloadContentTab dct = new DownloadContentTab();
        dct.execute();

    return rootView;
}

 // DownloadJSON AsyncTask
private class DownloadContentTab extends AsyncTask<Void, Void, Integer> {

    @Override
    protected Integer doInBackground(Void... params) {

        JSONfunctions jsf = new JSONfunctions(); //this function retrieve's json for me
        JSONArray jsonarray = jsf.getJSONfromURL(myUrl); // this url is differ for each page

        String[] gridNameArray = new String[jsonarray.length()];
        String[] gridPicArray  = new String[jsonarray.length()];

        for (int j = 0; j < jsonarray.length(); j++) {

            try {
            JSONObject jsonobject = jsonarray.getJSONObject(j);                          
            gridNameArray[j] = jsonobject.getString("title");
            gridPicArray[j] = jsonobject.getString("pic");

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }   

        arrayadapter = new ArrayAdapterGridview(context, gridPicArray, gridNameArray);

        return 0;
    }

    @Override
    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);

        mgridview.setAdapter(arrayadapter);

    }


   }    
 }

来源:https://stackoverflow.com/questions/35824429/populate-each-fragment-in-viewpager-with-different-json-object-without-loading-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!