【问题标题】:Color picker wont change background color from RecyclerView Adapter?颜色选择器不会从 RecyclerView 适配器更改背景颜色?
【发布时间】:2016-07-18 20:19:40
【问题描述】:

我有一个应用程序,用户可以在其中使用颜色选择器或预定义按钮来更改引用的背景。我想让用户可以通过长按来更改预定义按钮的颜色,然后出现一个颜色选择器,用户现在可以选择将任何颜色保存到按钮。

由于某种原因,颜色选择器侦听器方法在我的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

【问题讨论】:

  • 你用什么来创建你的ColorPickerDialogFrag2?它是一个库还是你自己的自定义类?
  • @Bryan 哦,它是一个图书馆。我现在会附上它的链接。
  • 你能发布你的ColorPickerDialogFrag2代码吗?
  • @Bryan 给你! pastebin.com/iJpzdXM7

标签: android


【解决方案1】:

问题是您为您的ViewHolder 实现了ColorPickerDialogListener,但是查看库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;
}

【讨论】:

  • 好的好信息!我知道它在 MainActivity 中工作,但它必须在预定义按钮所在的适配器中工作。因此,当用户在 recyclerview 中长按某个预定义按钮时,他们可以更改颜色。
  • 一切正常!谢谢。但是你能解释一下为什么我们必须将它自己的监听器传递给 newInstance 吗?
  • 我们这样做是因为我们可以让每个对象都成为监听器,而不仅仅是活动
  • 不是fragment自己的监听器,这是来自参数的监听器
猜你喜欢
  • 1970-01-01
  • 2018-03-04
  • 2021-07-11
  • 1970-01-01
  • 1970-01-01
  • 2018-10-13
  • 1970-01-01
  • 2012-05-30
  • 1970-01-01
相关资源
最近更新 更多