我有一个回收站视图,在该视图上我从顶部向下滑动,打开了另一页。
我想实现具有相同功能的下拉动画效果,即,如果从顶部将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。
此代码可以正确检测到滑动手势,现在我想实现下拉动画以及从顶部功能向下滑动。
答案 0 :(得分:0)
在包含回收者视图的根活动中添加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
来执行操作。
希望有帮助。