Search in sources :

Example 1 with OnItemTouchListener

use of androidx.recyclerview.widget.RecyclerView.OnItemTouchListener 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

MotionEvent (android.view.MotionEvent)1 GridLayoutManager (androidx.recyclerview.widget.GridLayoutManager)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 AdapterDataObserver (androidx.recyclerview.widget.RecyclerView.AdapterDataObserver)1 ItemAnimator (androidx.recyclerview.widget.RecyclerView.ItemAnimator)1 OnItemTouchListener (androidx.recyclerview.widget.RecyclerView.OnItemTouchListener)1 SimpleItemAnimator (androidx.recyclerview.widget.SimpleItemAnimator)1