JSONArray使用volley android在RecyclerView中解析

时间:2017-12-04 05:54:35

标签: android json android-recyclerview android-volley

我正在尝试使用JSONArray中的RecyclerView解析volley中的android,我每页有10个数据,当我们如何调用next page url时每次向下滚动recyclerview

我的JsonArray

"data": {
    "total": 146,
    "per_page": 10,
    "current_page": 1,
    "last_page": 15,
    "next_page_url": "http://www.xxxxxxxxxx.com/api/v1/auth/deal?page=2",
    "prev_page_url": null,
    "from": 1,
    "to": 10,
    "data": [
        {
            "id": "209",
            "ref": "101hd-209",
            "pid": "437",
            "name": "Office For Rent",
            "deal_mode": {
                "id": "128",
                "category_name": "Lease"
            },

我的recyclerview一次加载第一页数据,现在向下滚动我如何调用下一页page url来加载下一页数据。

2 个答案:

答案 0 :(得分:1)

将MainActivity替换为您的活动

//this code will be inside adapter in onBindViewHolder
if ((position+1) == dataList.size()){
        Log.d(TAG, "equal");
        ((MainActivity) context).loadNextPage(next_page_url);
}

//this code will be in MainActivity
public void loadNextPage(String pageUrl){
    //Here you make second page request
    //And add second page to list
    //then notify your adapter
}

答案 1 :(得分:0)

我创建了接口onbottom并检查Recyclerview是否位于底部,然后加载更多数据。表示计数将首先发布到API中它加载第一页数据意味着count = 1然后何时再次调用onbottom调用函数用count ++加载第二页数据

它与我面对的结构相同,这对我很好。

接口:

public interface OnBottomReachedListener {
    void onBottomReached(int position);

}

适配器:

OnBottomReachedListener onBottomReachedListener;
 public void setOnBottomReachedListener(OnBottomReachedListener onBottomReachedListener){

        this.onBottomReachedListener = onBottomReachedListener;
    }
 @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
       final PropertyInfo propertyInfo = propertyInfoList.get(position);

        if (position == propertyInfoList.size() - 1  ){

            onBottomReachedListener.onBottomReached(position);

        }
}

的活动:

adapter.setOnBottomReachedListener(new OnBottomReachedListener() {
            @Override
            public void onBottomReached(int position) {
                count++;
                String URl="http://www.xxxxxxxxxx.com/api/v1/auth/deal?

                page="+count;




            }
        });