如何在Android CardView上添加滑动功能?

8
我有一个包含两个TextView和两个ImageView的CardView。我想能够向左或向右滑动以“解除显示”。我实际上想要向右滑动发送一个Intent,但这可以稍后完成。现在,我希望能够通过向左或向右滑动来解除CardView的显示。我还希望有滑动的动画。
我尝试使用romannurik的SwipeDismissTouchListener,但CardView没有响应滑动。
如果有人有比romannurik的自定义监听器更好的解决方案,请分享。
这是我的CardView布局:
<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/generate"
    map:cardCornerRadius="@dimen/_3sdp"
    map:cardElevation="@dimen/_8sdp"
    android:id="@+id/restaurantContainer">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <ImageView
                    android:id="@+id/thumbnail"
                    android:layout_width="@dimen/_75sdp"
                    android:layout_height="@dimen/_75sdp"
                    android:layout_margin="@dimen/_5sdp" />

                <ImageView
                    android:id="@+id/rating"
                    android:layout_width="@dimen/_75sdp"
                    android:layout_height="@dimen/_15sdp"
                    android:layout_margin="@dimen/_5sdp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/name"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="@dimen/_5sdp"
                    android:gravity="top"
                    android:textAppearance="?android:attr/textAppearanceLarge" />

                <TextView
                    android:id="@+id/categories"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_margin="@dimen/_5sdp"
                    android:textAppearance="?android:attr/textAppearanceMedium" />
            </LinearLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <com.google.android.gms.maps.MapView
                android:id="@+id/mapView"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                map:cameraZoom="14"
                map:liteMode="true"
                map:mapType="normal" />

        </LinearLayout>

    </LinearLayout>

</android.support.v7.widget.CardView>

如何设置SwipeDismissTouchListener:

RelativeLayout rootLayout;
CardView restaurantCardView;

...

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        rootLayout = (RelativeLayout) inflater.inflate(R.layout.fragment_main, container, false);

        restaurantCardView = (CardView) rootLayout.findViewById(R.id.restaurantContainer);

        restaurantCardView.setOnTouchListener(new SwipeDismissTouchListener(restaurantCardView, null, new SwipeDismissTouchListener.DismissCallbacks() {
            @Override
            public boolean canDismiss(Object token) {
                Log.d("Chris", "canDismiss() called with: " + "token = [" + token + "]");
                return true;
            }

            @Override
            public void onDismiss(View view, Object token) {
                Log.d("Chris", "onDismiss() called with: " + "view = [" + view + "], token = [" + token + "]");
            }
        }));
    ....

    return rootLayout;

我能够看到canDismiss(...)的日志,但是看不到onDismiss(...)的日志。

1个回答

13

你需要将它放在RecyclerView中(并且将CardView作为唯一的项)

然后,使用ItemTouchHelper.SimpleCallbackitemTouchHelper.attachToRecyclerView(recyclerView);

这将为您提供动画。

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
}

你可以基于滑动方向指定特定的操作。

查看完整说明请点击这里:RecyclerView 滑动删除

此外,您还需要在 RecyclerView 中禁用垂直滚动:

public class UnscrollableLinearLayoutManager extends LinearLayoutManager {
    public UnscrollableLinearLayoutManager(Context context) {
        super(context);
    }

    @Override
    public boolean canScrollVertically() {
        return false;
    }
}

.....

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new UnscrollableLinearLayoutManager(this));
recyclerView.setAdapter(new RestaurantCardAdapter());
否则 - 一旦您尝试向上或向下滚动 - 您将看到RecyclerView的列表末尾动画。
更新:这是我用于测试的RecyclerView.Adapter
private class RestaurantCardAdapter extends RecyclerView.Adapter {
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new RestaurantViewHolder(new RestaurantCard(parent.getContext()));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {}

    @Override
    public int getItemCount() {
        return 1;
    }

    private class RestaurantViewHolder extends RecyclerView.ViewHolder {
        public RestaurantViewHolder(View itemView) {
            super(itemView);
        }
    }
}

RestaurantCard - 只是一个自定义的 View (在我们的情况下扩展了 CardView):

public class RestaurantCard extends CardView {
    public RestaurantCard(Context context) {
        super(context);
        initialize(context);
    }

    public RestaurantCard(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize(context);
    }

    private void initialize(Context context){
        LayoutInflater.from(context).inflate(R.layout.card, this);
        // ImageView imageView = (ImageView)getView.findViewById(...);
    }
}

太棒了,我非常感激你详细的回答。我有一个问题:由于我只使用一个CardView,创建RestaurantCardAdapter的最佳方法是什么?所有在线示例都是针对列表的。我需要特别注意什么吗?我只是不想在某个地方卡住并浪费时间尝试修复愚蠢的错误哈哈。 - ctzdev
1
@ChrisTarazi 我已经更新了答案,包括我用于测试这个想法的适配器和ViewHolder。祝你好运!有任何问题 - 尽管问。 - Konstantin Loginov
我在 onCreateViewHolder(...) 遇到了问题。根据我在网上看到的,我认为我们需要填充一个视图。因为在 onBindViewHolder(...) 中,我的 RestaurantViewHolderTextViewsImageViews 的引用是 null。所以肯定有一些与视图相关的东西我们忽略了。 - ctzdev
1
@ChrisTarazi 我在答案中添加了RestaurantCard类。不要对ViewHolder进行任何操作,只需按原样重用我的Adapter和ViewHolder即可。 - Konstantin Loginov
太棒了,谢谢你的帮助!一切都正常运行。 - ctzdev
@Konstantin Loginov 我看到设置将ItemTouchHelper对象附加到了RecycerView上。是否有一种方法可以将该对象附加到显示在详细活动中的单个CardView上(而不是RecyclerView活动)? - AJW

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接