Mam dwa ImageButtons, każdy wewnątrz RelativeLayout i te dwa RelativeLayouts są w innym RelativeLayout, chcę ustawić TouchDelegate dla każdego ImageButton. Jeśli normalnie dodaję TouchDelegate do każdego ImageButton i jego rodzica RelativeLayout, to tylko jeden ImageButton działa poprawnie, Inny nie rozszerza jego obszaru klikania. Więc PLease pomoże mi w użyciu TouchDelegate w obu ImageButtons. Jeśli nie jest to możliwe, jaki może być skuteczny sposób rozszerzenia obszaru klikania widoku? Z góry dzięki ........Jak korzystać z wielu TouchDelegate
Tu jest mój kodu xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/FrameContainer"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout android:layout_alignParentLeft="true"
android:id="@+id/relativeLayout3" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout android:layout_alignParentLeft="true"
android:id="@+id/relativeLayout1" android:layout_width="113dip"
android:layout_height="25dip">
<ImageButton android:id="@+id/tutorial1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@null" android:src="@drawable/tutorial" />
</RelativeLayout>
<RelativeLayout android:layout_alignParentLeft="true"
android:id="@+id/relativeLayout2" android:layout_width="113dip"
android:layout_height="25dip" android:layout_marginLeft="100dip">
<ImageButton android:id="@+id/tutorial2"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@null" android:src="@drawable/tutorial"
android:layout_marginLeft="50dip" />
</RelativeLayout>
</RelativeLayout>
Moja klasa aktywny:
import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.TouchDelegate;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
public class TestTouchDelegate extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View mParent1 = findViewById(R.id.relativeLayout1);
mParent1.post(new Runnable() {
@Override
public void run() {
Rect bounds1 = new Rect();
ImageButton mTutorialButton1 = (ImageButton) findViewById(R.id.tutorial1);
mTutorialButton1.setEnabled(true);
mTutorialButton1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(TestTouchDelegate.this, "Test TouchDelegate 1", Toast.LENGTH_SHORT).show();
}
});
mTutorialButton1.getHitRect(bounds1);
bounds1.right += 50;
TouchDelegate touchDelegate1 = new TouchDelegate(bounds1, mTutorialButton1);
if (View.class.isInstance(mTutorialButton1.getParent())) {
((View) mTutorialButton1.getParent()).setTouchDelegate(touchDelegate1);
}
}
});
//View mParent = findViewById(R.id.FrameContainer);
View mParent2 = findViewById(R.id.relativeLayout2);
mParent2.post(new Runnable() {
@Override
public void run() {
Rect bounds2 = new Rect();
ImageButton mTutorialButton2 = (ImageButton) findViewById(R.id.tutorial2);
mTutorialButton2.setEnabled(true);
mTutorialButton2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(TestTouchDelegate.this, "Test TouchDelegate 2", Toast.LENGTH_SHORT).show();
}
});
mTutorialButton2.getHitRect(bounds2);
bounds2.left += 50;
TouchDelegate touchDelegate2 = new TouchDelegate(bounds2, mTutorialButton2);
if (View.class.isInstance(mTutorialButton2.getParent())) {
((View) mTutorialButton2.getParent()).setTouchDelegate(touchDelegate2);
}
}
});
}
}
Próbowałem przesłonić getHitRect, ale nie działa (nie jest w ogóle wywoływany). Zasadniczo mogę utworzyć niestandardowy widok, który rozszerza ImageButton, a następnie w klasie I przesłonię getHitRect (z dodanym dopełnieniem), ale jak już wspomniałem, getHitRect nigdy nie zostanie wywołany. Czy można opublikować swoje rozwiązanie? – stephenteh
Okazuje się, że oferowane przeze mnie rozwiązanie nie działa w ICS, może dlatego nie działa dla Ciebie. Napisałem post na blogu na temat dwóch różnych strategii Delegatów dotykowych http://www.brendanweinstein.me/2012/06/26/touchdelegate-and-gethitrect-making-buttons-with-expanded-touch-areas/ i możesz pobrać próbkę kod dla obu strategii na https://github.com/brendanw/Touch-Delegates/blob/master/src/com/brendan/touchdelegates/MyTouchDelegate.java –
To nie odpowiada na pytanie OP. OP nie próbuje dodać wielu delegatów do jednego widoku. Dwa '' ImageButton''s w przykładzie OP mają * różnych * rodziców. – zyamys