Android 如何为偏好添加连锁反应
我有一个偏好Fragemnt,我想对偏好产生一个肋骨涟漪效应。所以我开始做一个CostumPreference,但我不知道如何设置背景可绘制。我找到了,但这对我不管用 这是我的参考资料:Android 如何为偏好添加连锁反应,android,android-preferences,Android,Android Preferences,我有一个偏好Fragemnt,我想对偏好产生一个肋骨涟漪效应。所以我开始做一个CostumPreference,但我不知道如何设置背景可绘制。我找到了,但这对我不管用 这是我的参考资料: public class RipPreference extends Preference { private Context ctx; public RipPreference(Context context, AttributeSet attrs) { super(cont
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);
}
}
公共类引用扩展了首选项{
私有上下文ctx;
公共引用(上下文、属性集属性){
超级(上下文,attrs);
ctx=上下文;
}
公共引用(上下文){
超级(上下文);
ctx=上下文;
}
//这项活动不会被推迟
受保护的视图onCreateView(视图组父级){
//首选项不是视图,因此没有onCreateView
视图=super.onCreateView(父级);
视图。挫折背景();
返回视图;
}
}
私有void setCustomStyle(视图){
RippleDrawable drawable=(RippleDrawable)ctx.getDrawable(R.drawable.settings\u preference\u background);
视图.立根台(可拉深);
}
}
也许有人有另一个想法。:) 在您链接到的帖子中,他们使用的是方法,而不是
onCreateView
所以你可以试试:
@Override
protected void onBindView(View view) {
super.onBindView(view);
view.setBackground(<my ripple>);
}
@覆盖
受保护的void onBindView(视图){
super.onBindView(视图);
视图。挫折背景();
}
编辑:androidx首选项库使用回收器视图,绑定视图的方法称为:
@覆盖
BindViewHolder上的受保护无效(首选ViewHolder){
super.onBindViewHolder(持有人);
holder.itemView.setBackground();
}
问题是超类没有onBindView,因此它也不会被触发。你的超类到底是什么?我还为androidx首选项库添加了一个示例,谢谢。我不知道。
@Override
protected void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
holder.itemView.setBackground(<my ripple>);
}