简体   繁体   English

如何在 Preference 中添加涟漪效果

[英]How to add ripple effect to Preference

I have an PreferenceFragemnt and I want to have a costum ripple effect on the Preferences.我有一个 PreferenceFragemnt,我想对 Preferences 产生服装连锁反应。 So I started making a CostumPreference but I don't know how to set the background drawable.所以我开始制作 CostumPreference 但我不知道如何设置背景可绘制。 I found this post .But this doesn't work for me.我找到了这篇文章。但这对我不起作用。

There's my CostumPreference:这是我的 CostumPreference:

public class RipPreference extends Preference {
    private Context ctx;

    public RipPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        ctx = context;
    }

    public RipPreference(Context context) {
        super(context);
        ctx = context;
    }

    //This event won't be tiggered
    protected View onCreateView(ViewGroup parent) {
        //Preference is not an View so there is no onCreateView
        View view = super.onCreateView(parent);
        view.setBackground(<my ripple>);
        return view;
    }
}

    private void setCustomStyle(View view) {
        RippleDrawable drawable = (RippleDrawable) ctx.getDrawable(R.drawable.settings_preference_background);
        view.setBackground(drawable);
    }
}

Maybe someone has another idea.也许有人有另一个想法。 :) :)

In the post you linked to they use the method onBindView , not onCreateView .在您链接到的帖子中,他们使用方法onBindView ,而不是onCreateView

So you could try that:所以你可以试试:

@Override
protected void onBindView(View view) {
    super.onBindView(view);
    view.setBackground(<my ripple>);
}

Edit: The androidx Preference library uses a recycler view and the method to bind views is called onBindViewHolder :编辑: androidx Preference 库使用回收器视图,绑定视图的方法称为onBindViewHolder

@Override
protected void onBindViewHolder(PreferenceViewHolder holder) {
    super.onBindViewHolder(holder);
    holder.itemView.setBackground(<my ripple>);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM