如何从服务器加载回收站视图中的更多数据?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从服务器加载回收站视图中的更多数据?相关的知识,希望对你有一定的参考价值。

我有一个回收站视图,当用户滚动到底部时,我从服务器加载一些数据我想显示进度条并向服务器发送另一个请求以获取更多数据。我尝试了下面的代码,但它无法从服务器加载更多数据。请帮忙

   private RecyclerView mRecyclerView;
    private List<User> mUsers = new ArrayList<>();
    private UserAdapter mUserAdapter;
    private static String sz_RecordCount;
    private static String sz_LastCount;
    private final int m_n_DefaultRecordCount = m_kDEFAULT_RECORD_COUNT;
    private static final int m_kDEFAULT_RECORD_COUNT = 5;
    private ArrayList<CDealAppDatastorage> s_oDataset;
    private String TAG = MainActivity.class.getSimpleName();
    private CDealAppDatastorage item;
    private static int arrayCount;
    private Context context;
    private PreferenceHelper m_oPreferenceHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = MainActivity.this;
        m_oPreferenceHelper = new PreferenceHelper(context);

        sz_RecordCount = String.valueOf(m_n_DefaultRecordCount);// increment of record count
        int intialLastCount = 0;
        sz_LastCount = String.valueOf(intialLastCount);// increment of last count...

        s_oDataset = new ArrayList<>();// making object of Arraylist
        //initial request for data
        initalDealListing();


        mRecyclerView = (RecyclerView) findViewById(R.id.recycleView);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mUserAdapter = new UserAdapter();


        mUserAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                Log.e("haint", "Load More");
                mUsers.add(null);
                mUserAdapter.notifyItemInserted(mUsers.size() - 1);

                //Load more data for reyclerview
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Log.e("haint", "Load More 2");


                        //Remove loading item
                        mUsers.remove(mUsers.size() - 1);
                        mUserAdapter.notifyItemRemoved(mUsers.size());
                        //sending request to server for more data
                        moreDealsRequest();

                        mUserAdapter.notifyDataSetChanged();
                        mUserAdapter.setLoaded();
                    }
                }, 5000);
            }
        });
    }

    private void initalDealListing() {
        String m = "9565656565";
        String p = "D55A8077E0208A5C5B25176608EF84BD";
        // 3. build jsonObject
        try {
            final JSONObject jsonObject = new JSONObject();// making object of Jsons.
            jsonObject.put("agentCode", m.trim());// put mobile number
            jsonObject.put("pin", p.trim());// put password
            jsonObject.put("recordcount", sz_RecordCount.trim());// put record count
            jsonObject.put("lastcountvalue", sz_LastCount.trim());// put last count
            Log.e("CAppList:", sz_RecordCount);
            Log.e("Capplist:", sz_LastCount);
            Log.d(TAG, "Server Request:-" + jsonObject.toString());
            final String m_DealListingURL = APIStorage.IREWARDS_URL + APIStorage.DEALLISTING_URL;
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, m_DealListingURL, jsonObject, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, "Server Response:-" + response);

                    try {
                        int nResultCodeFromServer = Integer.parseInt(response.getString(ServerResponseStorage.s_szRESULT_CODE));
                        if (nResultCodeFromServer == ConstantInt.m_kTRANSACTION_SUCCESS) {

                            JSONArray posts = response.optJSONArray(ServerResponseStorage.s_szDEAL_ARRAY);// get Deal list in array from response
                            s_oDataset.clear();
                            for (int i = 0; i < posts.length(); i++) {// loop for counting deals from server
                                try {
                                    JSONObject post = posts.getJSONObject(i);// counting deal based on index
                                    item = new CDealAppDatastorage();// creating object of DealAppdata storage
                                    item.setM_szHeaderText(post.getString(ServerResponseStorage.s_szDEAL_NAME));// get deal name from response
                                    item.setM_szsubHeaderText(post.getString(ServerResponseStorage.s_szDEAL_CODE));// get dealcode from response
                                    s_oDataset.add(item);// add all items in ArrayList
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                            arrayCount = posts.length();
                            Log.d(TAG, "ArrayCount::" + arrayCount);
                            /*here we are storing no. of deals coming from server*/
                            // write
                            m_oPreferenceHelper.saveIntegerValue("LastCountLength", arrayCount);
                            if (!s_oDataset.isEmpty()) {// condition if data in arraylist is not empty
                                mRecyclerView.setAdapter(mUserAdapter);
                            }


                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d(TAG, "Server error:-" + error);
                }
            });
            RequestQueue requestQueue = Volley.newRequestQueue(context);
            jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(ConstantInt.INITIAL_TIMEOUT_MS, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            requestQueue.add(jsonObjectRequest);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    /*This method send request to server for more deals*/
    private void moreDealsRequest() {
        try {
            // 3. build jsonObject
            final JSONObject jsonObject = new JSONObject();// making object of Jsons.
            jsonObject.put(ServerRequestKeyStorage.s_szAGENT_CODE, "9565656565");// put mobile number
            jsonObject.put(ServerRequestKeyStorage.s_szPASSWORD, "D55A8077E0208A5C5B25176608EF84BD");// put password
            jsonObject.put(ServerRequestKeyStorage.s_szRECORD_COUNT, sz_RecordCount.trim());// put record count
            jsonObject.put(ServerRequestKeyStorage.s_szLAST_COUNT, sz_LastCount.trim());// put last count
            Log.e("CAppList:", sz_RecordCount);
            Log.e("Capplist:", sz_LastCount);
            // 4. convert JSONObject to JSON to String

            Log.e(TAG, "Server Request:-" + jsonObject.toString());
            RequestQueue requestQueue = Volley.newRequestQueue(context);
            final String imgPath = APIStorage.IREWARDS_URL + APIStorage.DEAL_IMAGE_PATH;
            final String m_DealListingURL = APIStorage.IREWARDS_URL + APIStorage.DEALLISTING_URL;
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, m_DealListingURL, jsonObject, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.e(TAG, "Server Response:-" + response);
                    try {
                        int nResultCodeFromServer = Integer.parseInt(response.getString(ServerResponseStorage.s_szRESULT_CODE));

                        if (nResultCodeFromServer == ConstantInt.m_kTRANSACTION_SUCCESS) {
                            // Select the last row so it will scroll into view...
                            JSONArray posts = response.optJSONArray(ServerResponseStorage.s_szDEAL_ARRAY);// GETTING DEAL LIST
                            for (int i = 0; i < posts.length(); i++) {
                                try {
                                    JSONObject post = posts.getJSONObject(i);// GETTING DEAL AT POSITION AT I
                                    item = new CDealAppDatastorage();// object create of DealAppdatastorage
                                    item.setM_szHeaderText(post.getString(ServerResponseStorage.s_szDEAL_NAME));//getting deal name
                                    item.setM_szsubHeaderText(post.getString(ServerResponseStorage.s_szDEAL_CODE));// getting deal code
                                    s_oDataset.add(item);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }

                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d(TAG, "Server Error::" + error);
                }
            });
            jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(ConstantInt.INITIAL_TIMEOUT_MS, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            requestQueue.add(jsonObjectRequest);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    static class UserViewHolder extends RecyclerView.ViewHolder {
        public TextView tvName;
        public TextView tvEmailId;

        public UserViewHolder(View itemView) {
            super(itemView);
            tvName = (TextView) itemView.findViewById(R.id.tvName);

            tvEmailId = (TextView) itemView.findViewById(R.id.tvEmailId);
        }
    }

    static class LoadingViewHolder extends RecyclerView.ViewHolder {
        public ProgressBar progressBar;

        public LoadingViewHolder(View itemView) {
            super(itemView);
            progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar1);
        }
    }

    class UserAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private final int VIEW_TYPE_ITEM = 0;
        private final int VIEW_TYPE_LOADING = 1;

        private OnLoadMoreListener mOnLoadMoreListener;

        private boolean isLoading;
        private int visibleThreshold = 5;
        private int lastVisibleItem, totalItemCount;

        public UserAdapter() {
            final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
            mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);

                    totalItemCount = linearLayoutManager.getItemCount();
                    lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();

                    if (!isLoading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
                        if (mOnLoadMoreListener != null) {
                            mOnLoadMoreListener.onLoadMore();
                        }
                        isLoading = true;
                    }
                }
            });
        }

        public void setOnLoadMoreListener(OnLoadMoreListener mOnLoadMoreListener) {
            this.mOnLoadMoreListener = mOnLoadMoreListener;
        }

        @Override
        public int getItemViewType(int position) {
            return s_oDataset.get(position) == null ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if (viewType == VIEW_TYPE_ITEM) {
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_user_item, parent, false);
                return new UserViewHolder(view);
            } else if (viewType == VIEW_TYPE_LOADING) {
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_loading_item, parent, false);
                return new LoadingViewHolder(view);
            }
            return null;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof UserViewHolder) {
                CDealAppDatastorage user = s_oDataset.get(position);
                UserViewHolder userViewHolder = (UserViewHolder) holder;
                userViewHolder.tvName.setText(user.getM_szHeaderText());
                userViewHolder.tvEmailId.setText(user.getM_szsubHeaderText());
            } else if (holder instanceof LoadingViewHolder) {
                LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
                loadingViewHolder.progressBar.setIndeterminate(true);
            }
        }

        @Override
        public int getItemCount() {
            return s_oDataset == null ? 0 : s_oDataset.size();
        }

        public void setLoaded() {
            isLoading = false;
        }
    }
}
答案

使用以下代码:

首先声明这些全局变量:

int visibleItemCount, totalItemCount = 1;
int firstVisiblesItems = 0;
int totalPages = 1; // get your total pages from web service first response
int current_page = 0;

boolean canLoadMoreData = true; // make this variable false while your web service call is going on.

LinearLayoutManager linearLayoutManager;

将布局管理器分配给您的Recyclerview:

linearLayoutManager = new LinearLayoutManager(mActivity);
mRecyclerView.setLayoutManager(linearLayoutManager);

您的recyclerview的滚动监听器:

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            if (dy > 0) //check for scroll down
            {
                visibleItemCount = linearLayoutManager.getChildCount();
                totalItemCount = linearLayoutManager.getItemCount();
                firstVisiblesItems = linearLayoutManager.findFirstVisibleItemPosition();

                    if (canLoadMoreData) {
                        if ((visibleItemCount + firstVisiblesItems) >= totalItemCount) {
                            if (current_page < totalPages) {
                                canLoadMoreData  = false;
                                /**
                                 * .
                                 * .
                                 * .
                                 * .call your webservice with page index
                                 * .
                                 * .
                                 *
                                 */
                                //After completion of web service make 'canLoadMoreData = true'
                            }
                        }
                    }

            }
        }
    });
另一答案

太晚了,但我也提到同样的方式检查我的代码它对我有用

这是使用改造的API调用并获取listnotifications然后在这个方法中我为loadmore getMoreNotificationListApiCall()再创建一个方法

public void getNotification()
 {
    if (listnotifications.size() > 0) {
                                        notificationListAdapter = new NotificationListAdapter(NotificationListActivity.this, listnotifications, notificationListRecyclerview);
                                        notificationListRecyclerview.setAdapter(notificationListAdapter);

                                        notificationListAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
                                            @Override
                                            public void onLoadMore() {
                                                Log.e("ad", "Load More");

                                                listnotifications.add(null);

                                                Handler handler = new Handler();
                                                final Runnable r = new Runnable() {
                                                    public void run() {
                                                        if (listnotifications.size()>0) {
                                                            notificationListAdapter.notifyItemInserted(listnotifications.size() - 1);
                                                        }
                                                    }
                                                };
                                                handler.post(r);

                                                try {

                                                    if (CommonUtils.isConnectingToInternet(NotificationListActivity.this)) {
                                                        // Internet Connection is Present
                                                        getMoreNotificationListApiCall();

                                                    } else {
                                                        //Remove loading item
                                                        if (listnotifications.size()>0) {
                                                            listnotifications.remove(listnotifications.size() - 1);
                                                        }

                                                        notificationListAdapter.notifyItemRemoved(listnotifications.size());
                                                        CommonUtils.commonToast(NotificationListActivity.this, getResources().getString(R.string.no_internet_exist));
                                                    }

                                                } catch (Exception e) {
                                                    // TODO Auto-generated catch block
                                                    e.printStackTrace();
                                                }

                                            }
                                        });


                                    }

    }

这个方法中的getMoreNotificationListApiCall()方法代码我增加startlimit意味着页面索引并删除null(我正在使用retorfit进行api调用)

private void getMoreNotificationListApiCall() {
    try {
        if (CommonUtils.isConnectingToInternet(NotificationListActivity.this)) {

            StartLimit = StartLimit + 10;

            Call<NotificationBase> call = ApiHandler.getApiService().getNotificationListApi(notificationListJsonMap());

            call.enqueue(new Callback<NotificationBase>() {
                @Override
                public void onResponse(Call<NotificationBase> registerCall, Response<NotificationBase> response) {
                    Log.e(TAG, " Full json gson => " + "Hi i am here");
                    try {
                        Log.e(TAG, " Full json gson => " + new Gson().toJson(response));
                        JSONObject jsonObj = new JSONObject(new Gson().toJson(response).toString());
                        Log.e(TAG, " responce => " + jsonObj.getJSONObject("body").toString());

                        if (response.isSuccessful()) {
                            int success = response.body().getSuccess();
                            if (success == 1) {

                                List<NotificationBean> moreNotication = response.body().getNotificatons();

                                //Remove loading item
                                if (listnotifications.size() > 0) {
                                    listnotifications.remove(listnotifications.size() - 1);
                                }

                                notificationListAdapter.notifyItemRemoved(listnotifications.size());

                                for (int i = 0; i < moreNotication.size(); i++) {
                                    listnotifications.add(moreNotication.get(i));
                                }

                                notificationListAdapter.notifyDataSetChanged();
                                notificationListAdapter.setLoaded();


                            } else if (success == 0) {
                                if (listnotifications.size() > 0) {
                                    listnotifications.remove(listnotifications.size() - 1);
                                }
                                notificationListAdapter.notifyItemRemoved(listnotifications.size());

                            } else {
                                if (listnotifications.size() > 0) {
                                    listnotifications.remove(listnotifications.size() - 1);
                                }
                                notificationListAdapter.notifyItemRemoved(listnotifications.size());

                            }
                        } else {
                            if (listnotifications.size() > 0) {
        

以上是关于如何从服务器加载回收站视图中的更多数据?的主要内容,如果未能解决你的问题,请参考以下文章

从firebase检索图像以在片段中的回收器视图时出错

如何从android studio中的recycler视图将数据添加到SQLite数据库中

片段视图返回后执行的 Firebase 查询

如何使用 kotlin 实现分页

如何将值从一个片段的回收器视图项传递到另一个片段

如何使用光标和循环显示来自 sqlite 的片段的 recyclerview