Java Code Examples for androidx.recyclerview.widget.RecyclerView#AdapterDataObserver

The following examples show how to use androidx.recyclerview.widget.RecyclerView#AdapterDataObserver . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestUtils.java    From FeatureAdapter with Apache License 2.0 6 votes vote down vote up
/**
 * Fixes internal dependencies to android.database.Observable so that a RecyclerView.Adapter can
 * be tested using regular unit tests while verifying changes to the data.
 *
 * <p>Pulled from:
 * https://github.com/badoo/Chateau/blob/master/ExampleApp/src/test/java/com/badoo/chateau/example/ui/utils/TestUtils.java
 */
public static RecyclerView.AdapterDataObserver fixAdapterForTesting(RecyclerView.Adapter adapter)
    throws NoSuchFieldException, IllegalAccessException {
  // Observables are not mocked by default so we need to hook the adapter up to an observer so we can track changes
  Field observableField = RecyclerView.Adapter.class.getDeclaredField("mObservable");
  observableField.setAccessible(true);
  Object observable = observableField.get(adapter);
  Field observersField = Observable.class.getDeclaredField("mObservers");
  observersField.setAccessible(true);
  final ArrayList<Object> observers = new ArrayList<>();
  RecyclerView.AdapterDataObserver dataObserver =
      createMock(RecyclerView.AdapterDataObserver.class);
  observers.add(dataObserver);
  observersField.set(observable, observers);
  return dataObserver;
}
 
Example 2
Source File: BaseMVPActivity.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 注册 Adapter 观察者
 * @param recyclerView        {@link RecyclerView}
 * @param adapterDataObserver Adapter 观察者
 * @param isRefAdapter        是否刷新适配器
 */
public void registerAdapterDataObserver(RecyclerView recyclerView, RecyclerView.AdapterDataObserver adapterDataObserver, boolean isRefAdapter) {
    if (recyclerView != null) {
        RecyclerView.Adapter adapter = recyclerView.getAdapter();
        if (adapter != null) {
            adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
                @Override
                public void onChanged() {
                    super.onChanged();
                    // 获取数据总数
                    int itemCount = adapter.getItemCount();
                    // 如果为 null 特殊处理
                    ViewUtils.reverseVisibilitys(itemCount != 0, vid_ba_content_linear, vid_ba_state_linear);
                    // 判断是否不存在数据
                    if (itemCount == 0) {
                        stateLayout.showEmptyData();
                    }

                    if (adapterDataObserver != null) {
                        adapterDataObserver.onChanged();
                    }
                }
            });
            // 刷新适配器
            if (isRefAdapter) adapter.notifyDataSetChanged();
        }
    }
}
 
Example 3
Source File: AbstractPagerFragment.java    From cloudinary_android with MIT License 5 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_pager_page, container, false);

    recyclerView = (RecyclerView) rootView.findViewById(R.id.mainGallery);
    recyclerView.setHasFixedSize(true);
    ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
    RecyclerView.LayoutManager layoutManager = getLayoutManager(getContext());
    recyclerView.setLayoutManager(layoutManager);
    emptyView = rootView.findViewById(R.id.emptyListView);
    dividerSize = getResources().getDimensionPixelSize(R.dimen.grid_divider_width);
    addItemDecoration(recyclerView);
    observer = new RecyclerView.AdapterDataObserver() {
        @Override
        public void onChanged() {
            super.onChanged();
            emptyView.setVisibility(recyclerView.getAdapter().getItemCount() == 0 ? View.VISIBLE : View.INVISIBLE);
        }

        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            super.onItemRangeInserted(positionStart, itemCount);
            emptyView.setVisibility(recyclerView.getAdapter().getItemCount() == 0 ? View.VISIBLE : View.INVISIBLE);

        }

        @Override
        public void onItemRangeRemoved(int positionStart, int itemCount) {
            super.onItemRangeRemoved(positionStart, itemCount);
            emptyView.setVisibility(recyclerView.getAdapter().getItemCount() == 0 ? View.VISIBLE : View.INVISIBLE);

        }
    };

    return rootView;
}
 
Example 4
Source File: CompositeRecyclerAdapter.java    From recyclerview-adapters with Apache License 2.0 5 votes vote down vote up
@Override
public void bindParentAdapter(@Nullable CompositeRecyclerAdapter adapter, @Nullable RecyclerView.AdapterDataObserver dataObserver) {
    if (hasParentAdapter()) {
        throw new IllegalStateException("Adapter already has parentAdapter.");
    }
    parentAdapter = new WeakReference<>(adapter);
    registerAdapterDataObserver(dataObserver);
}
 
Example 5
Source File: BaseLocalAdapter.java    From recyclerview-adapters with Apache License 2.0 5 votes vote down vote up
@Override
public void bindParentAdapter(@Nullable CompositeRecyclerAdapter adapter, @Nullable RecyclerView.AdapterDataObserver dataObserver) {
    if (hasParentAdapter()) {
        throw new IllegalStateException("Adapter already has parentAdapter.");
    }
    parentAdapter = new WeakReference<>(adapter);
    registerAdapterDataObserver(dataObserver);
}
 
Example 6
Source File: BaseActivity.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 注册 Adapter 观察者
 * @param recyclerView        {@link RecyclerView}
 * @param adapterDataObserver Adapter 观察者
 * @param isRefAdapter        是否刷新适配器
 */
public void registerAdapterDataObserver(RecyclerView recyclerView, RecyclerView.AdapterDataObserver adapterDataObserver, boolean isRefAdapter) {
    if (recyclerView != null) {
        RecyclerView.Adapter adapter = recyclerView.getAdapter();
        if (adapter != null) {
            adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
                @Override
                public void onChanged() {
                    super.onChanged();
                    // 获取数据总数
                    int itemCount = adapter.getItemCount();
                    // 如果为 null 特殊处理
                    ViewUtils.reverseVisibilitys(itemCount != 0, vid_ba_content_linear, vid_ba_state_linear);
                    // 判断是否不存在数据
                    if (itemCount == 0) {
                        stateLayout.showEmptyData();
                    }

                    if (adapterDataObserver != null) {
                        adapterDataObserver.onChanged();
                    }
                }
            });
            // 刷新适配器
            if (isRefAdapter) adapter.notifyDataSetChanged();
        }
    }
}
 
Example 7
Source File: BaseAnimationAdapter.java    From PictureSelector with Apache License 2.0 4 votes vote down vote up
@Override
public void registerAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
    super.registerAdapterDataObserver(observer);
    mAdapter.registerAdapterDataObserver(observer);
}
 
Example 8
Source File: BaseAnimationAdapter.java    From PictureSelector with Apache License 2.0 4 votes vote down vote up
@Override
public void unregisterAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
    super.unregisterAdapterDataObserver(observer);
    mAdapter.unregisterAdapterDataObserver(observer);
}
 
Example 9
Source File: WrapperAdapter.java    From OmegaRecyclerView with MIT License 4 votes vote down vote up
@Override
public void registerAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
    mChildAdapter.registerAdapterDataObserver(observer);
}
 
Example 10
Source File: WrapperAdapter.java    From OmegaRecyclerView with MIT License 4 votes vote down vote up
@Override
public void unregisterAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
    mChildAdapter.unregisterAdapterDataObserver(observer);
}
 
Example 11
Source File: CircleIndicator3.java    From CircleIndicator with Apache License 2.0 4 votes vote down vote up
public RecyclerView.AdapterDataObserver getAdapterDataObserver() {
    return mAdapterDataObserver;
}
 
Example 12
Source File: BaseFilter.java    From MusicBobber with MIT License 4 votes vote down vote up
public RecyclerView.AdapterDataObserver getAdapterDataObserver() {
    return adapterDataObserver;
}
 
Example 13
Source File: ViewPager2Attacher.java    From ScrollingPagerIndicator with Apache License 2.0 4 votes vote down vote up
@Override
public void attachToPager(@NonNull final ScrollingPagerIndicator indicator, @NonNull final ViewPager2 pager) {
    attachedAdapter = pager.getAdapter();
    if (attachedAdapter == null) {
        throw new IllegalStateException("Set adapter before call attachToPager() method");
    }

    this.pager = pager;

    updateIndicatorDotsAndPosition(indicator);

    dataSetObserver = new RecyclerView.AdapterDataObserver() {
        @Override
        public void onChanged() {
            indicator.reattach();
        }
    };
    attachedAdapter.registerAdapterDataObserver(dataSetObserver);

    onPageChangeListener = new ViewPager2.OnPageChangeCallback() {

        boolean idleState = true;

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixel) {
            updateIndicatorOnPagerScrolled(indicator, position, positionOffset);
        }

        @Override
        public void onPageSelected(int position) {
            if (idleState) {
                updateIndicatorDotsAndPosition(indicator);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            idleState = state == ViewPager2.SCROLL_STATE_IDLE;
        }
    };

    pager.registerOnPageChangeCallback(onPageChangeListener);
}
 
Example 14
Source File: RecyclerViewAttacher.java    From ScrollingPagerIndicator with Apache License 2.0 4 votes vote down vote up
@Override
public void attachToPager(@NonNull final ScrollingPagerIndicator indicator, @NonNull final RecyclerView pager) {
    if (!(pager.getLayoutManager() instanceof LinearLayoutManager)) {
        throw new IllegalStateException("Only LinearLayoutManager is supported");
    }
    if (pager.getAdapter() == null) {
        throw new IllegalStateException("RecyclerView has not Adapter attached");
    }
    this.layoutManager = (LinearLayoutManager) pager.getLayoutManager();
    this.recyclerView = pager;
    this.attachedAdapter = pager.getAdapter();
    this.indicator = indicator;

    dataObserver = new RecyclerView.AdapterDataObserver() {
        @Override
        public void onChanged() {
            indicator.setDotCount(attachedAdapter.getItemCount());
            updateCurrentOffset();
        }

        @Override
        public void onItemRangeChanged(int positionStart, int itemCount) {
            onChanged();
        }

        @Override
        public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
            onChanged();
        }

        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            onChanged();
        }

        @Override
        public void onItemRangeRemoved(int positionStart, int itemCount) {
            onChanged();
        }

        @Override
        public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
            onChanged();
        }
    };
    attachedAdapter.registerAdapterDataObserver(dataObserver);
    indicator.setDotCount(attachedAdapter.getItemCount());
    updateCurrentOffset();

    scrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            if (newState == RecyclerView.SCROLL_STATE_IDLE && isInIdleState()) {
                int newPosition = findCompletelyVisiblePosition();
                if (newPosition != RecyclerView.NO_POSITION) {
                    indicator.setDotCount(attachedAdapter.getItemCount());
                    if (newPosition < attachedAdapter.getItemCount()) {
                        indicator.setCurrentPosition(newPosition);
                    }
                }
            }
        }

        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            updateCurrentOffset();
        }
    };

    recyclerView.addOnScrollListener(scrollListener);
}
 
Example 15
Source File: RecyclerConverterAdapter.java    From power-adapters with Apache License 2.0 4 votes vote down vote up
@Override
public final void registerAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
    super.registerAdapterDataObserver(observer);
    mAdapterDataObservers.add(observer);
    updateObserver();
}
 
Example 16
Source File: CircleIndicator2.java    From CircleIndicator with Apache License 2.0 4 votes vote down vote up
public RecyclerView.AdapterDataObserver getAdapterDataObserver() {
    return mAdapterDataObserver;
}
 
Example 17
Source File: AdapterWrapper.java    From SwipeRecyclerView with Apache License 2.0 4 votes vote down vote up
@Override
public void registerAdapterDataObserver(@NonNull RecyclerView.AdapterDataObserver observer) {
    super.registerAdapterDataObserver(observer);
}
 
Example 18
Source File: AdapterWrapper.java    From SwipeRecyclerView with Apache License 2.0 4 votes vote down vote up
@Override
public void unregisterAdapterDataObserver(@NonNull RecyclerView.AdapterDataObserver observer) {
    super.unregisterAdapterDataObserver(observer);
}
 
Example 19
Source File: AnimationAdapter.java    From recyclerview-animators with Apache License 2.0 4 votes vote down vote up
@Override public void registerAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
  super.registerAdapterDataObserver(observer);
  mAdapter.registerAdapterDataObserver(observer);
}
 
Example 20
Source File: LocalAdapter.java    From recyclerview-adapters with Apache License 2.0 votes vote down vote up
void bindParentAdapter(@Nullable CompositeRecyclerAdapter adapter, @Nullable RecyclerView.AdapterDataObserver dataObserver);