我有一个应用程序,用户可以使用颜色选择器或预定义按钮来更改其引号的背景。我希望用户可以通过长按更改预定义按钮的颜色,然后出现颜色选择器,用户现在可以选择将任何颜色保存到按钮。
由于某种原因,颜色选择器监听器方法在我的RecyclerView
适配器中不起作用。颜色选择器监听器应该给我当前的选择颜色。我用Log.d("TAG", ""+color);
进行了测试
我没有得到任何价值
RecyclerView适配器的Viewholder用于背景颜色:
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener, ColorPickerDialogFrag2.ColorPickerDialogListener {
public ImageButton colorButton;
public ViewHolder(View itemView) {
super(itemView);
this.colorButton = (ImageButton) itemView.findViewById(R.id.colorbutton);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
@Override
public void onClick(View v) {
int position = getLayoutPosition();
MainActivity.relativeLayout.setBackgroundColor(Color.parseColor((mColorButtons.get(position).getColor())));
PreferenceManager.getDefaultSharedPreferences(App.getAppContext()).edit().putInt(App.BACKGROUND, Color.parseColor(mColorButtons.get(position).getColor())).apply();
//ColorPicker for customizing colors for buttons
@Override
public boolean onLongClick(View v) {
ColorPickerDialogFrag2 cp = ColorPickerDialogFrag2.newInstance(6, Color.YELLOW);
cp.setStyle(android.support.v4.app.DialogFragment.STYLE_NORMAL, R.style.AppTheme);
cp.show(fragmentManager,"d");
return false;
}
@Override
public void onPreviewColorChanged(int dialogId, int color) {
//color is the current color from the colorpicker
Log.d("TAG", ""+color);
MainActivity.mEditText.setBackgroundColor(color);
}
@Override
public void onColorSelected(int dialogId, int color) {
Log.d("TAG", ""+color);
MainActivity.mEditText.setBackgroundColor(color);
}
}
以下是我的应用目前如何运作的视频: https://www.youtube.com/watch?v=lpYjKMLd9aU
我使用的拾色器库是:https://github.com/danielnilsson9/color-picker-view
答案 0 :(得分:0)
问题是您为ColorPickerDialogListener
实施ViewHolder
,但查看了库here的代码,您可以看到它尝试将activity
转换为ColorPickerDialogListener
,否则抛出异常。因此,请尝试在MainActivity
上实现此侦听器并查看结果。
<强>更新强>
因此,在这种情况下,您需要将ColorPickerDialogFrag2
的逻辑更改为以下内容:
...
private ColorPickerDialogListener mListener;
public static ColorPickerDialogFrag2 newInstance(ColorPickerDialogListener listener,
int dialogId, int initialColor) {
ColorPickerDialogFrag2 frag = new ColorPickerDialogFrag2();
frag.setListener(listener);
Bundle args = new Bundle();
args.putInt("id", dialogId);
args.putInt("init_color", initialColor);
frag.setArguments(args);
return frag;
}
public void setListener(ColorPickerDialogListener mListener) {
this.mListener = mListener;
}
...
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (mListener == null) {
// Check for listener in parent activity
try {
mListener = (ColorPickerDialogListener) activity;
} catch (ClassCastException e) {
e.printStackTrace();
throw new ClassCastException("Parent activity must implement "
+ "ColorPickerDialogListener to receive result.");
}
}
}
...
然后将ColorPickerDialogListener
作为参数传递给newInstance
方法:
@Override
public boolean onLongClick(View v) {
ColorPickerDialogFrag2 cp = ColorPickerDialogFrag2.newInstance(this, 6, Color.YELLOW);
cp.setStyle(android.support.v4.app.DialogFragment.STYLE_NORMAL, R.style.AppTheme);
cp.show(fragmentManager,"d");
return false;
}