Cardview onclick无法启用波纹效果

问题描述 投票:0回答:1

这是我到目前为止所取得的成就

图像适配器

public class ImageAdapter extends BaseAdapter {
private Context mContext;
TextView text;
ImageView imageView;

// Constructor
public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    View view = View.inflate(mContext, R.layout.grid_view, null);
    RelativeLayout rl = (RelativeLayout) view.findViewById(R.id.relaGrid);

    ImageView image = (ImageView) rl.findViewById(R.id.chooseImage);
    TextView text = (TextView) rl.findViewById(R.id.chooseText);       
    image.setImageResource(mThumbIds[position]);
    text.setText(mThumbTxt[position]);
    Typeface customFont = Typeface.createFromAsset(mContext.getAssets(), "fonts/faruma.ttf");
    text.setTypeface(customFont);
    return rl;
}

// Keep all Images in array
public Integer[] mThumbIds = {
    R.drawable.ic_action_gallery, R.drawable.ic_action_gallery,
    R.drawable.ic_action_gallery, R.drawable.ic_action_gallery,
    R.drawable.ic_action_gallery, R.drawable.ic_action_gallery,
    R.drawable.ic_action_gallery, R.drawable.ic_action_gallery
};

private String[] mThumbTxt = {
    "ހެނދުނުގެ ޛިކުރު", "ހަވީރުގެ ޛިކުރު", "İbadethanddjjdjdjdjdjdj deler", "Hepsi", "Abo", 
    "Ryhan", "Haly", "Arush"
};

}

查看

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/relaGrid"
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    card_view:cardCornerRadius="5dp"
    card_view:cardElevation="6dp"
    android:translationZ="5dp"
    android:clickable="true"
    android:foreground="?android:attr/selectableItemBackground"
    card_view:cardUseCompatPadding="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="10dp">

<ImageView
    android:id="@+id/chooseImage"
    android:layout_width="match_parent"
    android:layout_height="85dp">
</ImageView>

<TextView android:id="@+id/chooseText"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:textSize="18sp"
    android:layout_below="@+id/chooseImage"
    android:gravity="center"
    android:supportsRtl="true"
    android:singleLine="true"
    android:paddingRight="5dp"
    android:paddingLeft="5dp"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever">
    </TextView>

    </RelativeLayout>

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

我正在尝试通过onclick监听器在cardview上产生连锁反应,从而启动另一个目的。但是启用波纹效果后,onclick事件将无法正常工作。我只看到涟漪效应而已。 Onclick侦听器不起作用。如何实现两者?请帮助

java android cardview
1个回答
0
投票

[首先,尝试使用XML在CardView上设置侦听器。声明一个方法,其中包含单击时应执行的操作。然后,实现它:

类似:-

android:onClick = "(method)"

如果不起作用,则在相对布局上设置onClickListener,该相对布局是CardView的子级。像:-

relativeLayout.setOnClickListener ...

这应该用Java完成。

© www.soinside.com 2019 - 2024. All rights reserved.