在Fragment中未触发OnClickListener

时间:2016-11-30 12:52:51

标签: java android android-actionbar onclicklistener

我有自定义类(ActionBar.class),其中我使用OnClickListener(OnBackButtonClickListener)实现了自定义ActionBar组件。当我在片段中调用此方法时,它不会被触发。

这是我的自定义类:

public class ActionBar extends LinearLayout {

  private boolean showBackButton;
  private boolean showXButton;
  private boolean showHeader;
  private String header = "";
  private OnClickListener onBackButtonClickListener;
  private OnClickListener onXButtonClickListener;

  @Bind(R.id.back) ImageView backButton;
  @Bind(R.id.header) AutoResizeTextView headerText;
  @Bind(R.id.x_btn) ImageView xButton;

  public ActionBar(Context context) {
    super(context);
    initialize(context, null);
  }

  public ActionBar(Context context, AttributeSet attrs) {
    super(context, attrs);
    initialize(context, attrs);
  }

  public ActionBar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initialize(context, attrs);
  }

  public ActionBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    initialize(context, attrs);
  }


  private void initialize(Context context, AttributeSet attributeSet) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.action_bar_layout, this);
    ButterKnife.bind(this, view);

    if(attributeSet != null) {
      TypedArray typedArray =
          context.getTheme().obtainStyledAttributes(attributeSet, R.styleable.ActionBar, 0, 0);

      try {
        setHeader(typedArray.getString(R.styleable.ActionBar_header) == null ?
            "" : typedArray.getString(R.styleable.ActionBar_header));
        setShowBackButton(typedArray.getBoolean(R.styleable.ActionBar_show_back_button, false));
        setShowXButton(typedArray.getBoolean(R.styleable.ActionBar_show_x_button, false));
        setShowHeader(typedArray.getBoolean(R.styleable.ActionBar_show_header, false));
      } finally {
        typedArray.recycle();
      }
    }
  }

  public boolean isShowBackButton() {
    return showBackButton;
  }

  public void setShowBackButton(boolean showBackButton) {
    this.showBackButton = showBackButton;
    invalidate();
    requestLayout();
  }

  public boolean isShowXButton() {
    return showXButton;
  }

  public void setShowXButton(boolean showXButton) {
    this.showXButton = showXButton;
    invalidate();
    requestLayout();
  }

  public boolean isShowHeader() {
    return showHeader;
  }

  public void setShowHeader(boolean showHeader) {
    this.showHeader = showHeader;
    invalidate();
    requestLayout();
  }

  public String getHeader() {
    return header;
  }

  public void setHeader(String header) {
    this.header = header;
    invalidate();
    requestLayout();
  }

  public OnClickListener getOnBackButtonClickListener() {
    return onBackButtonClickListener;
  }

  public void setOnBackButtonClickListener(OnClickListener onBackButtonClickListener) {
    this.onBackButtonClickListener = onBackButtonClickListener;
  }

  public OnClickListener getOnXButtonClickListener() {
    return onXButtonClickListener;
  }

  public void setOnXButtonClickListener(OnClickListener onXButtonClickListener) {
    this.onXButtonClickListener = onXButtonClickListener;
  }

  @Override protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    backButton.setVisibility(showBackButton ? VISIBLE : INVISIBLE);
    xButton.setVisibility(showXButton ? VISIBLE : INVISIBLE);
    headerText.setVisibility(showHeader ? VISIBLE : INVISIBLE);
    headerText.setText(header);
  }

  @OnClick(R.id.x_btn) void onXButtonClick(View v) {
    if(getOnBackButtonClickListener() != null) {
      getOnBackButtonClickListener().onClick(v);
    }
  }

  @OnClick(R.id.back) void onBackButtonClick(View v) {
    if(getOnXButtonClickListener() != null) {
      getOnXButtonClickListener().onClick(v);
    }
  }
}

这是我的片段和方法setOnBackButtonClickListener调用:

public class ForgotPasswordFragment extends BasePagerFragment
        implements ForgotPasswordView, TextView.OnEditorActionListener, ActionBar.OnClickListener{

 @Bind(R.id.action_bar) ActionBar actionBar;


 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);


 actionBar.setOnBackButtonClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            navigateTo(LoginActivity.class);

        }
    });



}

2 个答案:

答案 0 :(得分:0)

你有

@OnClick(R.id.x_btn) void onXButtonClick(View v) {
    if(getOnBackButtonClickListener() != null) {
      getOnBackButtonClickListener().onClick(v);
    }
  }

  @OnClick(R.id.back) void onBackButtonClick(View v) {
    if(getOnXButtonClickListener() != null) {
      getOnXButtonClickListener().onClick(v);
    }
  }

但它看起来应该是:

@OnClick(R.id.back) void onXButtonClick(View v) {
    if(getOnBackButtonClickListener() != null) {
      getOnBackButtonClickListener().onClick(v);
    }
  }

  @OnClick(R.id.x_btn) void onBackButtonClick(View v) {
    if(getOnXButtonClickListener() != null) {
      getOnXButtonClickListener().onClick(v);
    }
  }

答案 1 :(得分:-1)

{*(Model)}.findById(id, function(err, animal) {
                if (err) console.log(err);

                {update new values here in animal with the ones you are receiving in the request}
                animal.save(function(err) {
                    if (err) console.log(err);
                });
            });