onClick事件未触发

时间:2017-05-05 18:47:40

标签: android android-fragments onclicklistener

我最近开始使用Fragments,所以我怀疑我的问题与它有关。我基本上有一个DialogFragment包含一个行列表(RelativeLayout类型)。每行都有一个自定义视图(包含画布)和一个按钮。我试图附加一个OnClickListener,但我发现它没有被触发。我做错了什么?

编辑2017年5月8日: 我在某种程度上得到了OnClick事件。我现在的问题是事件不会立即被触发。如果我单击几个按钮,然后只有当我点击AlertGialog中的其他位置时,事件才会被一起触发。

MainActivity.java

public class MainActivity extends AppCompatActivity implements ColorChoiceRow.OnColorButtonClicked {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager fm = getSupportFragmentManager();
        ColorDialogFragment colorDialogFragment = ColorDialogFragment.newInstance("Select a Color");
        colorDialogFragment.show(fm, "fragment_edit_name");
    }

    @Override
    public void OnColorButtonClicked(String name) {
        Log.d("ColorDebug", "Hey, you selected " + name + "!");
    }
}

ColorDialogFragment.java

public class ColorDialogFragment extends DialogFragment {
    private final String TAG = this.getClass().getSimpleName();
    private List<ColorChoiceRow> listColorChoiceRows;

    public ColorDialogFragment() {
    }

    public static ColorDialogFragment newInstance(String title) {
        ColorDialogFragment frag = new ColorDialogFragment();
        Bundle args = new Bundle();
        args.putString("title", title);
        frag.setArguments(args);
        return frag;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_color_ramp_dialog, container);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        listColorChoiceRows = new ArrayList<ColorChoiceRow>();
        listColorChoiceRows.add(new ColorChoiceRow(getContext(), Color.BLUE, "Blue"));
        listColorChoiceRows.add(new ColorChoiceRow(getContext(), Color.CYAN, "Cyan"));
        listColorChoiceRows.add(new ColorChoiceRow(getContext(), Color.BLACK, "Black"));
        listColorChoiceRows.add(new ColorChoiceRow(getContext(), Color.YELLOW, "Yellow"));
        String title = getArguments().getString("title");
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
        alertDialogBuilder.setTitle(title);

        alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (dialog != null) {
                    dialog.dismiss();
                }
            }
        });
        alertDialogBuilder.setAdapter(new RowAdapter(getContext(), listColorChoiceRows),
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        return alertDialogBuilder.create();
    }
}

ColorChoiceRow.java

public class ColorChoiceRow extends RelativeLayout {

    private ColorView colorView;
    private int color;
    private OnColorButtonClicked mListener;

    public ColorChoiceRow(final Context context, int color, final String text) {
        super(context);
        this.color = color;

        if (context instanceof OnColorButtonClicked) {
            mListener = (OnColorButtonClicked) context;
        } else {
            throw new ClassCastException(context.toString() + " must implement OnRageComicSelected.");
        }

        this.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        //this.setPadding(10, 10, 10, 0);

        colorView = new ColorView(context, color);
        colorView.setId(1);

        Button invertButton = new Button(context);
        invertButton.setId(3);
        invertButton.setClickable(true);
        invertButton.setText("Color Button");
        invertButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.OnColorButtonClicked(text);

            }
        });


        LayoutParams paramsColorRamp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        paramsColorRamp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        //paramsAlignLeft.addRule(RelativeLayout.);
        //colorView.setLayoutParams(paramsColorRamp);

        LayoutParams paramsInvertButton = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        paramsInvertButton.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        invertButton.setLayoutParams(paramsInvertButton);

        this.addView(colorView, paramsColorRamp);
        this.addView(invertButton, paramsInvertButton);
    }

    public interface OnColorButtonClicked {
        void OnColorButtonClicked(String name);
    }
}

ColorView.java

public class ColorView extends View {
    private int color;
    public ColorView(Context context, int color) {
        super(context);
        this.color = color;
    }

    @Override
    public void onDraw(Canvas canvas) {
        Paint paint = new Paint();
        paint.setColor(color);
        canvas.drawRect(0,0, 300, 100, paint );
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(300, 100);
    }
}

RowAdapter.java

public class RowAdapter extends BaseAdapter
{
    private List<ColorChoiceRow> colorChoiceRowList;
    private Context context;

    public RowAdapter(Context c, List<ColorChoiceRow> colorChoiceRows) {
        this.colorChoiceRowList = colorChoiceRows;
        this.context = c;
    }

    @Override
    public int getCount() {
        return colorChoiceRowList.size();
    }

    @Override
    public Object getItem(int position) {
        return colorChoiceRowList.get(position);
    }

    @Override
    public long getItemId(int id) {
        return id;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return colorChoiceRowList.get(position);
    }
}

1 个答案:

答案 0 :(得分:0)

解决方案是删除适配器(RowAdaptor)并改为使用LinearLayout。

使用setView方法解决了我的问题,而不是setAdapter方法。