带有重新加载值的 viewpager 中的列表视图

Listview inside viewpager with reload values

我想创建一个包含 2 个项目的 ViewPager,它的每个视图都包含列表视图。当我从一页滑动到另一页时,每次列表视图都会刷新。我只需要刷新一次。意味着如果一旦将值加载到列表视图,然后我滑动到下一页,它不需要再次重新加载。我需要显示已经加载的值以列表视图

代码

public class ViewpagerAdapter extends FragmentPagerAdapter {
private Context _context;
public static int totalPage=2;

public ViewpagerAdapter(Context applicationContext,
        android.support.v4.app.FragmentManager fragmentManager) {
    super(fragmentManager); 
    _context=applicationContext;
}

@Override
public Fragment getItem(int position) {
    Fragment f = new Fragment();
    switch(position){
    case 0:
        f= FirstFragment.newInstance(_context);
        break;
    case 1:
        f= SecondFragment.newInstance(_context);
        break;
    }
    return f;
}
@Override
public int getCount() {
    return totalPage;
}}

第一个片段

public class FirstFragment extends Fragment {


private Context context;
private List<Historyitem> historyitems;
HistoryRecycleListAdapter historyadapter;
Handler mHandler;

ListView hisrecyclerview;

public static Fragment newInstance(Context context) {
    FirstFragment f = new FirstFragment();
    return f;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ViewGroup view = (ViewGroup) inflater.inflate(R.layout.firstfragment, null);

    context = getActivity();
    hisrecyclerview = (ListView)view.findViewById(R.id.hisrecyclerview);  
    LoadHistoryInitiate();     
    return view;
}

public void LoadHistoryInitiate() throws IllegalStateException{

    new AsyncTask<String, String, String>(){

        protected void onPreExecute() {

            historyitems = new ArrayList<Historyitem>();
            historyitems.clear();
        };
        @Override
        protected String doInBackground(String... arg0) {


                final List<NameValuePair> list=new ArrayList<NameValuePair>();
                list.add(new BasicNameValuePair("id", id));

                final HttpParams httpParams = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
                DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
                HttpPost httpPost = new HttpPost(url);
                httpPost.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
                try {
                    httpPost.setEntity(new UrlEncodedFormEntity(list));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    HttpResponse response = httpClient.execute(httpPost);

                    BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                    String json = reader.readLine();


                    JSONObject jsonObj = new JSONObject(json);
                    if(jsonObj.has("values")){

                        JSONArray feedArray = jsonObj.getJSONArray("values");
                        for (int i = 0; i < feedArray.length(); i++) {
                            JSONObject feedObj = (JSONObject) feedArray.get(i);

                            final Historyitem item = new Historyitem();

                            if(feedObj.has("Reported_Time")){
                                if(!feedObj.getString("Reported_Time").replaceAll(" ", "").equals("")){
                                    item.setReported_Time(feedObj.getString("Reported_Time"));
                                }
                            }
                            historyitems.add(item);

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

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

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

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

                }

            return null;
        }
        protected void onPostExecute(String result) {
            System.out.println(historyitems.size());
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {

                        historyadapter = new HistoryRecycleListAdapter(getActivity(), getActivity(),historyitems);
                        hisrecyclerview.setAdapter(historyadapter);
                        hisrecyclerview.addFooterView(btnLoadMore);
                        hisrecyclerview.setAdapter(historyadapter);
                                       }
            }, 1000);
        };
    }.execute();
}  }

第二个片段

public class SecondFragment extends Fragment {
private Context context;
private List<Loaditem> loaditems;
RecycleListAdapter loadadapter;
Handler mHandler;

ListView recyclerview;

public static Fragment newInstance(Context context) {
    SecondFragment f = new SecondFragment();
    return f;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ViewGroup view = (ViewGroup) inflater.inflate(R.layout.secondfragment, null);

    context = getActivity();
    recyclerview = (ListView)view.findViewById(R.id.recyclerview);  
    LoadInitiate();     
    return view;
}

public void LoadInitiate() throws IllegalStateException{

    new AsyncTask<String, String, String>(){

        protected void onPreExecute() {

            loaditems = new ArrayList<Loaditem>();
            loaditems.clear();
        };
        @Override
        protected String doInBackground(String... arg0) {


                final List<NameValuePair> list=new ArrayList<NameValuePair>();
                list.add(new BasicNameValuePair("uid", uid));

                final HttpParams httpParams = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
                DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
                HttpPost httpPost = new HttpPost(url);
                httpPost.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
                try {
                    httpPost.setEntity(new UrlEncodedFormEntity(list));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    HttpResponse response = httpClient.execute(httpPost);

                    BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                    String json = reader.readLine();


                    JSONObject jsonObj = new JSONObject(json);
                    if(jsonObj.has("values")){

                        JSONArray feedArray = jsonObj.getJSONArray("values");
                        for (int i = 0; i < feedArray.length(); i++) {
                            JSONObject feedObj = (JSONObject) feedArray.get(i);

                            final Loaditem item = new Loaditem();

                            if(feedObj.has("Time")){
                                if(!feedObj.getString("Time").replaceAll(" ", "").equals("")){
                                    item.setReported_Time(feedObj.getString("Time"));
                                }
                            }
                            loaditems.add(item);

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

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

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

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

                }

            return null;
        }
        protected void onPostExecute(String result) {
            System.out.println(historyitems.size());
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {

                        loadadapter = new RecycleListAdapter(getActivity(), getActivity(),loaditems);
                        recyclerview.addFooterView(btnLoadMore);
                        recyclerview.setAdapter(loadadapter);
                                       }
            }, 1000);
        };
    }.execute();
}  }

最简单的解决方案是 setOffscreenPageLimit(int limit) 将其设置为您的 viewPager。

要解决您的问题,请在 Activity:

中使用以下方法
setOffscreenPageLimit(int limit) 

Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state.

在您的 ViewPager 实例化后添加下面一行

viewpager.setOffscreenPageLimit(2)