2017-04-10 41 views
0

我想点击添加到我的自定义项目:点击自定义项与Android的butterknife

public class ContactItem extends FrameLayout { 

    @BindView(R.id.itemHeader) 
    TextView textLabel; 
    @BindView(R.id.itemValue) 
    TextView textValue; 
    @BindView(R.id.imageIcon) 
    ImageView icon; 
    @BindView(R.id.mainLayout) 
    RelativeLayout mainLayout; 

    private String label = null; 

    public ContactItem(Context context) { 
     super(context); 

     init(context, null); 
    } 

    public ContactItem(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     init(context, attrs); 
    } 

    public ContactItem(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 

     init(context, attrs); 
    } 

    private void init(Context context, AttributeSet attrs) { 

     if (attrs != null) { 
      TypedArray a = context.obtainStyledAttributes(attrs, 
        R.styleable.textLabel); 

      label = a.getString(R.styleable.textLabel_text_attr); 

     } 

     addView(inflate(context, R.layout.item_contact, null)); 

     ButterKnife.bind(this); 

    } 

    public TextView getTextLabel() { 
     return textLabel; 
    } 

    public TextView getTextValue(){ 
     return textValue; 
    } 

    public ImageView getIcon(){ 
     return icon; 
    } 

    public void setTextLabelText(int text){ 
     textLabel.setText(text); 
    } 

    public void setTextValueText(String text){ 
     textValue.setText(text); 
    } 

    public void setIconRes(int res){ 
     icon.setImageResource(res); 
    } 

} 

在我的活动我加几件这种类型。

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:background="@color/color_white"> 

     <TextView 
      android:id="@+id/Header" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:paddingBottom="5dp" 
      android:paddingLeft="15dp" 
      android:paddingTop="20dp" 
      android:textAllCaps="true" 
      android:textStyle="bold" 
      android:background="@color/background"/> 

     <views.items.ContactItem 
      android:id="@+id/contactTelefon" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:clickable="true"/> 
     <views.items.ContactItem 
      android:id="@+id/contactEmail" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:clickable="true"/> 
    </LinearLayout> 

现在,如果我点击项目contactEmail我要发电子邮件,如果contactTelefon我要打个电话。我有功能,但onClick不起作用。有任何想法吗?

@OnClick(R.id.contactTelefon) 
public void clickOnCall(){ 
    presenter.callFunction()); 
} 

回答

0

我不知道它是与ButterKnife的问题,但你可以尝试添加android:duplicateParentState="true"ContactItem。这背后的想法是,您的顶部布局正在消耗点击事件之前,子布局获取相同的事件。

另一种方法是将clicklistener添加到LinearLayout中,例如

@OnClick(R.id.linearLayout) 
public void clickOnCall(View v) { 
    switch (v.getId()) { 
    case R.id.contactTelefon: 
     presenter.callFunction()); break; 
    } 
} 
+0

没有结果... :( – edi233

+0

@ edi233我编辑了我的答案 –

0

您应该承载您的ContactItem父视图进行ButterKnife.bind()。您的自定义类中的bind()是不同的绑定。

因此,如果这是一项活动,请将ButterKnife.bind(this)放入该活动中,此后您的@OnClick()将按预期工作。

相关问题