Проблема с RecycleView

344
13 апреля 2017, 16:32

В тестовом приложении я пытаюсь работать с RecyclerView и прокручивать recycleview items синхронно. Однако во время горизонтальной прокрутки у меня появляется проблема, вероятно из за кэширования RecycleView и приложение дает сбой.

public class TestActivty extends AppCompatActivity {
    public static final String Tag = TestActivty.class.getName();
    int mCurX = 0;
    int countMainRecyleView = 0;
    int countItemRecyleView = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.v_activty);
        final RecyclerView mainRecyclerView = (RecyclerView) findViewById(R.id.activity_main);
        final LinearLayoutManager verticalLinearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mainRecyclerView.setLayoutManager(verticalLinearLayoutManager);
        final LayoutInflater layoutInflater = LayoutInflater.from(this);
        final RecyclerView.OnScrollListener masterOnScrollListener = new RecyclerView.OnScrollListener() {
            RecyclerView masterRecyclerView = null;
            RecyclerView.OnScrollListener scrollListener = this;
            @Override
            public void onScrollStateChanged(final RecyclerView recyclerView, final int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                        switch (newState) {
                            case RecyclerView.SCROLL_STATE_IDLE:
                                if (masterRecyclerView != null) {
                                    masterRecyclerView = null;
                                    final int firstVisibleItemPosition = verticalLinearLayoutManager.findFirstVisibleItemPosition();
                                    final int lastVisibleItemPosition = verticalLinearLayoutManager.findLastVisibleItemPosition();
                                    for (int i = firstVisibleItemPosition; i <= lastVisibleItemPosition; ++i) {
                                        RecyclerView horizontalRecyclerView = (RecyclerView) mainRecyclerView.findViewHolderForAdapterPosition(i).itemView;
                                        if (horizontalRecyclerView != recyclerView)
                                            horizontalRecyclerView.addOnScrollListener(scrollListener);
                                    }
                                }
                                break;
                            case RecyclerView.SCROLL_STATE_SETTLING:
                            case RecyclerView.SCROLL_STATE_DRAGGING:
                                if (masterRecyclerView == null) {
                                    masterRecyclerView = recyclerView;
                                    final int firstVisibleItemPosition = verticalLinearLayoutManager.findFirstVisibleItemPosition();
                                    final int lastVisibleItemPosition = verticalLinearLayoutManager.findLastVisibleItemPosition();
                                    for (int i = firstVisibleItemPosition; i <= lastVisibleItemPosition; ++i) {
                                        RecyclerView horizontalRecyclerView = (RecyclerView) mainRecyclerView.findViewHolderForAdapterPosition(i).itemView;
                                        if (horizontalRecyclerView != recyclerView)
                                            horizontalRecyclerView.removeOnScrollListener(scrollListener);
                                    }
                                }
                        }
            }
            @Override
            public void onScrolled(final RecyclerView recyclerView, final int dx, final int dy) {
                super.onScrolled(recyclerView, dx, dy);
                        mCurX += dx;
                         int firstVisibleItemPosition = verticalLinearLayoutManager.findFirstVisibleItemPosition();
                         int lastVisibleItemPosition = verticalLinearLayoutManager.findLastVisibleItemPosition();
                        for (int i = firstVisibleItemPosition; i <= lastVisibleItemPosition; ++i) {
                            RecyclerView horizontalRecyclerView = (RecyclerView) mainRecyclerView.findViewHolderForAdapterPosition(i).itemView;
                            if (horizontalRecyclerView != recyclerView)
                                horizontalRecyclerView.scrollBy(dx, dy);
                            horizontalRecyclerView.getRecycledViewPool().clear();
                        }
            }
        };
        mainRecyclerView.setAdapter(new RecyclerView.Adapter() {
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
                RecyclerView horizontalRecyclerView = (RecyclerView) layoutInflater.inflate(R.layout.horizontal_recycler_view, parent, false);
                horizontalRecyclerView.setLayoutManager(new LinearLayoutManager(TestActivty.this, LinearLayoutManager.HORIZONTAL, false));
                horizontalRecyclerView.addOnScrollListener(masterOnScrollListener);
                final RecyclerView.ViewHolder horizontalViewHolder = new RecyclerView.ViewHolder(horizontalRecyclerView) {
                };
                horizontalRecyclerView.setAdapter(new RecyclerView.Adapter() {
                    @Override
                    public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
                        return new RecyclerView.ViewHolder(layoutInflater.inflate(R.layout.single_item, parent, false)) {
                        };
                    }
                    @Override
                    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
                        ((TextView) holder.itemView).setText("horizontalRecyclerView:" + horizontalViewHolder.getAdapterPosition() + "\nitem:" + position);
                    }
                    @Override
                    public int getItemCount() {
                        return 50;
                    }
                });
                return horizontalViewHolder;
            }
            @Override
            public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
                RecyclerView recyclerView = (RecyclerView) holder.itemView;
                recyclerView.removeOnScrollListener(masterOnScrollListener);
                recyclerView.scrollToPosition(0);
                recyclerView.scrollBy(mCurX,0);
                recyclerView.addOnScrollListener(masterOnScrollListener);
                recyclerView.getAdapter().notifyDataSetChanged();
            }
            @Override
            public int getItemCount() {
                return 105;
            }
        });
    }
}

v_activty.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

horizontal_recycler_view.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

single_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="10dp"/>

Error logcat

java.lang.NullPointerException: Attempt to read from field 'android.view.View android.support.v7.widget.RecyclerView$ViewHolder.itemView' on a null object reference
at verjnakan.TestActivty$1.onScrolled(TestActivty.java:80)
at android.support.v7.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:3954)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:3115)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2917)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283)
at android.view.View.layout(View.java:16711)
at android.view.ViewGroup.layout(ViewGroup.java:5328)
at android.support.v7.widget.RecyclerView$LayoutManager.layoutDecorated(RecyclerView.java:7650)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1448)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1180)
at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1031)
at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:4061)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
at android.view.Choreographer.doCallbacks(Choreographer.java:590)
at android.view.Choreographer.doFrame(Choreographer.java:559)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
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:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

+++++++++++++++++++++++++

D/Error: ERR: stack=java.lang.StackOverflowError: stack size 8MB
                                                                         at android.view.View.invalidateInternal(View.java:12678)
                                                                         at android.view.View.invalidate(View.java:12668)
                                                                         at android.view.View.invalidateViewProperty(View.java:12788)
                                                                         at android.view.View.offsetLeftAndRight(View.java:12299)
                                                                         at android.support.v7.widget.RecyclerView.offsetChildrenHorizontal(RecyclerView.java:3888)
                                                                         at android.support.v7.widget.RecyclerView$LayoutManager.offsetChildrenHorizontal(RecyclerView.java:7228)
                                                                         at android.support.v7.widget.OrientationHelper$1.offsetChildren(OrientationHelper.java:229)
                                                                         at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1189)
                                                                         at android.support.v7.widget.LinearLayoutManager.scrollHorizontallyBy(LinearLayoutManager.java:1019)
                                                                         at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1525)
                                                                         at android.support.v7.widget.RecyclerView.scrollBy(RecyclerView.java:1436)
                                                                         at verjnakan.TestActivty$1.onScrolled(TestActivty.java:95)
                                                                         at android.support.v7.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:3954)
                                                                         at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1557)
                                                                         at android.support.v7.widget.RecyclerView.scrollBy(RecyclerView.java:1436)
                                                                         at verjnakan.TestActivty$1.onScrolled(TestActivty.java:95)
                                                                         at android.support.v7.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:3954)
                                                                         at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1557)
                                                                         at android.support.v7.widget.RecyclerView.scrollBy(RecyclerView.java:1436)
                                                                         at verjnakan.TestActivty$1.onScrolled(TestActivty.java:95)
                                                                         at android.support.v7.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:3954)
                                                                         at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1557)
                                                                         at android.support.v7.widget.RecyclerView.scrollBy(RecyclerView.java:1436)
                                                                         at verjnakan.TestActivty$1.onScrolled(TestActivty.java:95)
                                                                         at android.support.v7.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:3954)
                                                                         at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1557)
                                                                         at android.support.v7.widget.RecyclerView.scrollBy(RecyclerView.java:1436)
                                                                         at verjnakan.TestActivty$1.onScrolled(TestActivty.java:95)
                                                                         at android.support.v7.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:3954)
                                                                         at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1557)
                                                                         at android.support.v7.widget.RecyclerView.scrollBy(RecyclerView.java:1436)
                                                                         at verjnakan.TestActivty$1.onScrolled(TestActivty.java:95)
                                                                         at android.support.v7.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:3954)
                                                                         at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1557)
                                                                         at android.support.v7.widget.RecyclerView.scrollBy(RecyclerView.java:1436)
                                                                         at verjnakan.TestActivty$1.onScrolled(TestActivty.java:95)
                                                                         at android.support.v7.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:3954)
                                                                         at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1557)
                                                                         at android.support.v7.widget.RecyclerView.scrollBy(RecyclerView.java:1436)
                                                                         at verjnakan.TestActivty$1.onScrolled(TestActivty.java:95)
                                                                         at android.support.v7.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:3954)
                                                                         at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1557)
                                                                         at android.support.v7.widget.RecyclerView.scrollBy(RecyclerView.java:1436)
                                                                         at verjnakan.TestActivty$1.onScrolled(TestActivty.java:95)
                                                                         at android.support.v7.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:3954)
                                                                         at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1557)
                                                                         at android.support.v7.widget.RecyclerView.scrollBy(RecyclerView.java:1436)
                                                                         at verjnakan.TestActivty$1.onScrolled(TestActivty.java:95)
                                                                         at android.support.v7.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:3954)
                                                                         at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1557)
                                                                         at android.support.v7.widget.RecyclerView.scrollBy(RecyclerView.java:1436)
                                                                         at verjnakan.TestActivty$1.onScrolled(TestActivty.java:95)
                                                                      at android.sup..
Answer 1

У вас похоже бесконечный цикл, который заканчивается переполнением стека вызовов. Механизм такой:

  1. Вы скроллируете
  2. Вызывается onScrolled()
  3. Который вызывает scrollBy()
  4. Опять вызывается onScrolled()
  5. Он вызывает scrollBy()
  6. ...
  7. Капут стеку

Измените логику обработки скроллинга.

READ ALSO
Android и bitrix

Android и bitrix

Есть сайт на битриксе, к которому подключена база данныхЧерез сайт можно заказать еду (сайт службы доставки), заказ вносится в базу

180
Проблема с запуском UI тестов

Проблема с запуском UI тестов

Столкнулся с странной ситуациейПытаюсь разобраться с мержем дева в мою ветку с тестами

221
Java Apache POI Excel Перезапись

Java Apache POI Excel Перезапись

Вот до сюда все работает, если файла в папке нет он создает

339