【问题标题】:How do I implement a pull down animation effect for my swipe down function?如何为我的向下滑动功能实现下拉动画效果?
【发布时间】:2018-11-01 00:26:56
【问题描述】:

我有一个回收站视图,我在该视图上从顶部向下滑动,打开另一个页面。

我想实现一个具有相同功能的下拉动画效果,如果您从顶部下拉 RecyclerView,则会打开一个新页面(活动开关)。

这是我的MainActivity.java

package com.example.surajpatil.sp_gesture;

public class MainActivity extends AppCompatActivity {

MyRecyclerViewAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ArrayList<String> roomNames = new ArrayList<>();
    roomNames.add("Hall");
    roomNames.add("Dinning");
    roomNames.add("Patio");
    roomNames.add("Bedroom");
    roomNames.add("Master Bedroom");
    roomNames.add("Kitchen");
    roomNames.add("Porch");
    roomNames.add("Hall Way");
    roomNames.add("Bathroom");

    GestureRecyclerView recyclerView = findViewById(R.id.rvRooms);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    adapter = new MyRecyclerViewAdapter(this, roomNames);
    recyclerView.setAdapter(adapter);
    recyclerView.setOnRecyclerViewGestureListener(new GestureRecyclerView.OnRecyclerViewGestureListener() {
        @Override
        public void onScrollUp() {
            Toast.makeText(MainActivity.this, " Scroll Up ", Toast.LENGTH_LONG).show();

        }

        @Override
        public void onSwipeLeft() {
            Toast.makeText(MainActivity.this, " Swipe Left ", Toast.LENGTH_LONG).show();

        }

        @Override
        public void onSipeRight() {
            Toast.makeText(MainActivity.this, " Swipe Right ", Toast.LENGTH_LONG).show();

        }

        @Override
        public void onScrollDown() {
            Toast.makeText(MainActivity.this, " Scroll Down", Toast.LENGTH_LONG).show();

        }

        @Override
        public void onSwipeDownOverFirstItem() {
            Toast.makeText(MainActivity.this, " Swipe Bottom", Toast.LENGTH_LONG).show();

            Intent roomIntent = new Intent(MainActivity.this, RoomActivity.class);
            startActivity(roomIntent);
        }
    });
}
}

这是我的GestureRecyclerView.java

package com.example.surajpatil.sp_gesture;


public class GestureRecyclerView extends RecyclerView implements GestureDetector.OnGestureListener {
private GestureDetector gestureDetector;
private OnRecyclerViewGestureListener onRecyclerViewGestureListener;

public GestureRecyclerView(@NonNull Context context) {
    super(context);
    init();
}

public GestureRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    init();
}

public GestureRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}


public void setOnRecyclerViewGestureListener(OnRecyclerViewGestureListener onRecyclerViewGestureListener) {
    this.onRecyclerViewGestureListener = onRecyclerViewGestureListener;
}

@Override
public boolean onTouchEvent(MotionEvent e) {
    gestureDetector.onTouchEvent(e);
    return super.onTouchEvent(e);
}

private void init(){
    gestureDetector = new GestureDetector(getContext(),this);
}



//Calculate the swipe gestures
//You can customize the swipe threshold values as per your need, they just represent the velocity threshold of the swipe

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    final int SWIPE_THRESHOLD = 300;
    final int SWIPE_VELOCITY_THRESHOLD = 150;
    boolean result = false;
    try {
        float diffY = e2.getY() - e1.getY();
        float diffX = e2.getX() - e1.getX();
        if (Math.abs(diffX) > Math.abs(diffY)) {
            if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                if (diffX > 0) {
                    onRecyclerViewGestureListener.onSipeRight();
                } else {
                    onRecyclerViewGestureListener.onSwipeLeft();

                }
                result = true;
            }
        }
        else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
            if (diffY > 0) {
                if (((LinearLayoutManager)getLayoutManager()).findFirstCompletelyVisibleItemPosition() == 0) {
                    onRecyclerViewGestureListener.onSwipeDownOverFirstItem();
                }else {
                    onRecyclerViewGestureListener.onScrollDown();
                }
            } else {
                onRecyclerViewGestureListener.onScrollUp();
            }
            result = true;
        }
    } catch (Exception exception) {
        exception.printStackTrace();
    }
    return result;
}


//Callbacks to listen to the different swipe events.
interface OnRecyclerViewGestureListener{
    void onScrollUp();
    void onSwipeLeft();
    void onSipeRight();
    void onScrollDown();
    void onSwipeDownOverFirstItem(); // Gets called when the swipe down gesture is detected and the list has reached to the top position.
}


//Intercepting the touch event from the user touch and passing it on to the Gesture Detector
@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
    gestureDetector.onTouchEvent(e);
    return super.onInterceptTouchEvent(e);
}


@Override
public boolean onDown(MotionEvent motionEvent) {
    return false;
}

@Override
public void onShowPress(MotionEvent motionEvent) {

}

@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
    return false;
}

@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
    return false;
}

@Override
public void onLongPress(MotionEvent motionEvent) {

}
}

除此之外,我的项目中还有 MyRecyclerView.java、RoomActivity.java、main_activity.xml、recyclerview_row.xml 和 room.xml。

此代码正确检测滑动手势,现在我想实现下拉动画以及从顶部向下滑动的功能。

【问题讨论】:

    标签: android


    【解决方案1】:

    在包含回收站视图的根 Activity 中添加 SwipeRefreshLayout 布局。 示例:

    <android.support.v4.widget.SwipeRefreshLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/swiperefresh"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
    
        <RecyclerView
         android:id="@android:id/list"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />
    
    </android.support.v4.widget.SwipeRefreshLayout>
    

    进一步使用接口回调SwipeRefreshLayout.OnRefreshListener执行动作。

    希望对你有帮助。

    【讨论】:

    • 这会在 RecyclerView 之上创建另一个视图层,并在下拉时显示一个刷新图标。我不想要那个;我想把 RecyclerView 拉下来!目前,它在一个位置上是静止的;我想从顶部向下滑动时将其拉下。
    猜你喜欢
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 2018-03-24
    • 2023-01-16
    • 2012-02-05
    • 2011-12-21
    • 1970-01-01
    相关资源
    最近更新 更多