[英]How to add ripple effect to Preference
我有一個 PreferenceFragemnt,我想對 Preferences 產生服裝連鎖反應。 所以我開始制作 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);
}
}
也許有人有另一個想法。 :)
在您鏈接到的帖子中,他們使用方法onBindView ,而不是onCreateView
。
所以你可以試試:
@Override
protected void onBindView(View view) {
super.onBindView(view);
view.setBackground(<my ripple>);
}
編輯: 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.