我有一个包含两个TextView和两个ImageView的CardView。我想能够向左或向右滑动以“解除显示”。我实际上想要向右滑动发送一个Intent,但这可以稍后完成。现在,我希望能够通过向左或向右滑动来解除CardView的显示。我还希望有滑动的动画。
我尝试使用
如果有人有比
这是我的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(...)
的日志。
RestaurantCardAdapter
的最佳方法是什么?所有在线示例都是针对列表的。我需要特别注意什么吗?我只是不想在某个地方卡住并浪费时间尝试修复愚蠢的错误哈哈。 - ctzdevonCreateViewHolder(...)
遇到了问题。根据我在网上看到的,我认为我们需要填充一个视图。因为在onBindViewHolder(...)
中,我的RestaurantViewHolder
对TextViews
和ImageViews
的引用是null
。所以肯定有一些与视图相关的东西我们忽略了。 - ctzdev