android recyclelerview使用齐射滚动时加载更多项目

时间:2017-07-04 10:18:09

标签: android android-recyclerview

滚动我的RecyclerView时,请帮我从服务器加载更多数据。在这里,我通过使用排球字符串请求从RecyclerView服务器加载数据,成功创建了Mysql

这是我的代码。

    private void populateRecycleView() {
    if (Utility.checkNetworkConnection(this)) {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("Searching...");
        progressDialog.setMessage("Searching for the blood donor. Please wait a moment.");
        progressDialog.setCancelable(false);
        progressDialog.show();
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Constants.GET_DONORS_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        progressDialog.dismiss();
                        try {
                            JSONArray jsonArray = new JSONArray(response);
                            int count = 0;
                            while (count < jsonArray.length()) {
                                JSONObject jsonObject = jsonArray.getJSONObject(count);

                                String firstName = jsonObject.getString("fName");
                                String secondName = jsonObject.getString("sName");
                                String email = jsonObject.getString("emailid");
                                String password = jsonObject.getString("pass");
                                String mobile = jsonObject.getString("mobile");
                                String bloodRt = jsonObject.getString("blood");
                                String age = jsonObject.getString("age");
                                String gender = jsonObject.getString("gender");
                                String country = jsonObject.getString("country");
                                String location = jsonObject.getString("location");
                                String latitude = jsonObject.getString("latitude");
                                String longitude = jsonObject.getString("longitude");
                                String profilePicFIleName = jsonObject.getString("picname");
                                String profilePicURL = jsonObject.getString("pic");

                                Donor donor = new Donor(firstName, secondName, email, password, mobile, bloodRt, age, gender,
                                        country, location, latitude, longitude, profilePicFIleName, profilePicURL);
                                donorsList.add(donor);
                                count++;
                            }
                            donorsAdapter = new DonorsAdapter(FindDonorResult.this, donorsList);
                            recyclerView = (RecyclerView) findViewById(R.id.rv_search_result_donor);
                            recyclerView.setHasFixedSize(true);
                            recyclerView.setLayoutManager(new LinearLayoutManager(FindDonorResult.this));
                            recyclerView.setItemAnimator(new DefaultItemAnimator());
                            recyclerView.setAdapter(donorsAdapter);

                            donorsAdapter.notifyDataSetChanged();

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

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        progressDialog.dismiss();
                        Toast.makeText(FindDonorResult.this, "Active data network is not available.", Toast.LENGTH_LONG).show();

                    }
                }
        ) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("bloodGroup", bloodGroup);
                return params;
            }
        };
        NetworkRequestSingleTon.getOurInstance(this).addToRequestQue(stringRequest);
    } else {
        Utility.checkNetworkConnectionFound(this);
    }
}

这是我的RecyclerView适配器......

public class DonorsAdapter extends RecyclerView.Adapter<DonorsAdapter.CustomViewHolder> {

private Context context;
private ArrayList<Donor> donorList;
private String bloodGroup;



public DonorsAdapter(Context context, ArrayList<Donor> donorList) {
    this.context = context;
    this.donorList = donorList;
}


@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_blood_donors_result,
            parent, false);
    return new CustomViewHolder(view);
}

@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {

    final Donor donor = donorList.get(position);
    String displayName = donor.getFirstName() + " " + donor.getSecondName();
    holder.tvDisplayName.setText(displayName);
    holder.tvEmailID.setText(donor.getEmail());

    String userProfileURL = donor.getProfilePicURL();
    if (!userProfileURL.equals("")) {
        Picasso.with(context).load(userProfileURL).resize(80, 80).centerCrop().
                into(holder.ivProfilePic);
    } else {
        holder.ivProfilePic.setImageResource(R.drawable.ic_person_white_24dp);
    }

    bloodGroup = donor.getBloodGroup();
    if (bloodGroup.equals("A+"))
        holder.ivBloodTypeDisplay.setImageResource(R.drawable.a_);
    else if (bloodGroup.equals("A-"))
        holder.ivBloodTypeDisplay.setImageResource(R.drawable.a_negative);
    else if (bloodGroup.equals("B+"))
        holder.ivBloodTypeDisplay.setImageResource(R.drawable.b_positive);
    else if (bloodGroup.equals("B-"))
        holder.ivBloodTypeDisplay.setImageResource(R.drawable.b_negative);
    else if (bloodGroup.equals("O+"))
        holder.ivBloodTypeDisplay.setImageResource(R.drawable.o_positive);
    else if (bloodGroup.equals("O-"))
        holder.ivBloodTypeDisplay.setImageResource(R.drawable.o_negative);
    else if (bloodGroup.equals("AB+"))
        holder.ivBloodTypeDisplay.setImageResource(R.drawable.ab_positive);
    else if (bloodGroup.equals("AB-"))
        holder.ivBloodTypeDisplay.setImageResource(R.drawable.ab_negative);

    if(Utility.isNetworkEnabled){
        holder.constraintLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, DisplayDonorDetails.class);
                intent.putExtra("donor", donor);
                context.startActivity(intent);
            }
        });
    }else {
        Toast.makeText(context, "Network not available.", Toast.LENGTH_SHORT).show();
    }

}

@Override
public int getItemCount() {
    if(donorList != null){
        return donorList.size();
    }else {
        return 0;
    }
}

public class CustomViewHolder extends RecyclerView.ViewHolder {
    ImageView ivProfilePic, ivBloodTypeDisplay, ivArrow;
    TextView tvDisplayName;
    TextView tvEmailID;
    ConstraintLayout constraintLayout;


    public CustomViewHolder(View itemView) {
        super(itemView);
        ivProfilePic = (ImageView) itemView.findViewById(R.id.civ_user_profile_picture);
        ivBloodTypeDisplay = (ImageView) itemView.findViewById(R.id.civ_user_blood_type_display);
        ivArrow = (ImageView) itemView.findViewById(R.id.civ_arrow);
        tvDisplayName = (TextView) itemView.findViewById(R.id.tvUserNameOnRV);
        tvEmailID = (TextView) itemView.findViewById(R.id.tvEmailDisplayOnRV);
        constraintLayout = (ConstraintLayout) itemView.findViewById(R.id.recycle_view_item_container);
    }
}

}

2 个答案:

答案 0 :(得分:0)

仅使用donorList的50个第一个元素填充您的donorAdapter,创建一个保存所显示的最新元素位置的函数,并从您需要时保存的最新位置开始,将其他50个捐赠者添加到您的适配器。

希望它有所帮助。

修改

首先创建一个emptyList:

List<Donor> subElements = new ArrayList<>();

并将其传递给您的适配器:

donorsAdapter = new DonorsAdapter(FindDonorResult.this, subElements);

现在您可以创建一个这样的方法(例如,您可以调用onClick事件):

private int LAST_POSITION = 0;
private int DONORS_NUM_TOSHOW = 50;

public void showMoreDonors(){
    if(donarsList.size() > Last_postion+50){
        List<Donor> tempList = new ArrayList<Donor>(donorsList.subList(Last_postion,Last_postion+DONORS_NUM_TOSHOW));

        for(Donor a : tempList){
            subElements.add(a);
        }
        Last_postion += DONORS_NUM_TOSHOW;
        donorsAdapter.notifyDataSetChanged();
    }else{
        List<Donor> tempList = new ArrayList<Donor>(donorsList.subList(Last_postion,donorsList.size()));
        for(Donor a : tempList){
            subElements.add(a);
        }
        donorsAdapter.notifyDataSetChanged();
    }
}

记得检查donorList何时结束。 我没有测试它,但我希望理解这个想法是有用的。

答案 1 :(得分:0)

最后,我把它排除在外。我从这个博客得到了一个很棒的教程。

http://android-pratap.blogspot.in/2015/06/endless-recyclerview-with-progress-bar.html

我做了一些更改来填充列表,因为我的数据在远程服务器上,并且通过使用volley库我将数据提取到列表中。剩下的事情是一样的。