Search in sources :

Example 11 with AdapterDataObserver

use of androidx.recyclerview.widget.RecyclerView.AdapterDataObserver in project epoxy by airbnb.

the class OnModelBindListenerTest method nullBindListenerChangesHashCode.

@Test
public void nullBindListenerChangesHashCode() {
    TestController controller = new TestController();
    AdapterDataObserver observerMock = mock(AdapterDataObserver.class);
    controller.getAdapter().registerAdapterDataObserver(observerMock);
    ModelWithClickListener_ model = new ModelWithClickListener_();
    controller.buildWithModel(model);
    verify(observerMock).onItemRangeInserted(eq(0), eq(1));
    model = new ModelWithClickListener_();
    model.onBind(new BindListener());
    controller.buildWithModel(model);
    model = new ModelWithClickListener_();
    model.onBind(null);
    controller.buildWithModel(model);
    verify(observerMock, times(2)).onItemRangeChanged(eq(0), eq(1), any());
}
Also used : ModelWithClickListener_(com.airbnb.epoxy.integrationtest.ModelWithClickListener_) AdapterDataObserver(androidx.recyclerview.widget.RecyclerView.AdapterDataObserver) Test(org.junit.Test)

Example 12 with AdapterDataObserver

use of androidx.recyclerview.widget.RecyclerView.AdapterDataObserver in project epoxy by airbnb.

the class OnModelBindListenerTest method newUnbindListenerDoesNotChangHashCode.

@Test
public void newUnbindListenerDoesNotChangHashCode() {
    TestController controller = new TestController();
    AdapterDataObserver observerMock = mock(AdapterDataObserver.class);
    controller.getAdapter().registerAdapterDataObserver(observerMock);
    ModelWithClickListener_ model = new ModelWithClickListener_();
    controller.buildWithModel(model);
    verify(observerMock).onItemRangeInserted(eq(0), eq(1));
    model = new ModelWithClickListener_();
    model.onUnbind(new UnbindListener());
    controller.buildWithModel(model);
    model = new ModelWithClickListener_();
    model.onUnbind(new UnbindListener());
    controller.buildWithModel(model);
    verify(observerMock).onItemRangeChanged(eq(0), eq(1), any());
}
Also used : ModelWithClickListener_(com.airbnb.epoxy.integrationtest.ModelWithClickListener_) AdapterDataObserver(androidx.recyclerview.widget.RecyclerView.AdapterDataObserver) Test(org.junit.Test)

Example 13 with AdapterDataObserver

use of androidx.recyclerview.widget.RecyclerView.AdapterDataObserver in project epoxy by airbnb.

the class OnModelBindListenerTest method newBindListenerDoesNotChangeHashCode.

@Test
public void newBindListenerDoesNotChangeHashCode() {
    TestController controller = new TestController();
    AdapterDataObserver observerMock = mock(AdapterDataObserver.class);
    controller.getAdapter().registerAdapterDataObserver(observerMock);
    ModelWithClickListener_ model = new ModelWithClickListener_();
    controller.buildWithModel(model);
    verify(observerMock).onItemRangeInserted(eq(0), eq(1));
    model = new ModelWithClickListener_();
    model.onBind(new BindListener());
    controller.buildWithModel(model);
    model = new ModelWithClickListener_();
    model.onBind(new BindListener());
    controller.buildWithModel(model);
    verify(observerMock).onItemRangeChanged(eq(0), eq(1), any());
}
Also used : ModelWithClickListener_(com.airbnb.epoxy.integrationtest.ModelWithClickListener_) AdapterDataObserver(androidx.recyclerview.widget.RecyclerView.AdapterDataObserver) Test(org.junit.Test)

Example 14 with AdapterDataObserver

use of androidx.recyclerview.widget.RecyclerView.AdapterDataObserver in project epoxy by airbnb.

the class OnModelBindListenerTest method unbindListenerChangesHashCode.

@Test
public void unbindListenerChangesHashCode() {
    TestController controller = new TestController();
    AdapterDataObserver observerMock = mock(AdapterDataObserver.class);
    controller.getAdapter().registerAdapterDataObserver(observerMock);
    ModelWithClickListener_ model = new ModelWithClickListener_();
    controller.buildWithModel(model);
    verify(observerMock).onItemRangeInserted(eq(0), eq(1));
    // shouldn't change
    model = new ModelWithClickListener_();
    model.onUnbind(null);
    controller.buildWithModel(model);
    verify(observerMock, never()).onItemRangeChanged(eq(0), eq(1), any());
    model = new ModelWithClickListener_();
    UnbindListener listener1 = new UnbindListener();
    model.onUnbind(listener1);
    controller.buildWithModel(model);
    verify(observerMock, times(1)).onItemRangeChanged(eq(0), eq(1), any());
    model = new ModelWithClickListener_();
    model.onUnbind(listener1);
    controller.buildWithModel(model);
    verify(observerMock, times(1)).onItemRangeChanged(eq(0), eq(1), any());
}
Also used : ModelWithClickListener_(com.airbnb.epoxy.integrationtest.ModelWithClickListener_) AdapterDataObserver(androidx.recyclerview.widget.RecyclerView.AdapterDataObserver) Test(org.junit.Test)

Example 15 with AdapterDataObserver

use of androidx.recyclerview.widget.RecyclerView.AdapterDataObserver in project apps-android-commons by commons-app.

the class ContributionsListFragment method initRecyclerView.

private void initRecyclerView() {
    final GridLayoutManager layoutManager = new GridLayoutManager(getContext(), getSpanCount(getResources().getConfiguration().orientation));
    rvContributionsList.setLayoutManager(layoutManager);
    // Setting flicker animation of recycler view to false.
    final ItemAnimator animator = rvContributionsList.getItemAnimator();
    if (animator instanceof SimpleItemAnimator) {
        ((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
    }
    contributionsListPresenter.setup(userName, Objects.equals(sessionManager.getUserName(), userName));
    contributionsListPresenter.contributionList.observe(getViewLifecycleOwner(), list -> {
        contributionsSize = list.size();
        adapter.submitList(list);
        if (callback != null) {
            callback.notifyDataSetChanged();
        }
    });
    rvContributionsList.setAdapter(adapter);
    adapter.registerAdapterDataObserver(new AdapterDataObserver() {

        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            super.onItemRangeInserted(positionStart, itemCount);
            if (itemCount > 0 && positionStart == 0) {
                if (adapter.getContributionForPosition(positionStart) != null) {
                    rvContributionsList.scrollToPosition(// Newly upload items are always added to the top
                    0);
                }
            }
        }

        /**
         * Called whenever items in the list have changed
         * Calls viewPagerNotifyDataSetChanged() that will notify the viewpager
         */
        @Override
        public void onItemRangeChanged(final int positionStart, final int itemCount) {
            super.onItemRangeChanged(positionStart, itemCount);
            if (callback != null) {
                callback.viewPagerNotifyDataSetChanged();
            }
        }
    });
    // Fab close on touch outside (Scrolling or taping on item triggers this action).
    rvContributionsList.addOnItemTouchListener(new OnItemTouchListener() {

        /**
         * Silently observe and/or take over touch events sent to the RecyclerView before
         * they are handled by either the RecyclerView itself or its child views.
         */
        @Override
        public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
            if (e.getAction() == MotionEvent.ACTION_DOWN) {
                if (isFabOpen) {
                    animateFAB(isFabOpen);
                }
            }
            return false;
        }

        /**
         * Process a touch event as part of a gesture that was claimed by returning true
         * from a previous call to {@link #onInterceptTouchEvent}.
         *
         * @param rv
         * @param e  MotionEvent describing the touch event. All coordinates are in the
         *           RecyclerView's coordinate system.
         */
        @Override
        public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
        // required abstract method DO NOT DELETE
        }

        /**
         * Called when a child of RecyclerView does not want RecyclerView and its ancestors
         * to intercept touch events with {@link ViewGroup#onInterceptTouchEvent(MotionEvent)}.
         *
         * @param disallowIntercept True if the child does not want the parent to intercept
         *                          touch events.
         */
        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
        // required abstract method DO NOT DELETE
        }
    });
}
Also used : SimpleItemAnimator(androidx.recyclerview.widget.SimpleItemAnimator) ItemAnimator(androidx.recyclerview.widget.RecyclerView.ItemAnimator) OnItemTouchListener(androidx.recyclerview.widget.RecyclerView.OnItemTouchListener) SimpleItemAnimator(androidx.recyclerview.widget.SimpleItemAnimator) GridLayoutManager(androidx.recyclerview.widget.GridLayoutManager) RecyclerView(androidx.recyclerview.widget.RecyclerView) AdapterDataObserver(androidx.recyclerview.widget.RecyclerView.AdapterDataObserver) MotionEvent(android.view.MotionEvent)

Aggregations

AdapterDataObserver (androidx.recyclerview.widget.RecyclerView.AdapterDataObserver)15 Test (org.junit.Test)14 ModelWithClickListener_ (com.airbnb.epoxy.integrationtest.ModelWithClickListener_)8 ArrayList (java.util.ArrayList)3 ModelWithDataBindingBindingModel_ (com.airbnb.epoxy.integrationtest.ModelWithDataBindingBindingModel_)2 MotionEvent (android.view.MotionEvent)1 View (android.view.View)1 OnClickListener (android.view.View.OnClickListener)1 GridLayoutManager (androidx.recyclerview.widget.GridLayoutManager)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 ItemAnimator (androidx.recyclerview.widget.RecyclerView.ItemAnimator)1 OnItemTouchListener (androidx.recyclerview.widget.RecyclerView.OnItemTouchListener)1 SimpleItemAnimator (androidx.recyclerview.widget.SimpleItemAnimator)1