web-dev-qa-db-fra.com

Ajout d'un effet d'entraînement pour Afficher dans onClick

Bonjour, j'essaie d'ajouter un effet d'entraînement sur la méthode onClick pour View, mais celui-ci ne fonctionne pas. Tous mes articles ont une ID, mais je ne sais pas comment l'appeler

Voici un code.

 @Override
public void onClick(View v) {
    int[] attrs = new int[]{R.attr.selectableItemBackground};
    TypedArray typedArray = getActivity().obtainStyledAttributes(attrs);
    int backgroundResource = typedArray.getResourceId(0, 0);
    v.setBackgroundResource(backgroundResource);

    switch (v.getId()) {
        case ACTION_PLAY_ID:
            Log.d(MainActivity.TAG, getString(R.string.detail_action_play));
            v.setBackgroundResource(backgroundResource);
            Intent intent = new Intent(getActivity(), PlayerActivity.class);
            intent.putExtra(Video.VIDEO_TAG, videoModel);
            startActivity(intent);

            break;
        case ACTION_BOOKMARK_ID:
            if (bookmarked) {
                v.setBackgroundResource(backgroundResource);
                deleteFromBookmarks();
                ((ImageView) v).setImageDrawable(res.getDrawable(R.drawable.star_outline));
            } else {
                v.setBackgroundResource(backgroundResource);
                addToBookmarks();
                ((ImageView) v).setImageDrawable(res.getDrawable(R.drawable.star));
            }
            break;
        case ACTION_REMINDER_ID:
            if (!isReminderSet) {
                createReminderDialog((ImageView) v);
            } else {
                cancelReminder(liveTvProgram.getProgramId());
                ((ImageView) v).setImageDrawable(res.getDrawable(R.drawable.alarm));
            }
            break;
    }
}

Pour Lubomir

j'ai quelque chose comme ça mais ne fonctionne pas trop:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
        savedInstanceState) {
    View view = inflater.inflate(R.layout.item_detail, container, false);
    ButterKnife.bind(this, view);

    View myView = view.findViewById(R.id.actions_container);
    int[] attrs = new int[]{R.attr.selectableItemBackground};
    TypedArray typedArray = getActivity().obtainStyledAttributes(attrs);
    int backgroundResource = typedArray.getResourceId(0, 0);
    myView.setBackgroundResource(backgroundResource);

    loadImage();
    init();

    return view;
}

ImageViews (actionbuttons) crée dans Java pour LinearLayout actions_container

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
                Android:layout_width="match_parent"
                Android:layout_height="match_parent">

    <ImageView
        Android:id="@+id/header_image"
        Android:layout_width="250dp"
        Android:layout_height="250dp"
        Android:layout_alignParentLeft="true"
        Android:layout_marginLeft="10dp"
        Android:layout_marginStart="10dp"
        Android:layout_marginTop="@dimen/detail_image_1_state"
        Android:elevation="8dp"/>

    <RelativeLayout
        Android:id="@+id/container"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        Android:layout_marginBottom="@dimen/detail_bottom_margin"
        Android:layout_marginTop="@dimen/detail_top_margin"
        Android:background="@color/primary_color">

        <LinearLayout
            Android:id="@+id/actions_container"
            Android:layout_width="match_parent"
            Android:layout_height="@dimen/detail_actions_height"
            Android:layout_alignParentTop="true"
            Android:background="@drawable/ripple_effect_image"
            Android:elevation="2dp"
            Android:orientation="horizontal"
            Android:paddingLeft="300dp"
            Android:paddingStart="300dp"/>

        <LinearLayout
            Android:id="@+id/content_container"
            Android:layout_width="match_parent"
            Android:layout_height="wrap_content"
            Android:layout_below="@id/actions_container"
            Android:orientation="vertical"
            Android:paddingLeft="300dp"
            Android:paddingStart="300dp">

            <TextView
                Android:id="@+id/title"
                style="@style/TextTitleStyle"
                Android:layout_width="wrap_content"
                Android:layout_height="wrap_content"/>

            <TextView
                Android:id="@+id/subtitle"
                style="@style/TextSubtitleStyle"
                Android:layout_width="wrap_content"
                Android:layout_height="wrap_content"
                Android:visibility="gone"/>

            <TextView
                Android:id="@+id/duration"
                style="@style/TextSubtitleStyle"
                Android:layout_width="wrap_content"
                Android:layout_height="wrap_content"/>

            <TextView
                Android:id="@+id/season"
                style="@style/TextDescriptionStyle"
                Android:layout_width="wrap_content"
                Android:layout_height="wrap_content"
                Android:visibility="gone"/>

            <TextView
                Android:id="@+id/episode"
                style="@style/TextDescriptionStyle"
                Android:layout_width="wrap_content"
                Android:layout_height="wrap_content"
                Android:visibility="gone"/>

            <TextView
                Android:id="@+id/description"
                style="@style/TextDescriptionStyle"
                Android:layout_width="wrap_content"
                Android:layout_height="wrap_content"
                Android:maxLines="7"/>

        </LinearLayout>

        <FrameLayout
            Android:id="@+id/recommended_frame"
            Android:layout_width="match_parent"
            Android:layout_height="200dp"
            Android:layout_alignParentBottom="true">

            <Android.support.v17.leanback.widget.HorizontalGridView
                Android:id="@+id/recommendation"
                Android:layout_width="match_parent"
                Android:layout_height="wrap_content"
                Android:clipChildren="false"
                Android:clipToPadding="false"
                Android:paddingLeft="10dp"
                Android:paddingRight="10dp"/>
        </FrameLayout>

        <TextView
            Android:id="@+id/recommended_text"
            style="@style/TextHeaderStyle"
            Android:layout_width="match_parent"
            Android:layout_height="wrap_content"
            Android:layout_above="@id/recommended_frame"
            Android:text="@string/related_programs"/>

    </RelativeLayout>


</RelativeLayout>

De plus, mon fichier d'effet d'ondulation xml est comme:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:color="@color/dark_primary_color">
    <item>
        <color Android:color="@color/dark_primary_color" />
    </item>
    <item Android:id="@Android:id/mask">
        <shape Android:shape="rectangle">
            <solid Android:color="?android:colorAccent" />
        </shape>
    </item>
</ripple>
18
Rodriquez

Vues cliquables

En général, l'effet d'entraînement pour les boutons normaux fonctionne par défaut dans l'API 21 et pour les autres vues tactiles, il peut être obtenu en spécifiant

Android:background="?android:attr/selectableItemBackground"

Dans du code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);

    View myView = findViewById(R.id.myView);
    int[] attrs = new int[]{R.attr.selectableItemBackground};
    TypedArray typedArray = getActivity().obtainStyledAttributes(attrs);
    int backgroundResource = typedArray.getResourceId(0, 0);
    myView.setBackgroundResource(backgroundResource);
}
47
Lubomir Babev

créer un fond d'ondulation

view_background.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:color="@color/blue" >
    <item Android:drawable="@drawable/view_normal">
    </item>
</ripple>

view_noraml.xml // voici comment vous voyez apparaître en normal

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:shape="rectangle" >

    <corners
        Android:radius="@dimen/button_corner"/>
    <solid
        Android:color="@Android:color/transparent"/>
    <stroke
        Android:width="0.5dp"
        Android:color="@color/white"/>

</shape>

définissez maintenant le view_background sur votre vue

exemple

<FrameLayout
    xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="match_parent"
    Android:layout_height="120dp"
    Android:foreground="@drawable/view_background"
    Android:clickable="true"
    Android:focusable="true"
    >
    <ImageView
        Android:id="@+id/grid_item_imageView"
        Android:layout_width="match_parent"
        Android:layout_height="120dp"
        Android:layout_gravity="center"
        Android:scaleType="centerInside"
        />
</FrameLayout>
3
SaravInfern

Vous pouvez ajouter:

 <ImageView
  Android:layout_width="wrap_content"
  Android:layout_height="wrap_content"
  Android:clickable="true"
  Android:background="@drawable/ripple"/>   
2
cheng

Comme indiqué dans réponse de Lubomir Babev , en ajoutant Android:background="?android:attr/selectableItemBackground" fait l'affaire.

Cependant, si votre vue a déjà un arrière-plan, vous pouvez utiliser la même chose sur le Android:foreground attribut à la place:

Android:background="@color/anyColor"
Android:foreground="?android:attr/selectableItemBackground"

Android:foreground n'est cependant pris en charge que par l'API 23+.

2
Ramy Arbid

La solution pour cela est simple et facile de mon côté.

Voici l'effet d'entraînement:

    <?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:color="#BFB3F7">
    <item Android:id="@Android:id/mask">
        <shape Android:shape="oval">
            <solid Android:color="@color/button_background_color" />
        </shape>
    </item>
</ripple>

et ensuite sur la classe dont j'ai besoin pour rechercher la fonction setBackground

Ensuite, je dois lui déclarer un article à dessiner. quelque chose comme ça:

 @Override
public void onFocusChange(View v, boolean hasFocus) {

    if (hasFocus) {
        v.setBackground(res.getDrawable(R.drawable.ripple_effect_for_buttons));
        scrollContainer(false);
    } else {
        v.setBackground(null);
        if (recommendation.getFocusedChild() != null) {
            scrollContainer(true);
        }

    }
}

Et YUPII son fonctionnement

1
Rodriquez