Добавить эффект щелчка Эффект ряби на Карточном представлении в Android Studio - PullRequest
0 голосов
/ 04 ноября 2019

Я программно добавил вид карты. Я просто хочу сделать его кликабельным и показывать анимацию во время нажатия. Вот мой код

CardView cardView = new CardView(this);
LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
cardView.setLayoutParams(layoutParams);
cardView.setRadius(15);
cardView.setPadding(25, 25, 25, 25);
cardView.setCardBackgroundColor(Color.MAGENTA);

TextView textView = new TextView(this);
textView.setLayoutParams(layoutParams);
textView.setText("Programmatically set");
textView.setGravity(Gravity.CENTER);
textView.setTextColor(Color.WHITE);
cardView.addView(textView);
LinearLayout linearLayout = findViewById(R.id.linearLayout1);
linearLayout.addView(cardView);

Ответы [ 3 ]

1 голос
/ 04 ноября 2019
int[] attrs = new int[]{R.attr.selectableItemBackground};
TypedArray typedArray = this.obtainStyledAttributes(attrs);
int selectableItemBackground = typedArray.getResourceId(0, 0);
typedArray.recycle();

cardView.setForeground(this.getDrawable(selectableItemBackground));
cardView.setClickable(true);
0 голосов
/ 04 ноября 2019

С библиотекой компонентов материалов просто используйте:

cardView.setRippleColor(ContextCompat.getColorStateList(this,R.color.selector_card));
0 голосов
/ 04 ноября 2019

Вы можете использовать это:

cardView.setClickable(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    TypedValue typedValue = new TypedValue();
    getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, typedValue, true);
    cardView.setBackgroundResource(typedValue.resourceId);
}
...