下面列出了androidx.recyclerview.widget.RecyclerView#getItemAnimator ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void init() {
inflate(getContext(), R.layout.colorpicker_layout_preset_picker, this);
adapter = new PresetColorAdapter(DEFAULT_PRESETS).withListener(this);
RecyclerView recycler = findViewById(R.id.recycler);
recycler.setHasFixedSize(true);
RecyclerView.ItemAnimator animator = recycler.getItemAnimator();
if (animator instanceof SimpleItemAnimator)
((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
recycler.setLayoutManager(new GridLayoutManager(getContext(), 4));
recycler.setAdapter(adapter);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.mal_material_about_content, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.mal_recyclerview);
adapter = new MaterialAboutListAdapter(getViewTypeManager());
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
RecyclerView.ItemAnimator animator = recyclerView.getItemAnimator();
if (animator instanceof SimpleItemAnimator) {
((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
}
recyclerView.setAlpha(0f);
recyclerView.setTranslationY(20);
new ListTask(this).execute();
return rootView;
}
/**
* Process a change event.
* @param debug: string for debug usually the source of the call
* @param checkItemAnimator: true if it need to check if ItemAnimator is running
*/
private void processChangeEvent(String debug, boolean checkItemAnimator) {
final RecyclerView recyclerView = attachedRecyclerView;
if (recyclerView != null) {
final ItemAnimator itemAnimator = recyclerView.getItemAnimator();
if (checkItemAnimator && itemAnimator != null) {
// `itemAnimatorFinishedListener.onAnimationsFinished` will process visibility check
// - If the animations are running `onAnimationsFinished` will be invoked on animations end.
// - If the animations are not running `onAnimationsFinished` will be invoked right away.
if (itemAnimator.isRunning(itemAnimatorFinishedListener)) {
// If running process visibility now as `onAnimationsFinished` was not yet called
processChangeEventWithDetachedView(null, debug);
}
} else {
processChangeEventWithDetachedView(null, debug);
}
}
}
@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanner);
ButterKnife.bind(this);
final MaterialToolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle(R.string.app_name);
setSupportActionBar(toolbar);
// Create view model containing utility methods for scanning
scannerViewModel = new ViewModelProvider(this).get(ScannerViewModel.class);
scannerViewModel.getScannerState().observe(this, this::startScan);
// Configure the recycler view
final RecyclerView recyclerView = findViewById(R.id.recycler_view_ble_devices);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
final RecyclerView.ItemAnimator animator = recyclerView.getItemAnimator();
if (animator instanceof SimpleItemAnimator) {
((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
}
final DevicesAdapter adapter = new DevicesAdapter(this, scannerViewModel.getDevices());
adapter.setOnItemClickListener(this);
recyclerView.setAdapter(adapter);
}
/**
* Disables all the {@link RecyclerView} animations.
*/
public static void disableAnimations(@NonNull RecyclerView recyclerView) {
Preconditions.nonNull(recyclerView);
if(recyclerView.getItemAnimator() != null) {
recyclerView.setItemAnimator(null);
}
}
@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanner);
ButterKnife.bind(this);
// Create view model containing utility methods for scanning
mViewModel = new ViewModelProvider(this, mViewModelFactory).get(ScannerViewModel.class);
final Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle(R.string.title_scanner);
setSupportActionBar(toolbar);
//noinspection ConstantConditions
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (getIntent() != null) {
mScanWithProxyService = getIntent().getBooleanExtra(Utils.EXTRA_DATA_PROVISIONING_SERVICE, true);
if (mScanWithProxyService) {
getSupportActionBar().setSubtitle(R.string.sub_title_scanning_nodes);
} else {
getSupportActionBar().setSubtitle(R.string.sub_title_scanning_proxy_node);
}
}
// Configure the recycler view
final RecyclerView recyclerViewDevices = findViewById(R.id.recycler_view_ble_devices);
recyclerViewDevices.setLayoutManager(new LinearLayoutManager(this));
final DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerViewDevices.getContext(), DividerItemDecoration.VERTICAL);
recyclerViewDevices.addItemDecoration(dividerItemDecoration);
final SimpleItemAnimator itemAnimator = (SimpleItemAnimator) recyclerViewDevices.getItemAnimator();
if (itemAnimator != null) itemAnimator.setSupportsChangeAnimations(false);
final DevicesAdapter adapter = new DevicesAdapter(this, mViewModel.getScannerRepository().getScannerResults());
adapter.setOnItemClickListener(this);
recyclerViewDevices.setAdapter(adapter);
mViewModel.getScannerRepository().getScannerState().observe(this, this::startScan);
}
public long getAnimationDuration(@NonNull RecyclerView recyclerView, int animationType,
float animateDx, float animateDy) {
final RecyclerView.ItemAnimator itemAnimator = recyclerView.getItemAnimator();
if (itemAnimator == null) {
return DEFAULT_SWIPE_ANIMATION_DURATION;
} else {
return itemAnimator.getMoveDuration();
}
}
protected void initRecyclerView(RecyclerView view){
view.addItemDecoration(new BoxItemDividerDecoration(getResources()));
view.addItemDecoration(new FooterDecoration(getResources()));
view.setLayoutManager(new LinearLayoutManager(getActivity()));
if (view.getItemAnimator() instanceof SimpleItemAnimator) {
((SimpleItemAnimator) view.getItemAnimator()).setSupportsChangeAnimations(false);
}
}
protected void initRecyclerView(RecyclerView view){
int numColumns = (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) ? 5 : 3;
view.setLayoutManager(new GridLayoutManager(getActivity(),numColumns));
view.addItemDecoration(new SpacesItemDecoration(8));
if (view.getItemAnimator() instanceof SimpleItemAnimator) {
((SimpleItemAnimator) view.getItemAnimator()).setSupportsChangeAnimations(false);
}
}
/**
* Disables the animations of the specified {@link RecyclerView}.
*
* @param recyclerView The recycler view to disable animations of
*/
public static void disableRecyclerViewAnimations(RecyclerView recyclerView) {
if((recyclerView != null) && (recyclerView.getItemAnimator() != null)) {
recyclerView.setItemAnimator(null);
}
}
/**
* Called by the WeSwipeHelper when user action finished on a ViewHolder and now the View
* will be animated to its final position.
* <p>
* Default implementation uses ItemAnimator's duration values. If
* <code>animationType</code> is {@link #ANIMATION_TYPE_DRAG}, it returns
* {@link RecyclerView.ItemAnimator#getMoveDuration()}, otherwise, it returns
* {@link RecyclerView.ItemAnimator#getRemoveDuration()}. If RecyclerView does not have
* any {@link RecyclerView.ItemAnimator} attached, this method returns
* {@code DEFAULT_DRAG_ANIMATION_DURATION} or {@code DEFAULT_SWIPE_ANIMATION_DURATION}
* depending on the animation type.
*
* @param recyclerView The RecyclerView to which the WeSwipeHelper is attached to.
* @param animationType The type of animation. Is one of {@link #ANIMATION_TYPE_DRAG},
* {@link #ANIMATION_TYPE_SWIPE_CANCEL} or
* {@link #ANIMATION_TYPE_SWIPE_SUCCESS}.
* @param animateDx The horizontal distance that the animation will offset
* @param animateDy The vertical distance that the animation will offset
* @return The duration for the animation
*/
public long getAnimationDuration(RecyclerView recyclerView, int animationType,
float animateDx, float animateDy) {
final RecyclerView.ItemAnimator itemAnimator = recyclerView.getItemAnimator();
if (itemAnimator == null) {
return animationType == ANIMATION_TYPE_DRAG ? DEFAULT_DRAG_ANIMATION_DURATION
: DEFAULT_SWIPE_ANIMATION_DURATION;
} else {
return animationType == ANIMATION_TYPE_DRAG ? itemAnimator.getMoveDuration()
: itemAnimator.getRemoveDuration();
}
}
/**
* Called by the ItemTouchHelper when user action finished on a ViewHolder and now the View
* will be animated to its final position.
* <p>
* Default implementation uses ItemAnimator's duration values. If
* <code>animationType</code> is {@link #ANIMATION_TYPE_DRAG}, it returns
* {@link RecyclerView.ItemAnimator#getMoveDuration()}, otherwise, it returns
* {@link RecyclerView.ItemAnimator#getRemoveDuration()}. If RecyclerView does not have
* any {@link RecyclerView.ItemAnimator} attached, this method returns
* {@code DEFAULT_DRAG_ANIMATION_DURATION} or {@code DEFAULT_SWIPE_ANIMATION_DURATION}
* depending on the animation type.
*
* @param recyclerView The RecyclerView to which the ItemTouchHelper is attached to.
* @param animationType The type of animation. Is one of {@link #ANIMATION_TYPE_DRAG},
* {@link #ANIMATION_TYPE_SWIPE_CANCEL} or
* {@link #ANIMATION_TYPE_SWIPE_SUCCESS}.
* @param animateDx The horizontal distance that the animation will offset
* @param animateDy The vertical distance that the animation will offset
* @return The duration for the animation
*/
public long getAnimationDuration(RecyclerView recyclerView, int animationType,
float animateDx, float animateDy) {
final RecyclerView.ItemAnimator itemAnimator = recyclerView.getItemAnimator();
if (itemAnimator == null) {
return animationType == ANIMATION_TYPE_DRAG ? DEFAULT_DRAG_ANIMATION_DURATION
: DEFAULT_SWIPE_ANIMATION_DURATION;
} else {
return animationType == ANIMATION_TYPE_DRAG ? itemAnimator.getMoveDuration()
: itemAnimator.getRemoveDuration();
}
}
/**
* Called by the ItemTouchHelper when user action finished on a ViewHolder and now the View
* will be animated to its final position.
* <p>
* Default implementation uses ItemAnimator's duration values. If
* <code>animationType</code> is {@link #ANIMATION_TYPE_DRAG}, it returns
* {@link RecyclerView.ItemAnimator#getMoveDuration()}, otherwise, it returns {@link
* RecyclerView.ItemAnimator#getRemoveDuration()}. If RecyclerView does not have any {@link
* RecyclerView.ItemAnimator} attached, this method returns {@code
* DEFAULT_DRAG_ANIMATION_DURATION} or {@code DEFAULT_SWIPE_ANIMATION_DURATION} depending on
* the animation type.
*
* @param recyclerView The RecyclerView to which the ItemTouchHelper is attached to.
* @param animationType The type of animation. Is one of {@link #ANIMATION_TYPE_DRAG},
* {@link #ANIMATION_TYPE_SWIPE_CANCEL} or {@link
* #ANIMATION_TYPE_SWIPE_SUCCESS}.
* @param animateDx The horizontal distance that the animation will offset
* @param animateDy The vertical distance that the animation will offset
* @return The duration for the animation
*/
public long getAnimationDuration(RecyclerView recyclerView, int animationType,
float animateDx, float animateDy) {
final RecyclerView.ItemAnimator itemAnimator = recyclerView.getItemAnimator();
if (itemAnimator == null) {
return animationType == ANIMATION_TYPE_DRAG ? DEFAULT_DRAG_ANIMATION_DURATION
: DEFAULT_SWIPE_ANIMATION_DURATION;
} else {
return animationType == ANIMATION_TYPE_DRAG ? itemAnimator.getMoveDuration()
: itemAnimator.getRemoveDuration();
}
}