2017-05-05 151 views
0

我最近開始使用Fragments,所以我懷疑我的問題與它有關。我基本上有一個包含行列表(RelativeLayout類型)的DialogFragment。每行都有一個自定義視圖(包含畫布)和一個按鈕。我試圖附加一個OnClickListener,但我看到它沒有被觸發。我究竟做錯了什麼?onClick event not triggered

編輯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); 
    } 
} 
+0

也將'setClickable(true)'應用於'ColorChoiceRow'。 – azizbekian

+0

這沒有奏效 – Naci

回答

0

將溶液去除適配器(RowAdaptor),使用的LinearLayout代替。

而不是setAdapter方法,使用setView方法解決了我的問題。