[6.2.6][View点击态] 功能打开

This commit is contained in:
renwj
2023-12-19 18:15:43 +08:00
parent f0773c70e7
commit 2d5441acb0
8 changed files with 114 additions and 25 deletions

View File

@@ -62,6 +62,7 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_height="@dimen/dp_119"
app:pressed_enabled="false"
app:layout_constraintBottom_toBottomOf="parent">
<ImageView

View File

@@ -77,6 +77,7 @@
android:layout_marginRight="150dp"
android:layout_marginBottom="51dp"
android:layout_height="99dp"
app:pressed_enabled="false"
app:layout_constraintBottom_toBottomOf="parent">
<ImageView

View File

@@ -2,6 +2,7 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
app:pressed_enabled="false"
android:layout_width="@dimen/module_och_taxi_panel_width"
android:layout_height="match_parent"
android:background="@drawable/taxi_order_viewpager_bg"

View File

@@ -77,6 +77,7 @@
android:layout_marginRight="150dp"
android:layout_marginBottom="51dp"
android:layout_height="99dp"
app:pressed_enabled="false"
app:layout_constraintBottom_toBottomOf="parent">
<ImageView

View File

@@ -8,9 +8,13 @@ import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.view.ViewCompat;
import com.knightboost.lancet.api.Origin;
@@ -28,6 +32,39 @@ import com.mogo.launcher.R;
@Group("view_pressed_state")
public class ViewPressedStateLancet {
public static class OnClickWrapper implements View.OnClickListener {
private final View.OnClickListener delegate;
public OnClickWrapper(View.OnClickListener delegate) {
this.delegate = delegate;
}
@Override
public void onClick(View v) {
Object tag = v.getTag(R.id.click_pressed_attr_id);
checkSetBgIfNeed(null, v, tag == null ? null : (AttributeSet) tag);
this.delegate.onClick(v);
}
}
public static class OnLongClickWrapper implements View.OnLongClickListener {
private final View.OnLongClickListener delegate;
public OnLongClickWrapper(View.OnLongClickListener delegate) {
this.delegate = delegate;
}
@Override
public boolean onLongClick(View v) {
Object tag = v.getTag(R.id.click_pressed_attr_id);
checkSetBgIfNeed(null, v, tag == null ? null : (AttributeSet) tag);
return delegate.onLongClick(v);
}
}
@TargetClass(value = "android.view.View", scope = Scope.ALL)
@TargetMethod(methodName = "setOnClickListener")
@ReplaceInvoke
@@ -35,9 +72,7 @@ public class ViewPressedStateLancet {
if (view == null) {
return;
}
Object tag = view.getTag(R.id.click_pressed_attr_id);
checkSetBgIfNeed(view, tag == null ? null : (AttributeSet) tag);
view.setOnClickListener(listener);
view.setOnClickListener(new OnClickWrapper(listener));
}
@TargetClass(value = "android.view.View", scope = Scope.ALL)
@@ -47,17 +82,19 @@ public class ViewPressedStateLancet {
if (view == null) {
return;
}
Object tag = view.getTag(R.id.click_pressed_attr_id);
checkSetBgIfNeed(view, tag == null ? null : (AttributeSet) tag);
view.setOnLongClickListener(listener);
view.setOnLongClickListener(new OnLongClickWrapper(listener));
}
@ImplementedInterface(value = "android.view.LayoutInflater$Factory", scope = Scope.LEAF)
@Insert(mayCreateSuper = true)
@TargetMethod(methodName = "onCreateView")
public View onCreateView(String name, Context context, AttributeSet attrs) {
Log.d("XXXXXX", "onCreateView -- 1: name:" + name);
View view = (View) Origin.call();
checkSetBgIfNeed(view, attrs);
if (view == null && attrs != null) {
view = tryCreateView(name, context, attrs);
}
checkSetBgIfNeed(null, view, attrs);
return view;
}
@@ -65,37 +102,82 @@ public class ViewPressedStateLancet {
@Insert(mayCreateSuper = true)
@TargetMethod(methodName = "onCreateView")
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
Log.d("XXXXXX", "onCreateView -- 2: name:" + name);
View view = (View) Origin.call();
checkSetBgIfNeed(view, attrs);
if (view == null && attrs != null) {
view = tryCreateView(name, context, attrs);
}
checkSetBgIfNeed(parent, view, attrs);
return view;
}
private static void checkSetBgIfNeed(View view, AttributeSet attr) {
@Nullable
private static View tryCreateView(String name, Context context, AttributeSet attrs) {
View ret = null;
if (name != null && name.length() > 0) {
if (context != null) {
try {
ret = LayoutInflater.from(context).createView(name, null, attrs);
} catch (ClassNotFoundException ignore) {}
if (ret == null) {
String[] prefixList = {
"android.widget.",
"android.webkit.",
"android.app."
};
for (String prefix : prefixList) {
try {
ret = LayoutInflater.from(context).createView(name, prefix, attrs);
} catch (ClassNotFoundException ignore) { }
}
}
}
}
return ret;
}
private static void checkSetBgIfNeed(View parent, View view, AttributeSet attr) {
try {
TypedArray t = null;
float alpha;
float alpha = 0.6f;
boolean enabled;
try {
if (view == null) {
if (parent == null && view == null) {
return;
}
Context context = view.getContext();
Context context = parent == null ? view.getContext() : parent.getContext();
if (context == null) {
return;
}
if (attr == null) {
return;
if (parent != null) {
Object tag = parent.getTag(R.id.click_pressed_attr_enabled);
if (tag != null && !((Boolean) tag)) {
view.setTag(R.id.click_pressed_attr_enabled, false);
return;
}
}
t = context.obtainStyledAttributes(attr, R.styleable.ClickPressedStyle);
enabled = t.getBoolean(R.styleable.ClickPressedStyle_pressed_enabled, true);
if (!enabled) {
return;
if (view != null) {
Object tag = view.getTag(R.id.click_pressed_attr_enabled);
if (tag != null && !((Boolean) tag)) {
view.setTag(R.id.click_pressed_attr_enabled, false);
return;
}
if (attr != null) {
t = context.obtainStyledAttributes(attr, R.styleable.ClickPressedStyle);
enabled = t.getBoolean(R.styleable.ClickPressedStyle_pressed_enabled, true);
if (!enabled) {
view.setTag(R.id.click_pressed_attr_enabled, false);
return;
}
alpha = t.getFloat(R.styleable.ClickPressedStyle_pressed_alpha, 0.6f);
if (alpha < 0.0f || alpha > 1.0f) {
throw new AssertionError("view custom attr \\'alpha\\' must be in [0.0, 1.0]");
}
}
view.setTag(R.id.click_pressed_attr_id, attr);
}
alpha = t.getFloat(R.styleable.ClickPressedStyle_pressed_alpha, 0.6f);
if (alpha < 0.0f || alpha > 1.0f) {
throw new AssertionError("view custom attr \\'alpha\\' must be in [0.0, 1.0]");
}
view.setTag(R.id.click_pressed_attr_id, attr);
} finally {
if (t != null) {
t.recycle();
@@ -203,7 +285,7 @@ public class ViewPressedStateLancet {
if (constantState != null) {
Drawable pressed = DrawableCompat.wrap(constantState.newDrawable().mutate());
pressed.setAlpha((int)(255 * alpha));
drawable.addState(new int[] {android.R.attr.state_pressed}, pressed);
drawable.addState(new int[] { android.R.attr.state_pressed }, pressed);
return drawable;
}
}

View File

@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/item_video_cover"
android:layout_width="match_parent"
android:layout_height="match_parent">
@@ -31,6 +32,7 @@
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:gravity="bottom"
app:pressed_enabled="false"
android:orientation="horizontal">
<ImageView

View File

@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="click_pressed_attr_id" type="id" />
<item name="click_pressed_attr_enabled" type="id" />
</resources>

View File

@@ -31,7 +31,7 @@ LancetX {
enable rootProject.isJunkDetectEnable()
}
view_pressed_state {
enable false
enable true
}
}
}