在到达列表底部之前,将数据顺利添加到recyclerView

时间:2019-04-04 11:11:31

标签: android android-recyclerview recycler-adapter

我正在尝试创建Google分页库的类似物。我做了这样的事:

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

                    if (loading) {
                        if (visibleItemCount + pastVisiblesItems >= totalItemCount && !recyclerView.canScrollVertically(1)) {
                            loading = false;
                            /*if (next_url != null) {
                                getMessages(type, a_token, Integer.parseInt(Uri.parse(next_url).getQueryParameter("offset")));
                                ms.setM_offset(Integer.valueOf(Uri.parse(next_url).getQueryParameter("offset")));
                            }*/
                        }
                    }


                }

                if (dy >= 5) {
                    loading = true;
                }

                if (dy < 0) {
                    visibleItemCount = layoutManager.getChildCount();
                    pastVisiblesItems = layoutManager.findLastVisibleItemPosition();

                    if (loading) {
                        if (pastVisiblesItems - visibleItemCount <= 0 && !recyclerView.canScrollVertically(-1)) {
                            loading = false;


                        }
                    }
                }

            }
        });

,它通常可以正常工作,当我到达列表的底部时,我发送请求,但是我看到非常奇怪的添加动画,并且我对重新创建这种预下载数据的逻辑感兴趣。这是我的适配器:

public class myAdapter extends RecyclerView.Adapter<myAdapter.SenderHolder> {

    private Integer type, position;
    private ArrayList<MessageModel> messageList;
    private Context ctx;
    private static final String keyOutboundMessageId = "outbound_message_id";
    private static final String keyIncomingMessageId = "incoming_message_id";
    private static final String keyDraftMessageId = "draft_message_id";

    myAdapter(ArrayList<MessageModel> messageList, Context ctx, int type) {
        this.messageList = messageList;
        this.ctx = ctx;
        this.type = type;
    }


    @NonNull
    @Override
    public SenderHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.mc_list_item, viewGroup, false);
        return new SenderHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull final SenderHolder holder, final int position) {
        final MessageModel message = messageList.get(position);




 }



    void removeItem(int position) {
        setPosition(position);
        messageList.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, messageList.size());
    }


    public Integer getPosition() {
        return position;
    }

    public void setPosition(Integer position) {
        this.position = position;
    }



    void restoreItem(MessageModel item) {
        messageList.add(getPosition(), item);
        notifyItemInserted(getPosition());
    }

    ArrayList<MessageModel> getMessageList() {
        return messageList;
    }

    private Date yesterday() {
        final Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        return cal.getTime();
    }

    @Override
    public void onBindViewHolder(@NonNull SenderHolder holder, int position, @NonNull List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
    }

    private String getCurrentDate() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat mdformat = new SimpleDateFormat("dd.MM.yyyy", Locale.getDefault());
        return mdformat.format(calendar.getTime());
    }

    @Override
    public int getItemCount() {
        return messageList.size();
    }

    class SenderHolder extends RecyclerView.ViewHolder {

        final TextView tvFrom, tvSubject, tvDate, tvBody;
        final ImageView attachIV, indicactor;
        RelativeLayout relativeLayout;

        SenderHolder(View itemView) {
            super(itemView);
            tvFrom = itemView.findViewById(R.id.tv_from);
            tvSubject = itemView.findViewById(R.id.tv_subject);
            tvDate = itemView.findViewById(R.id.tv_date);
            tvBody = itemView.findViewById(R.id.letter_text);
            attachIV = itemView.findViewById(R.id.clip);
            indicactor = itemView.findViewById(R.id.indicator);
            relativeLayout = itemView.findViewById(R.id.message_list_item);
        }
    }
}

也许有人知道如何解决我的问题?谢谢您的帮助:)

0 个答案:

没有答案