当有人在RecyclerView内部时,有没有人解决了CardView之谜而没有触摸反馈?
我有一个带有一堆CardViews(一个CardList)的RecyclerView.当我点击任何CardView时,我启动另一个Activity.这工作得很好,但是当我点击CardView时,我看不到任何触摸反馈.
及时,我已经使用以下方法配置了我的CardView(XML):
android:clickable="true"
android:background="?android:selectableItemBackground"
Run Code Online (Sandbox Code Playgroud)
谢谢!
android touch android-cardview android-5.0-lollipop android-recyclerview
我想将CardView的前景设置为?attr/selectableItemBackground.
当我在 xml 中声明前景时它起作用:
android:foreground="?attr/selectableItemBackground"
Run Code Online (Sandbox Code Playgroud)
但我想以编程方式设置前景。这不起作用:
int[] attrs = new int[]{R.attr.color_a, R.attr.selectableItemBackground};
TypedArray ta = context.obtainStyledAttributes(attrs);
mColorA = ta.getColor(0, 0);
mSelectableItemBackground = ContextCompat.getDrawable(context, ta.getResourceId(1, 0));
ta.recycle();
...
cardView.setOnClickListener(onClickListener);
cardView.setClickable(true);
cardView.setForeground(mSelectableItemBackground);
Run Code Online (Sandbox Code Playgroud)
但是,检索属性有效(mSelectableItemBackground 包含一个RippleDrawable),但是当我按下卡片时前景不会改变。
我正试图在点击卡片视图时添加涟漪效果,奇怪的是它没有出现?
这里可能有什么问题?
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/tools"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:layout_margin="5dp"
card_view:cardCornerRadius="6dp"
card_view:contentPadding="5dp"
card_view:cardElevation="4dp"
card_view:cardMaxElevation="6dp"
app:ignore="NamespaceTypo">
</androidx.cardview.widget.CardView>
Run Code Online (Sandbox Code Playgroud)
// 我在cardview中有一个带有三个textview的线性布局。
回收者视图:
<LinearLayout
android:id="@+id/cardViewLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:visibility="gone">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/cardList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
谢谢!