使用左/右滑动Swip​​eRefreshLayout内的RecyclerView来更改视图

时间:2017-07-11 09:24:54

标签: android android-recyclerview swipe swiperefreshlayout

我在SwipeRefreshLayout

中有一个RecyclerView
<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/workout_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.v4.widget.SwipeRefreshLayout>  

在我的活动中

    refresh.setColorSchemeColors(getResources().getColor(R.color.buttonNormal));
    refresh.setProgressViewOffset(true, 80, 150);
    refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            loadWorkoutList();
        }
    });

    rvActivities.setHasFixedSize(true);
    rvActivities.setItemAnimator(new DefaultItemAnimator());
    rvActivities.setAdapter(mAdapter);
    rvActivities.setLayoutManager(mLayoutManager);  

如何在我的RecyclerView上进行左/右滑动以调用loadWorkoutList();方法?
我已经尝试使用OnGestureDetector来捕获触摸事件,但是SwipteRefreshLayout会阻止它。

public class Activity implement OnGesuteDetector {

@Override
public void onCreate(Bundle bundle) {
    GestureDetector gestureDetector = new GestureDetector(this, this);

    // my code
}

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

@Override
public void onShowPress(MotionEvent e) {

}

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

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    return false;
}

@Override
public void onLongPress(MotionEvent e) {

}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    // swipe left
    if (e1.getX() - e2.getX() > 50) {
        calendar.onDayClick(selectedDate.plusDays(1));

        return false;
    }

    // swipe right
    if (e2.getX() - e1.getX() > 50) {
        calendar.onDayClick(selectedDate.minusDays(1));

        return false;
    }

    return false;
}

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

1 个答案:

答案 0 :(得分:1)

您可以使用 ItemTouchHelper 在recyclerview中提供滑动 请查看以下代码,询问我是否有任何疑问

//in On Create Method
ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback
                (0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {

            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                return false;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir)
            {
                String str = movie_Name.get(viewHolder.getAdapterPosition());

                movie_Name.remove(viewHolder.getAdapterPosition());
                movie_Type.remove(viewHolder.getAdapterPosition());
                color_Array.remove(viewHolder.getAdapterPosition());
                image_Array.remove(viewHolder.getAdapterPosition());


                myDatabase = openOrCreateDatabase("movie", MODE_PRIVATE, null);
                myDatabase.execSQL("DELETE FROM movie WHERE name='" + str + "';");


                adapter.notifyItemRemoved(viewHolder.getAdapterPosition());

                adapter.notifyItemRangeChanged(viewHolder.getAdapterPosition(), movie_Name.size());
                adapter.notifyItemRangeChanged(viewHolder.getAdapterPosition(), movie_Type.size());
                adapter.notifyItemRangeChanged(viewHolder.getAdapterPosition(), image_Array.size());
                adapter.notifyItemRangeChanged(viewHolder.getAdapterPosition(), color_Array.size());

            }
        };
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
        itemTouchHelper.attachToRecyclerView(recyclerView);