CardView单击状态不起作用

时间:2015-05-26 05:44:12

标签: android material-design android-cardview

我想在用户点击任何项目时更改卡片商品的颜色,但没有任何反应..为什么?

<android.support.v7.widget.CardView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="3dp"
    android:layout_marginTop="9dp"
    android:layout_marginLeft="9dp"
    android:layout_marginRight="9dp"
    card_view:cardElevation="0.01dp"
    android:layout_marginBottom="0dp"
    android:foreground="@drawable/card_foreground">

2 个答案:

答案 0 :(得分:12)

请试试这个..

添加android:clickable="true"android:focusable="true"和 将android:foreground更改为android:foreground="?android:selectableItemBackground"

<android.support.v7.widget.CardView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="3dp"
    android:layout_marginTop="9dp"
    android:layout_marginLeft="9dp"
    android:layout_marginRight="9dp"
    card_view:cardElevation="0.01dp"
    android:layout_marginBottom="0dp"
    android:clickable="true"
    android:focusable="true"
    android:foreground="?android:selectableItemBackground" >

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

答案 1 :(得分:1)

  Recylerview.Adapter类中的

使用以下代码

@Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        LayoutInflater inflater = LayoutInflater.from(applicationContext);
        final View view = inflater.inflate(R.layout.additem, null);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int itemPosition = recyclerview.getChildPosition(view);
                String item = list.get(itemPosition);
                Toast.makeText(applicationContext, item, Toast.LENGTH_LONG).show();
            }
        });
        return new ViewHolder(view);
    }