如何在android中的recyclerview中向左和向右滑动两侧

时间:2017-04-20 06:57:01

标签: android android-recyclerview

我想左右两侧向左侧和右侧滑动recyclelerview,如下图所示: - enter image description here

但我做不到我看到很多图书馆,但我发现只有一面滑动。我希望双方都刷我怎么做,这有助于我做到这一点。

提前致谢

4 个答案:

答案 0 :(得分:3)

你可以尝试下面给出一个你只需要一些修改的文件

https://github.com/daimajia/AndroidSwipeLayout

谢谢!

答案 1 :(得分:0)

你需要这样的东西:

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getAdapterPosition();

if (direction == ItemTouchHelper.LEFT){
  Log.i("ItemSwipe", "LEFT");
} 
else if (direction == ItemTouchHelper.RIGHT){
  Log.i("ItemSwipe", "RIGHT");
 }
}

答案 2 :(得分:0)

可能已经晚了,但是可以帮上忙。

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_childs, container, false);
    recyclerView = v.findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false));
    recyclerView.addItemDecoration(new VerticalItemDecoration());
    recyclerView.setAdapter(new ChildCareAdapter());
    touchHelper.attachToRecyclerView(recyclerView); // Attaching with RecyclerView
    return v;
}

ItemTouchHelper touchHelper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder viewHolder1) {
        return false;
    }

    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (i == ItemTouchHelper.LEFT) {
            Toast.makeText(getContext(),"Swipe left",Toast.LENGTH_SHORT).show();
        } else if (i == ItemTouchHelper.RIGHT) {
            Toast.makeText(getContext(),"Swipe right",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
            float alpha = 1 - (Math.abs(dX) / recyclerView.getWidth());
            viewHolder.itemView.setAlpha(alpha);
        }
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }
});

答案 3 :(得分:-1)

下面是工作代码创建自定义侦听器 SyncedScrollListener 并将其分配给recycleview。

  rv1.setOnScrollListener(new SyncedScrollListener(rv2));
  rv2.setOnScrollListener(new SyncedScrollListener(rv1));


public class SyncedScrollListener implements AbsListView.OnScrollListener {
    int offset;
    int oldVisibleItem = -1;
    int currentHeight;
    int prevHeight;
    private View mSyncedView;

    public SyncedScrollListener(View syncedView) {

        if (syncedView == null) {
            throw new IllegalArgumentException("syncedView is null");
        }

        mSyncedView = syncedView;
    }

    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {

        int[] location = new int[2];

        if (visibleItemCount == 0) {
            return;
        }

        if (oldVisibleItem != firstVisibleItem) {

            if (oldVisibleItem < firstVisibleItem) {
                prevHeight = currentHeight;
                currentHeight = view.getChildAt(0).getHeight();

                offset += prevHeight;

            } else {
                currentHeight = view.getChildAt(0).getHeight();

                View prevView;
                if ((prevView = view.getChildAt(firstVisibleItem - 1)) != null) {
                    prevHeight = prevView.getHeight();
                } else {
                    prevHeight = 0;
                }

                offset -= currentHeight;
            }

            oldVisibleItem = firstVisibleItem;
        }

        view.getLocationOnScreen(location);
        int listContainerPosition = location[1];

        view.getChildAt(0).getLocationOnScreen(location);
        int currentLocation = location[1];

        int blah = listContainerPosition - currentLocation + offset;

        mSyncedView.scrollTo(0, blah);

    }

    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // TODO Auto-generated method stub

    }
}