我的理解是,当你的视图太小而不能轻易触摸时,你应该使用TouchDelegate来增加该视图的可点击区域.
但是,在Google上搜索使用示例会让很多人提出问题,但答案却很少.
有没有人知道为视图设置触摸委托的正确方法,比如说,在每个方向上将可点击区域增加4个像素?
emm*_*mby 98
我问过Google的一位朋友,他们能够帮我弄清楚如何使用TouchDelegate.以下是我们提出的建议:
final View parent = (View) delegate.getParent();
parent.post( new Runnable() {
// Post in the parent's message queue to make sure the parent
// lays out its children before we call getHitRect()
public void run() {
final Rect r = new Rect();
delegate.getHitRect(r);
r.top -= 4;
r.bottom += 4;
parent.setTouchDelegate( new TouchDelegate( r , delegate));
}
});
Run Code Online (Sandbox Code Playgroud)
Kyl*_*egg 20
我能够通过一个屏幕上的多个视图(复选框)完成此操作,主要来自此博客文章.基本上你采用emmby的解决方案并将其单独应用于每个按钮及其父级.
public static void expandTouchArea(final View bigView, final View smallView, final int extraPadding) {
bigView.post(new Runnable() {
@Override
public void run() {
Rect rect = new Rect();
smallView.getHitRect(rect);
rect.top -= extraPadding;
rect.left -= extraPadding;
rect.right += extraPadding;
rect.bottom += extraPadding;
bigView.setTouchDelegate(new TouchDelegate(rect, smallView));
}
});
}
Run Code Online (Sandbox Code Playgroud)
在我的情况下,我有一个图像视图的gridview,复选框覆盖在顶部,并调用方法如下:
CheckBox mCheckBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
final ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
// Increase checkbox clickable area
expandTouchArea(imageView, mCheckBox, 100);
Run Code Online (Sandbox Code Playgroud)
为我工作很棒.
Dmi*_*sev 10
该解决方案由@BrendanWeinstein在评论中发布.
而不是发送TouchDelegate
你可以覆盖你的getHitRect(Rect)
方法View
(如果你正在扩展一个).
public class MyView extends View { //NOTE: any other View can be used here
/* a lot of required methods */
@Override
public void getHitRect(Rect r) {
super.getHitRect(r); //get hit Rect of current View
if(r == null) {
return;
}
/* Manipulate with rect as you wish */
r.top -= 10;
}
}
Run Code Online (Sandbox Code Playgroud)
emmby的approch对我不起作用,但经过一些改动之后它做了:
private void initApplyButtonOnClick() {
mApplyButton.setOnClickListener(onApplyClickListener);
final View parent = (View)mApplyButton.getParent();
parent.post(new Runnable() {
@Override
public void run() {
final Rect hitRect = new Rect();
parent.getHitRect(hitRect);
hitRect.right = hitRect.right - hitRect.left;
hitRect.bottom = hitRect.bottom - hitRect.top;
hitRect.top = 0;
hitRect.left = 0;
parent.setTouchDelegate(new TouchDelegate(hitRect , mApplyButton));
}
});
}
Run Code Online (Sandbox Code Playgroud)
也许它可以节省一些人的时间
归档时间: |
|
查看次数: |
38359 次 |
最近记录: |