CardView上的滚动条无法正常工作

时间:2015-12-14 07:52:08

标签: android android-recyclerview android-cardview

我已在我的应用中使用recyclerview实现了cardview。我在滚动cardview记录时遇到问题。当我向下滚动并加载更多记录时,新记录将被旧记录替换,旧记录在向上滚动时不显示。我不想替换旧记录。

代码片段:

    recList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            int firstVisiblesItems, visibleItemCount, totalItemCount;

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                if(dy > 0) //check for scroll down
                {
                    visibleItemCount = recList.getChildCount();
                    totalItemCount = llm.getItemCount();
                    firstVisiblesItems = llm.findFirstVisibleItemPosition();
                    final int lastItem = firstVisiblesItems + visibleItemCount;

                    if (lastItem == totalItemCount && totalItemCount != 0 && Restotcnt - lastItem >= totresults && stprocess == 1) {
//                            // you have reached end of list, load more data
                        totcount = lastItem + 1;
                        limit = limit + totresults;
                        new LoadContestList(getApplicationContext()).execute();
//                    Log.d("limit=", limit + "-results=-" + totresults);
                    }
                }


            }
        });

这是我完整的Async Class代码:

    public class LoadContestList extends AsyncTask<String, String, String> {
        JSONArray contacts = null;
        private ProgressDialog pDialog;
        private Context context;
        ArrayList<contestInfo> arrayList = new ArrayList<contestInfo>();
        public LoadContestList(Context context) {
            this.context = context;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            listofcontests.setVisibility(View.GONE);
            recList.setVisibility(View.VISIBLE);
            pDialog = new ProgressDialog(MainActivityz.this);
            pDialog.setMessage("Loading...");
            pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... args) {

            // TODO Auto-generated method stub
            try{

                stprocess=0;
                JSONObject jsonObj = new JSONObject();
                jsonObj.put("limitCountMin", limit);
                jsonObj.put("totalResults", totresults);
                jsonObj.put("sort", "most-views");
                jsonObj.put("days", "");
                jsonObj.put("posted_search", ""); // Set the first name/pair
                jsonObj.put("rewardSearch", "");
                jsonObj.put("categories","");
                jsonObj.put("expiry", "");
                jsonObj.put("contestWorth", textSeek_text.getText().toString());
                jsonObj.put("MediaType","");
                jsonObj.put("ParticipationType",aParticipation);
                jsonObj.put("AgeType", aAge);
                jsonObj.put("TargetType", aTarget);
                jsonObj.put("TalentType", aTalent);
                jsonObj.put("RewardType", aReward);

                JSONArray jsonArray = new JSONArray();
                jsonArray.put(jsonObj);

                final HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(LOAD_CONTEST_LIST_URL);

                String str = jsonArray.toString().replace("[", "");
                String str1 = str.replace("]", "");

                httppost.setEntity(new StringEntity(str1.toString()));
                HttpResponse resp = httpclient.execute(httppost);
                HttpEntity ent = resp.getEntity();

                String result = EntityUtils.toString(ent);
                JSONObject jsobj = new JSONObject(result);
                Res = jsobj.getString(TAG_RESULT);

                if (Res.equals("Success")) {
                    flag = true;
                    ResData = jsobj.getString(TAG_DATA);

                    if (!ResData.equals("No Result")) {

                        Restotcnt = jsobj.getInt(TAG_TOTAL_COUNT);
                        Rescntbusiness = jsobj.getInt(TAG_COUNT_BUSINESS);

                        JSONArray jsonArray1 = jsobj.getJSONArray("data");

                        for (int i = 0; i < jsonArray1.length(); i++) {
                            JSONObject c = jsonArray1.getJSONObject(i);
                            contestInfo ci = new contestInfo();

                            ci.id = c.getString(TAG_CONTEST_ID);
                            ci.image = c.getString(TAG_CONTEST_IMAGE);
                            ci.name = c.getString(TAG_CONTEST_NAME);
                            ci.winStr = c.getString(TAG_WIN_STRING);
                            ci.description = c.getString(TAG_CONTEST_DESCR);
                            ci.date = c.getString(TAG_CONTEST_DATE);

                            arrayList.add(ci);
                        }
                        Log.d("CardArray",arrayList+"");


                    } else {
                        flag = false;

                    }
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            contestcout.setText(Restotcnt.toString());
//                            brandcout.setText(Rescntbusiness.toString());
//                            contestListCustom adapter = new contestListCustom(MainActivityz.this, contestid, contestname, contestdescription, contestdateandtime, logo);
//                            adapter.notifyDataSetChanged();
//                            listofcontests.setAdapter(adapter);
//                            listofcontests.setSelection(limit);
//                            pDialog.dismiss();
//
//                        }
//                    });

                }

            }

            catch (JSONException ej)
            {
                flag = false;
                ej.printStackTrace();
            }
            catch (Exception e)
            {
                flag = false;
                e.printStackTrace();
            }

            return null;

        }


        protected void onPostExecute(String file_url) {
            if(flag)
            {

                contestcout.setText(Restotcnt.toString());
                brandcout.setText(Rescntbusiness.toString());
                ContactAdapter ca = new ContactAdapter(MainActivityz.this,arrayList);
                ca.notifyDataSetChanged();
                recList.setAdapter(ca);

                pDialog.dismiss();
            }
            else
            {
//                flag =false;
//                listofcontests.setVisibility(View.GONE);
//                nodatafound = (TextView) findViewById(R.id.tvInternetAlert);
//                nodatafound.setVisibility(View.VISIBLE);
            }
        stprocess=1;
        }

    }

请帮帮我。

1 个答案:

答案 0 :(得分:1)

使全局arrayList像

private List ca = new ArrayList&lt;&gt;();

还要使适配器的全局实例像

私人ContactAdapter ca;

然后在你的onCreate方法中添加以下这些行

ca = new ContactAdapter(MainActivityz.this,arrayList); recList.setAdapter(CA);

现在在onPostExecute里面只写

notifyDataSetChanged();

你的问题将得到解决。 如果您仍然遇到任何问题,请告诉我