【问题标题】:JSONArray Parsing in RecyclerView using volley android使用 volley android 在 RecyclerView 中解析 JSONArray
【发布时间】:2017-12-04 05:54:35
【问题描述】:

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

我的 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 来加载下一页数据。

【问题讨论】:

  • 最简单的方法是在onBindViewHolder 检查if((list.size()==(position-1))) 如果为真,然后回调活动或片段并将下一页添加到适配器中的列表。
  • 你用next_page_url回调了吗?
  • 在将下一页添加到您的适配器后不要忘记致电notifyDataSetChanged()
  • 谢谢,成功了
  • 不客气。

标签: android json android-recyclerview android-volley


【解决方案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
}

【讨论】:

    【解决方案2】:

    我创建接口onbottom 并检查recyclerview 是否在底部,然后加载更多数据。意味着 count 将首先发布到 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;
    
    
    
    
                }
            });
    

    【讨论】:

    • 当我们到达recyclerview 的底部时,我怎么能打电话给next page url。这就是我想要的。
    • 你好 Waleed Asim,它的工作,但问题是它再次重置了我的recyclerview,我的意思是当我到达底部时它会加载下一页数据但我再次到达 recyclerview 的顶部。每次当我到达底部时,它都会加载下一页数据,但一次又一次地将我扔到 recyclerview 的顶部。
    • @Dinesh 你在哪里调用 notifyDataSetChanged()?
    • 我在哪里以及如何调用 notifyDataSetChanged()?
    • 如果在循环中添加数据,只需在循环外调用它 youradapter.notifyDataSetChanged();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-12
    • 1970-01-01
    相关资源
    最近更新 更多