3

В этом приложении заметил, что если нажать на элемент и удерживать то элемент окрашивается в сероватый цвет давая понять пользователю что он нажал на него. Как в ListView. Как сделать также? А то я нажимаю/удерживаю на элементе, но непонятно как-то. В ListView тоже окрашивался в сероватый цвет.

введите сюда описание изображения

Разметка

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="10dp">

        <de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/imgRadio"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:src="@mipmap/ic_launcher"
            app:civ_border_color="@android:color/white"
            app:civ_border_width="2dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="?android:selectableItemBackground"
            android:gravity="center_vertical"
            android:orientation="vertical"
            android:paddingLeft="8dp"
            android:paddingRight="8dp">

            <TextView
                android:id="@+id/txtRadioTitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:singleLine="true"
                android:text="Название радио"
                android:textColor="@android:color/darker_gray"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/txtRadioCity"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:singleLine="true"
                android:text="Город"
                android:textColor="@android:color/darker_gray"
                android:textSize="16sp" />
        </LinearLayout>

        <ImageButton
            android:id="@+id/btnFavorite"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@drawable/favorites_button"
            android:focusable="false"
            android:onClick="onFavoriteClick"
            android:padding="15dp"
            android:scaleType="centerCrop"
            app:srcCompat="@drawable/ic_star_black" />

    </LinearLayout>
</android.support.v7.widget.CardView>
1
  • 4
    Зачем вы вопросы свои удаляете? Я потратил время на написание ответа, а отправить его не смог - вопрос удалён. Прекратите так делать. Мы тут не конкретным юзерам на вопросы отвечаем, а составляем общедоступную базу знаний. Ваши вопросы принадлежат сообществу, а не лично вам.
    – ЮрийСПб
    19 авг 2017 в 21:56

2 ответа 2

10

LinearLayout в котором у вас view элементы задайте background:

android:background="?android:selectableItemBackground"

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="?android:selectableItemBackground"  >
4
  • Сделал так, но по нажатию на item списка не заметно что фон как-то меняется
    – Scorpion
    19 авг 2017 в 17:15
  • Покажите код разметки, я только пишу приложение, тоже реализовал, у меня прекрасно работает.
    – McDaggen
    19 авг 2017 в 17:22
  • В вопрос добавил
    – Scorpion
    19 авг 2017 в 17:30
  • Сработало. Оказывается не в тот LinerLayout добавил атрибут
    – Scorpion
    19 авг 2017 в 17:33
2

Если у вас собственный адаптер для ListView, то повесте на него хендлер через setOnItemLongClickListener, внутри которого можно подкрашивать, анимировать или что-то еще делать со своими вьюхами.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.