android - 滑动以在回收站 View android 中显示另一个隐藏的布局

标签 android android-layout android-recyclerview

我有一个回收站 View ,我想在其中显示隐藏在当前布局后面的滑动项目 View 的另一个布局。简而言之,我想实现如下图所示的效果。

enter image description here

我的代码的问题是整个 View 都被滑动了,但我只想滑动到隐藏布局的宽度。

Activity 中的代码

final ItemTouchHelper.Callback simpleItemTouchCallback = new ItemTouchHelper.Callback() {
            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                if (viewHolder.getAdapterPosition() < target.getAdapterPosition()) {
                    for (int i = viewHolder.getAdapterPosition(); i < target.getAdapterPosition(); i++) {
                        Collections.swap(myDataset, i, i + 1);
                    }
                } else {
                    for (int i = viewHolder.getAdapterPosition(); i > target.getAdapterPosition(); i--) {
                        Collections.swap(myDataset, i, i - 1);
                    }
                }
                mAdapter.notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
                return true;
            }

            @Override
            public boolean isLongPressDragEnabled() {
                return true;
            }
            @Override
            public boolean isItemViewSwipeEnabled() {
                return true;
            }
            @Override
            public void onSwiped(final RecyclerView.ViewHolder viewHolder, final int swipeDir) {

            }

            @Override
            public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
                View itemView = viewHolder.itemView;
                ImageView delete_image=(ImageView) itemView.findViewById(R.id.delete_image);
                delete_image.setY(itemView.getTop());
                if(isCurrentlyActive) {
                    delete_image.setVisibility(View.VISIBLE);
                }else{
                    delete_image.setVisibility(View.GONE);
                }
                super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
            }

            @Override
            public int getMovementFlags(RecyclerView recyclerView,
                                        RecyclerView.ViewHolder viewHolder) {
                int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
                int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
                return makeMovementFlags(dragFlags, swipeFlags);
            }
        };


ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
        itemTouchHelper.attachToRecyclerView(mRecyclerView);

recylerview 项目的自定义布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"

    android:paddingBottom="@dimen/padding_xsmall">

    <LinearLayout
        android:id="@+id/top_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:background="@color/very_light_grey"
        android:orientation="horizontal"
        android:paddingBottom="@dimen/padding_xlarge"
        android:paddingEnd="@dimen/padding_small"
        android:paddingLeft="@dimen/padding_xlarge"
        android:paddingRight="@dimen/padding_small"
        android:paddingStart="@dimen/padding_xlarge"
        android:paddingTop="@dimen/padding_xlarge">

        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/friend_image"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center_vertical"
            fresco:placeholderImage="@drawable/user_placeholder"
            fresco:roundAsCircle="true"
            fresco:roundedCornerRadius="50dp"
            fresco:roundingBorderColor="@android:color/white"
            fresco:roundingBorderWidth="2dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginEnd="@dimen/margin_small"
            android:layout_marginLeft="@dimen/margin_small"
            android:layout_marginRight="@dimen/margin_small"
            android:layout_marginStart="@dimen/margin_small"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <bi.it.com.bio.textview.CustomTextView
                    android:id="@+id/friend_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_toLeftOf="@+id/badge_text"
                    android:text="John"
                    android:textSize="@dimen/text_size_medium" />

                <bi.it.com.bio.textview.CustomTextView
                    android:id="@+id/badge_text"
                    android:layout_width="16dp"
                    android:layout_height="16dp"
                    android:layout_alignParentRight="true"
                    android:background="@drawable/badgeicon"
                    android:gravity="center"
                    android:text="24"
                    android:textColor="@android:color/white"
                    android:textSize="@dimen/text_size_xxsmall" />
            </RelativeLayout>

            <bi.it.com.bio.textview.CustomTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="this is message from john"
                android:textSize="@dimen/text_size_xsmall" />

        </LinearLayout>

    </LinearLayout>

    <ImageView
        android:id="@+id/delete_image"
        android:paddingLeft="@dimen/padding_large"
        android:paddingStart="@dimen/padding_large"
        android:paddingEnd="@dimen/padding_large"
        android:paddingRight="@dimen/padding_large"
        android:paddingTop="@dimen/padding_small"
        android:paddingBottom="@dimen/padding_small"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@color/red_color_list"
        android:src="@drawable/ic_delete_frd"
        android:layout_gravity="end|right"
        android:visibility="gone"/>

</FrameLayout>

我不想使用任何库。非常感谢任何帮助。 提前致谢。

最佳答案

您可以使用 ViewPagerPagerAdapter 来完成此操作。您的 PagerAdapter 将有两个页面:您的“主”全角 View 和具有操作按钮的第二个 View 。

关键是在您的适配器中覆盖 getPageWidth() 并为您的主视图返回 1.0 并为您的第二个 View 返回一些分数。该分数的计算方法是将所需的 View 宽度除以 ViewPager 的宽度。

当用户滑动时,第二个 View 无法占据整个屏幕,因此第一个 View 仍会部分显示,就像在您的动画图像中一样。

通过引用 ViewPagerViewHolder 和引用 PagerAdapterViewPager,您甚至可以回收适配器与 ViewPager 一起。

关于android - 滑动以在回收站 View android 中显示另一个隐藏的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33267148/

相关文章:

Android - 如何让我的系统覆盖窗口在不同屏幕上具有相同的尺寸

android - 自动占用空闲空间

java - ExpandableRecyclerView - 如何同时展开和折叠多个 CardView

java - FirebaseRecyclerAdapter 的 FirebaseDatabase 查询 : How to query some data basing on some values from other childs.

android - 使用没有任何 RecyclerView 的 PreferenceFragmentCompat 的 RecyclerView 异常

Android - 使用相机闪光灯

android - 使用 Android 服务的音频逐帧动画

java - getter 和 setter 方法的问题

android - 突出显示抽屉导航中相对于选项卡布局中的 fragment 的项目

android - ListView 涵盖了它下面的所有内容