从偏好中删除涟漪效应

Remove Ripple Effect from Preference

我想要一个可点击但隐藏的没有连锁反应的首选项。

我已经尝试添加按钮、创建自定义首选项并禁用 XML 中的内容,但涟漪效应仍然存在。

如何创建可点击但隐藏且没有连锁反应的首选项?

XML

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <SwitchPreference
            android:defaultValue="false"
            android:persistent="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:clickable="true"
            android:visibility="gone"
            android:visible="false"
            android:background="@drawable/bg_transparent"
            android:textColor="@color/transparent"
            android:switchTextAppearance="@color/transparent"
            android:button="@color/transparent"
            android:cursorVisible="false"
            app:rippleColor="@android:color/transparent"
            tools:visibility="gone"
            android:animateLayoutChanges="false"
            android:animationCache="false"
            android:animation="@color/transparent"
            android:background="@drawable/dt_transparent"
            android:listSelector="@android:color/transparent"
            android:title="" />

    <mypreferences.HiddenPreference
            android:clickable="true"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:foreground="@color/transparent"
            android:key="UnlockSelection"
            android:layoutAnimation="@color/transparent"
            android:longClickable="false"
            android:overScrollMode="never"
            android:soundEffectsEnabled="false"
            android:visibility="gone"
            android:visible="false"
            app:rippleColor="@android:color/transparent"
            app:tabRippleColor="@android:color/transparent"
            app:tabIndicatorColor="@android:color/transparent"
            tools:visibility="gone" />
</PreferenceScreen>

HiddenPreference.java

public class HiddenPreference extends Preference
{
    @SuppressWarnings("unused")
    public HiddenPreference(Context context) {
        super(context);
    }
    
    @SuppressWarnings("unused")
    public HiddenPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    @Override
    protected View onCreateView(ViewGroup parent)
    {
        Resources res = getContext().getResources();
        View v = super.onCreateView(parent);
        v.setAnimation(null);
        v.setEnabled(false);
        v.setBackground(res.getDrawable(R.drawable.dt_transparent)); // R.drawable.dt_transparent is a transparent .png file
        // v.setBackground(null); // -- This didn't work either
        v.setBackgroundColor(Color.TRANSPARENT);
        v.setAlpha(0);
        v.setSoundEffectsEnabled(false);
        v.setSystemUiVisibility(View.GONE);
        v.setWillNotDraw(true);
        v.clearAnimation();
        v.setVisibility(View.GONE);
        v.setClickable(true);
        return v;
    }
}

我最终创建了一个 LinearLayout 和一个透明的 Image,然后我使用 LinearLayout 作为我想要隐藏的首选项的布局。

首选项屏幕 XML

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <com.compay.preferences.HiddenPreference
        android:clickable="true"
        android:key="myHiddenPreference" />

    <!-- Add other preferences here -->
</PreferenceScreen>

hidden_preference.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/widget_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/hiddenPreference"
        android:clickable="true"
        android:focusable="true"
        android:soundEffectsEnabled="false"
        android:background="@drawable/dt_transparent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="hiddenPreference"
         />

</LinearLayout>

HiddenPreference.java

public class HiddenPreference extends Preference
{
    @SuppressWarnings("unused")
    public HiddenPreference(Context context) {
        super(context);
    }

    @SuppressWarnings("unused")
    public HiddenPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected View onCreateView(ViewGroup parent)
    {
        View v = super.onCreateView(parent);
        LayoutInflater li = (LayoutInflater)v.getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );

        return li.inflate(R.layout.hidden_preference, parent, false);
    }

    @Override
    protected void onBindView(View view)
    {
        view.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, 20));
        view.setSoundEffectsEnabled(false);

        view.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                // Handle click event
            }
        }
    }
}