在thresold值刷新Android之后,关注endlessrecycleview中的问题

时间:2016-11-05 04:31:03

标签: android android-recyclerview recycler-adapter

我在代码

下面有一个endlessrecycleview检查
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

public abstract class EndlessRecyclerOnScrollListener extends
    RecyclerView.OnScrollListener {
public static String TAG = EndlessRecyclerOnScrollListener.class
        .getSimpleName();

private int previousTotal = 0;
private boolean loading = true;
private int visibleThreshold = 5;
int firstVisibleItem, visibleItemCount, totalItemCount;

private int current_page = 1;

private LinearLayoutManager mLinearLayoutManager;

public EndlessRecyclerOnScrollListener(
        LinearLayoutManager linearLayoutManager) {
    this.mLinearLayoutManager = linearLayoutManager;
}

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

    visibleItemCount = recyclerView.getChildCount();
    totalItemCount = mLinearLayoutManager.getItemCount();
    firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();

    if (loading) {
        if (totalItemCount > previousTotal) {
            loading = false;
            previousTotal = totalItemCount;
        }
    }
    if (!loading
            && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
        // End has been reached

        // Do something
        current_page++;

        onLoadMore(current_page);

        loading = true;
       }
   }

     public abstract void onLoadMore(int current_page);
}

在我的片段类中,我在上面的课程中调用

mRecyclerView.setOnScrollListener(new EndlessRecyclerOnScrollListener(
            mLayoutManager) {
        @Override
        public void onLoadMore(int current_page) {
            if (SolutionEnterprises.mGetRCAAcceptedListDatas.size() < 100) {
                from = from + limit;
                new getHomeTicketList(false).execute();
            }
        }
    });

当我在我的循环视图中滚动5个项目的时候运行上面的代码时它将调用agian我的web api它很好但是问题是当第5个项目scrooll recycleview foucs在第一个位置再次进行web api调用时那么如何解决这个问题呢?

1 个答案:

答案 0 :(得分:0)

you need to carefully with your adapter.

private ContractsAdapter contractListAdapter;

private void setAdapter(final ArrayList<ContractsModel> list) {
    if (contractListAdapter == null) {
            contractList = new ArrayList<>();
            contractList.addAll(list);
            contractListAdapter = new ContractsAdapter(this, contractList);
            rvContracts.setAdapter(contractListAdapter);
        } else {
            if (isLoadMore) {
                contractList.addAll(list);
                contractListAdapter.notifyDataSetChanged();
                isLoadMore = false;
            } else {
                contractList.clear();
                contractList.addAll(list);
                contractListAdapter.notifyDataSetChanged();
            }
        }
    }