理论上可以很好的支持 item swiping / dragging (based on ItemTouchHelper), 但是还是建议按如下代码使用。
1 2 3 4 5 6 7 8 9 10 11
// Normally you would attach an ItemTouchHelper & a callback to a RecyclerView, this way: RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); ItemTouchHelper.Callback myCallback = new ItemTouchHelper.Callback() { ... }; ItemTouchHelper myHelper = new ItemTouchHelper(myCallback); myHelper.attachToRecyclerView(recyclerView);
// INSTEAD of attaching the helper yourself, simply use the dedicated adapter c'tor, e.g.: new VerticalElasticityBounceEffect(new RecyclerViewElasticityAdapter(recyclerView, myCallback));
// Note: over-scroll is set-up using the helper method. IElasticity elasticity = ElasticityHelper.setUpOverScroll(recyclerView, ORIENTATION.HORIZONTAL);
elasticity.setOverScrollStateListener(new IElasticityStateListener() { @Override publicvoidonOverScrollStateChange(IElasticity elasticity, int oldState, int newState){ switch (newState) { case STATE_IDLE: // No over-scroll is in effect. break; case STATE_DRAG_START_SIDE: // Dragging started at the left-end. break; case STATE_DRAG_END_SIDE: // Dragging started at the right-end. break; case STATE_BOUNCE_BACK: if (oldState == STATE_DRAG_START_SIDE) { // Dragging stopped -- view is starting to bounce back from the *left-end* onto natural position. } else { // i.e. (oldState == STATE_DRAG_END_SIDE) // View is starting to bounce back from the *right-end*. } break; } } }
Real-time Updates Listener
滑动过程监听,可以监听滑动过程中手势的具体变化。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Note: over-scroll is set-up by explicity instantiating a decorator rather than using the helper; The two methods can be used interchangeably for registering listeners. IElasticity elasticity = new VerticalElasticityBounceEffect(new RecyclerViewElasticityAdapter(recyclerView, itemTouchHelperCallback));
elasticity.setOverScrollUpdateListener(new IElasticityUpdateListener() { @Override publicvoidonOverScrollUpdate(IElasticity elasticity, int state, float offset){ final View view = elasticity.getView(); if (offset > 0) { // 'view' is currently being over-scrolled from the top. } elseif (offset < 0) { // 'view' is currently being over-scrolled from the bottom. } else { // No over-scroll is in-effect. // This is synonymous with having (state == STATE_IDLE). } } });
publicclassCustomViewextendsView{ // ... } final CustomView view = (CustomView) findViewById(R.id.custom_view); new VerticalElasticityBounceEffect(new IElasticityAdapter() {
@Override public View getView(){ return view; }
@Override publicbooleanisInAbsoluteStart(){ // canScrollUp() is an example of a method you must implement return !view.canScrollUp(); }
@Override publicbooleanisInAbsoluteEnd(){ // canScrollDown() is an example of a method you must implement return !view.canScrollDown(); } });
完全自定义
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/// Make over-scroll applied over a list-view feel more 'stiff' new VerticalElasticityBounceEffect(new AbsListViewElasticityAdapter(view), 5f, // Default is 3 VerticalElasticityBounceEffect.DEFAULT_TOUCH_DRAG_MOVE_RATIO_BCK, VerticalElasticityBounceEffect.DEFAULT_DECELERATE_FACTOR, VerticalElasticityBounceEffect.MAX_SCALE_FACTOR); // Make over-scroll applied over a list-view bounce-back more softly new VerticalElasticityBounceEffect(new AbsListViewElasticityAdapter(view), VerticalElasticityBounceEffect.DEFAULT_TOUCH_DRAG_MOVE_RATIO_FWD, VerticalElasticityBounceEffect.DEFAULT_TOUCH_DRAG_MOVE_RATIO_BCK, -1f// Default is -2, VerticalElasticityBounceEffect.MAX_SCALE_FACTOR);