java - mouselistener left click




onClick의 View에 파급 효과 추가 (4)

다음을 추가 할 수 있습니다.

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

안녕하세요,보기에 대한 파급 효과 onClick 메서드를 추가하려면 노력하고 있지만이 하나의 작동하지 않습니다. ID가있는 모든 항목은 있지만 통화 방법은 모르겠습니다.

여기에 코드가 있습니다.

 @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;
    }
}

Lubomir 들어

나는 이것 같이 그러나 무언가가 너무 일하는 것을 가지고있다 :

 @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;
}

LinearLayout 용 ImageView (actionbuttons)가 java에서 생성됩니다. 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>

또한 내 xml 파급 효과 파일은 다음과 같습니다.

<?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>

리플 배경 만들기

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 //이 방법은 정상적으로 나타납니다.

<?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>

이제 view_background를보기로 설정하십시오.

<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>

클릭 가능한 조회수

일반적으로 일반 버튼의 리플 효과는 API 21에서 기본적으로 작동하며 다른 터치 가능보기의 경우에는 다음을 지정하여 수행 할 수 있습니다.

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

코드에서 :

@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);
}

Lubomir Babev의 대답에 명시된 바와 같이 android:background="?android:attr/selectableItemBackground" 하면 트릭을 수행합니다.

그러나보기에 이미 배경이있는 경우 대신 android:foreground 속성에서 같은 것을 사용할 수 있습니다.

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

android:foreground 은 API 23+에서만 지원됩니다.





tv