android - Endless Scroll RecyclerView 总是返回顶部

标签 android android-recyclerview infinite-scroll endlessscroll

我对无限滚动有疑问。每次加载更多数据时,它都会返回顶部 View 。我想要的是 RecyclerView 在加载新数据时保持在最后一个位置。 我正在尝试实现此代码 https://github.com/codepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews-and-RecyclerView

这是无尽的滚动代码

public abstract class EndlessRecyclerViewScrollListener extends RecyclerView.OnScrollListener{
// The minimum amount of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 30;
// The current offset index of data you have loaded
private int currentPage = 0;
// The total number of items in the dataset after the last load
private int previousTotalItemCount = 0;
// True if we are still waiting for the last set of data to load.
private boolean loading = true;
// Sets the starting page index
private int startingPageIndex = 0;

RecyclerView.LayoutManager mLayoutManager;

public EndlessRecyclerViewScrollListener(LinearLayoutManager layoutManager) {
    this.mLayoutManager = layoutManager;
}

public int getLastVisibleItem(int[] lastVisibleItemPositions) {
    int maxSize = 0;
    for (int i = 0; i < lastVisibleItemPositions.length; i++) {
        if (i == 0) {
            maxSize = lastVisibleItemPositions[i];
        }
        else if (lastVisibleItemPositions[i] > maxSize) {
            maxSize = lastVisibleItemPositions[i];
        }
    }
    return maxSize;
}

// This happens many times a second during a scroll, so be wary of the code you place here.
// We are given a few useful parameters to help us work out if we need to load some more data,
// but first we check if we are waiting for the previous load to finish.
@Override
public void onScrolled(RecyclerView view, int dx, int dy) {
    int lastVisibleItemPosition = 0;
    int totalItemCount = mLayoutManager.getItemCount();

    if (mLayoutManager instanceof LinearLayoutManager) {
        lastVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition();
    }

    // If the total item count is zero and the previous isn't, assume the
    // list is invalidated and should be reset back to initial state

    if (totalItemCount < previousTotalItemCount) {
        this.currentPage = this.startingPageIndex;
        this.previousTotalItemCount = totalItemCount;
        if (totalItemCount == 0) {
            this.loading = true;
        }
    }
    // If it’s still loading, we check to see if the dataset count has
    // changed, if so we conclude it has finished loading and update the current page
    // number and total item count.
    if (loading && (totalItemCount > previousTotalItemCount)) {
        loading = false;
        previousTotalItemCount = totalItemCount;
    }

    // If it isn’t currently loading, we check to see if we have breached
    // the visibleThreshold and need to reload more data.
    // If we do need to reload some more data, we execute onLoadMore to fetch the data.
    // threshold should reflect how many total columns there are too
    if (!loading && (lastVisibleItemPosition + visibleThreshold) > totalItemCount) {
        currentPage++;
        onLoadMore(currentPage, totalItemCount, view);
        loading = true;
    }
}

// Call this method whenever performing new searches
public void resetState() {
    this.currentPage = this.startingPageIndex;
    this.previousTotalItemCount = 0;
    this.loading = true;
}

// Defines the process for actually loading more data based on page
public abstract void onLoadMore(int page, int totalItemsCount, RecyclerView view);

创建 View

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View view = inflater.inflate(R.layout.booking_fragment, container, false);

    recyclerView = (RecyclerView) view.findViewById(R.id.bookingRecyclerView);
    linearLayoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(linearLayoutManager);
    recyclerView.setHasFixedSize(true);

scrollListener = new EndlessRecyclerViewScrollListener(linearLayoutManager) {
        @Override
        public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
            // Triggered only when new data needs to be appended to the list
            // Add whatever code is needed to append new items to the bottom of the list


            if(CurrentStatus.equals("notSearch")){
                if (current < Integer.parseInt(TP)) {
                    current++;
                    loadMoreBookings(sort);
                }
                else if(current == Integer.parseInt(TP)){
                    Toast.makeText(getActivity(),"No More Data to Load", Toast.LENGTH_SHORT).show();
                }
            }else{
                if (current < Integer.parseInt(TP)) {
                    current++;
                    searchMoreBookings(search, sort);
                }
                else if(current == Integer.parseInt(TP)){
                    Toast.makeText(getActivity(),"No More Data to Be Load", Toast.LENGTH_SHORT).show();
                }
            }
        }

加载更多预订

private void loadMoreBookings(final String sort){
    CurrentStatus = "notSearch";
    final ProgressDialog progressDialog = new ProgressDialog(getActivity());
    progressDialog.setMessage("Please Wait While Retrieving Data");
    progressDialog.setCancelable(false);
    progressDialog.show();
    StringRequest requesting = new StringRequest(Request.Method.POST, URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String JSONString) {
                    progressDialog.dismiss();
                    try{
                        JSONObject jsonTP = new JSONObject(JSONString);
                        JSONArray jsonArrayB = jsonTP.getJSONArray("Data");
                        for(int i = 0; i < jsonArrayB.length(); i++){
                            JSONObject o = jsonArrayB.getJSONObject(i);
                            Booking list = new Booking(
                                    o.getString("bookID"),
                                    o.getString("userEmail"),
                                    o.getString("paymentMethod"),
                                    o.getString("paymentStatus"),
                                    o.getString("totalPrice"),
                                    o.getString(String.valueOf("securityCode")),
                                    o.getString(String.valueOf("travelDate")),
                                    o.getString("paymentID"),
                                    o.getString("userFN"),
                                    o.getString("userLN"),
                                    o.getString(String.valueOf("createdAt")),
                                    o.getString("tTM"),
                                    o.getString("messageToCustomer"),
                                    o.getString("messageFromMerchant"),
                                    o.getString("wCN"),
                                    o.getString("wLocation"),
                                    o.getString("wWebsite")
                            );
                            listBooking.add(list);
                            count++;
                        }
                        Toast.makeText(getActivity(), "Data : "+count, Toast.LENGTH_SHORT).show();
                        adapter = new BookingAdapter(listBooking,getActivity());
                        recyclerView.setAdapter(adapter);
                    } catch (JSONException e) {
                        loadMoreBookings(sort);
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressDialog.dismiss();
                    Toast.makeText(getActivity().getApplicationContext(), "Failed To Retrieve Data. Please Try Again.",Toast.LENGTH_LONG).show();
                }
            }
    ){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> params = new HashMap<String, String>();
            String user = getActivity().getIntent().getStringExtra("username");
            params.put("username", user);
            params.put("currentpage", String.valueOf(current));
            params.put("sorting", sort);
            return params;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
    requestQueue.add(requesting);
}

最佳答案

onCreateView() 中设置适配器

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.booking_fragment, container, false);

        recyclerView = (RecyclerView) view.findViewById(R.id.bookingRecyclerView);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);

 adapter = new BookingAdapter(listBooking,getActivity());
                            recyclerView.setAdapter(adapter);

    scrollListener = new EndlessRecyclerViewScrollListener(linearLayoutManager) {
            @Override
            public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
                // Triggered only when new data needs to be appended to the list
                // Add whatever code is needed to append new items to the bottom of the list


                if(CurrentStatus.equals("notSearch")){
                    if (current < Integer.parseInt(TP)) {
                        current++;
                        loadMoreBookings(sort);
                    }
                    else if(current == Integer.parseInt(TP)){
                        Toast.makeText(getActivity(),"No More Data to Load", Toast.LENGTH_SHORT).show();
                    }
                }else{
                    if (current < Integer.parseInt(TP)) {
                        current++;
                        searchMoreBookings(search, sort);
                    }
                    else if(current == Integer.parseInt(TP)){
                        Toast.makeText(getActivity(),"No More Data to Be Load", Toast.LENGTH_SHORT).show();
                    }
                }
            }

不要每次都设置适配器,只通知插入位置的适配器项。

private void loadMoreBookings(final String sort){
    CurrentStatus = "notSearch";
    final ProgressDialog progressDialog = new ProgressDialog(getActivity());
    progressDialog.setMessage("Please Wait While Retrieving Data");
    progressDialog.setCancelable(false);
    progressDialog.show();
    StringRequest requesting = new StringRequest(Request.Method.POST, URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String JSONString) {
                    progressDialog.dismiss();
                    try{
                        JSONObject jsonTP = new JSONObject(JSONString);
                        JSONArray jsonArrayB = jsonTP.getJSONArray("Data");
                        for(int i = 0; i < jsonArrayB.length(); i++){
                            JSONObject o = jsonArrayB.getJSONObject(i);
                            Booking list = new Booking(
                                    o.getString("bookID"),
                                    o.getString("userEmail"),
                                    o.getString("paymentMethod"),
                                    o.getString("paymentStatus"),
                                    o.getString("totalPrice"),
                                    o.getString(String.valueOf("securityCode")),
                                    o.getString(String.valueOf("travelDate")),
                                    o.getString("paymentID"),
                                    o.getString("userFN"),
                                    o.getString("userLN"),
                                    o.getString(String.valueOf("createdAt")),
                                    o.getString("tTM"),
                                    o.getString("messageToCustomer"),
                                    o.getString("messageFromMerchant"),
                                    o.getString("wCN"),
                                    o.getString("wLocation"),
                                    o.getString("wWebsite")
                            );
                            listBooking.add(list);
                            adapter.notifyItemInserted(count);
                            count++;
                        }
                        Toast.makeText(getActivity(), "Data : "+count, Toast.LENGTH_SHORT).show();

                    } catch (JSONException e) {
                        loadMoreBookings(sort);
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressDialog.dismiss();
                    Toast.makeText(getActivity().getApplicationContext(), "Failed To Retrieve Data. Please Try Again.",Toast.LENGTH_LONG).show();
                }
            }
    ){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> params = new HashMap<String, String>();
            String user = getActivity().getIntent().getStringExtra("username");
            params.put("username", user);
            params.put("currentpage", String.valueOf(current));
            params.put("sorting", sort);
            return params;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
    requestQueue.add(requesting);
}

关于android - Endless Scroll RecyclerView 总是返回顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46046373/

相关文章:

java - 进度对话框阻止内容 View 切换?

android - Smack 中的聊天标记 (XEP-0333)

java - 检测到不一致。无效的项目位置 0(偏移量 :-1). .. RecyclerView 在删除第一个项目时崩溃

android - recyclerView.addOnScrollListener - "retrofit pagination with MVVM"正在加载相同的响应/列表

flutter - 如何将 infinite_scroll_pagination 用于 bloc 模式

javascript - 如何在无限滚动加载新页面后请求类似 Tumblr 的按钮状态

java - 奇怪的错误 : Skipping entry 0x106000d in package table 0 because it is not complex

android-我如何在 "Android"的谷歌地图中实现集群管理器时获得确切的数字请给我正确的答案,如果有人有...?

android - 在顶部更新时 RecyclerView 崩溃

android - 将类转换为Kotlin的子类