2017-04-10 5 views
0

Ich möchte, klicken Sie auf meine benutzerdefinierte Element hinzuzufügen:Klicken Sie auf benutzerdefinierte Artikel mit butter android

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); 
    } 

} 

In meiner Tätigkeit ich diese Art einige Elemente hinzuzufügen.

<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> 

jetzt, wenn ich auf Artikel CONTACT klicken Ich möchte E-Mail senden und wenn contactTelefon möchte ich einen Anruf tätigen. Ich habe Funktionen, aber onClick funktioniert nicht. Irgendwelche Ideen?

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

Antwort

0

Ich bin nicht sicher, ob es mit Buttermesser ein Problem ist, aber man könnte versuchen android:duplicateParentState="true" zu Ihrem ContactItem hinzufügen. Die Idee dahinter ist, dass das oberste Layout das click -Ereignis verbraucht, bevor das untergeordnete Layout dasselbe Ereignis erhält.

Ein anderer Ansatz wäre, den Clicklistener dem LinearLayout hinzuzufügen, z.

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

keine Ergebnisse ... :( – edi233

+0

@ edi233 Ich habe meine Antwort bearbeitet –

0

Sie sollten ButterKnife.bind() auf der übergeordneten Ansicht durchführen, die Ihre ContactItem hostet. Die bind() innerhalb Ihrer benutzerdefinierten Klasse ist eine andere Bindung.

Also, wenn dies eine Aktivität ist, setzen Sie ButterKnife.bind(this) in dieser Aktivität, und danach Ihre @OnClick() würde funktionieren wie erwartet.

Verwandte Themen