alibaba / Tangram-Android

Tangram is a modular UI solution for building native page dynamically including Tangram for Android, Tangram for iOS and even backend CMS. This project provides the sdk on Android.
http://tangram.pingguohe.net/
MIT License
4.11k stars 526 forks source link

有关自动加载更多会触发下标越界的问题 #38

Closed wht007 closed 6 years ago

wht007 commented 7 years ago

需求:我需要分页加载列表布局,分页加载的json { "type": "1", "load": "com.tmall.request.load.more", "loadType": 1, "items": [ ] } 然后在分页的监听里面做网络加载分页数据。 在实际中操作发现不能在分页加载的items的JsonArray中添加默认的数据类似 "items": [ { "type": 1 }, { "type": 1 } ] 这样的数据,添加后运行完分页的回调监听之后就直接崩溃了

FATAL EXCEPTION: main Process: com.tmall.wireless.tangram.example, PID: 21751 java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{3c5fc95d position=58 id=-1, oldPos=50, pLpos:50 scrap [attachedScrap] tmpDetached no parent} at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:4251) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4382) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363) at com.alibaba.android.vlayout.ExposeLinearLayoutManagerEx$LayoutState.next(ExposeLinearLayoutManagerEx.java:1607) at com.alibaba.android.vlayout.VirtualLayoutManager$LayoutStateWrapper.next(VirtualLayoutManager.java:978) at com.alibaba.android.vlayout.layout.GridLayoutHelper.layoutViews(GridLayoutHelper.java:317) at com.alibaba.android.vlayout.layout.BaseLayoutHelper.doLayout(BaseLayoutHelper.java:260) at com.alibaba.android.vlayout.VirtualLayoutManager.layoutChunk(VirtualLayoutManager.java:574) at com.alibaba.android.vlayout.ExposeLinearLayoutManagerEx.fill(ExposeLinearLayoutManagerEx.java:1140) at com.alibaba.android.vlayout.ExposeLinearLayoutManagerEx.onLayoutChildren(ExposeLinearLayoutManagerEx.java:358) at com.alibaba.android.vlayout.VirtualLayoutManager.onLayoutChildren(VirtualLayoutManager.java:398) at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2864) at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071) at android.view.View.layout(View.java:15734) at android.view.ViewGroup.layout(ViewGroup.java:5196) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) at android.widget.FrameLayout.onLayout(FrameLayout.java:514) at android.view.View.layout(View.java:15734) at android.view.ViewGroup.layout(ViewGroup.java:5196) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) at android.widget.FrameLayout.onLayout(FrameLayout.java:514) at android.view.View.layout(View.java:15734) at android.view.ViewGroup.layout(ViewGroup.java:5196) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) at android.widget.LinearLayout.onLayout(LinearLayout.java:1466) at android.view.View.layout(View.java:15734) at android.view.ViewGroup.layout(ViewGroup.java:5196) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) at android.widget.FrameLayout.onLayout(FrameLayout.java:514) at android.view.View.layout(View.java:15734) at android.view.ViewGroup.layout(ViewGroup.java:5196) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2151) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1875) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1092) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6050) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:805) at android.view.Choreographer.doCallbacks(Choreographer.java:618) at android.view.Choreographer.doFrame(Choreographer.java:588) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:791) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5418) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1037) at @com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)

longerian commented 7 years ago

代码是怎么调用的? 贴出来看看

wht007 commented 7 years ago
 //Step 5: add card load support if you have card that loading cells async
        //需要分页加载
        engine.addCardLoadSupport(new CardLoadSupport(new AsyncPageLoader() {
            @Override
            public void loadData(final int page, @NonNull final Card card, @NonNull final LoadedCallback callback) {
                card_ceshi = card;
                callback_ceshi = callback;
                com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
                jsonObject.put("page_index", page_index);
                jsonObject.put("page_count", "10");
                BaseHttpUtil baseHttpUtil = new BaseHttpUtil();
                HttpParams httpParams = new HttpParams();
                httpParams.put("data", jsonObject.toString());
                httpParams.put("sign", Constants.sign);
                baseHttpUtil.doPostString(url, httpParams, new HttpStringCallBack() {
                    @Override
                    public void onSuccess(Object result) {
                        LogUtils.d("result" + result.toString());
                        try {
                            JSONObject object = new JSONObject(result.toString());
                            if (object.getInt("state") == 1) {//1代表成功
                                JSONArray jsonArray = object.getJSONArray("data");
                                cs = engine.parseComponent(jsonArray);
                                if (page_index == 0) {
                                    adapter = engine.getGroupBasicAdapter();
                                    card.setCells(cs);
                                    adapter.refreshWithoutNotify();
                                    Range<Integer> range = adapter.getCardRange(card);
                                    adapter.notifyItemRemoved(range.getLower());
                                    adapter.notifyItemRangeInserted(range.getLower(), cs.size());
                                } else {
                                    card.addCells(cs);

                                }
                                //如果获取的数长度不大于0,就认为没有分页数据了
                                callback.finish(jsonArray.length() > 0);
                                //刷新数据和view
                                card.notifyDataChange();
                                page_index++;
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onFailure(int code, Throwable e) {
                        ToastUtils.show(getActivity(), "请求异常");
                        LogUtils.d("Throwable="+e.getMessage());
                    }
                });

            }
        }));
longerian commented 7 years ago

你这个请求回调是在主线程吗

wht007 commented 7 years ago

在UI线程里面。因为之前使用的本地配的json数据,所以我在json中设置了一些模拟数据,但是如果在分页回调里面在进行网络请求加载就会出现这个问题。 在实际应用中是不是不用再json数据中配置一些默认数据?

longerian commented 7 years ago

默认数据是不需要的。 如果已经存在默认数据,则用card.addCells(cs);去追加数据,刷新数据直接用card.notifyDataChange();

wht007 commented 7 years ago

好的。