在开发过程中,遇到一个问题,listview的getview方法,返回的view是一个自定义的viewgroup,该viewgroup根据服务器返回的数据动态组合子view,当然每个子view按下的时候背景色需要变换,这个是通过为布局添加selector实现的,selector中只是简单区分了state_pressed的状态,分别给与图片,遇到的问题是,当按下子view的时候整个viewgroup背景色都变化了,想到的第一个问题就是touch事件的分发,于是弄了半天touch,改好了一版,但是有个问题就是,可能不是在按下的时候 背景色也会变一下,因为重写了touch时间的action_down,后来找到一个和press相关的api,遂把最终解决方案记录下,由于默认的dispatchSetPressed方法是 分发给了所有的子view,故需要重写:
1.重写viewgroup的 protected void dispatchSetPressed(boolean pressed)方法:
protected void dispatchSetPressed(boolean pressed) {
if (pressed) {
String tag = "";
for (int i = 0; i < vCard.getChildCount(); i++) {
ViewGroup viewGroup = (ViewGroup) vCard.getChildAt(i);
tag = (String) viewGroup.getTag();
if (tag != null && tag.equals("1")) {
viewGroup.setPressed(pressed);
}
}
} else {
String tag = "";
for (int i = 0; i < vCard.getChildCount(); i++) {
ViewGroup viewGroup = (ViewGroup) vCard.getChildAt(i);
tag = (String) viewGroup.getTag();
if (tag != null && tag.equals("1")) {
viewGroup.setPressed(pressed);
viewGroup.setTag("0");
}
}
}
}
if (pressed) {
String tag = "";
for (int i = 0; i < vCard.getChildCount(); i++) {
ViewGroup viewGroup = (ViewGroup) vCard.getChildAt(i);
tag = (String) viewGroup.getTag();
if (tag != null && tag.equals("1")) {
viewGroup.setPressed(pressed);
}
}
} else {
String tag = "";
for (int i = 0; i < vCard.getChildCount(); i++) {
ViewGroup viewGroup = (ViewGroup) vCard.getChildAt(i);
tag = (String) viewGroup.getTag();
if (tag != null && tag.equals("1")) {
viewGroup.setPressed(pressed);
viewGroup.setTag("0");
}
}
}
}
2.对子view进行tag设定,这需要重写子view的 public boolean onTouchEvent(MotionEvent event) 方法:
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
setTag("1");
}
if (event.getAction() == MotionEvent.ACTION_CANCEL) {
setTag("0");
}
if (event.getAction() == MotionEvent.ACTION_UP) {
setTag("0");
}
return super.onTouchEvent(event);
}
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
setTag("1");
}
if (event.getAction() == MotionEvent.ACTION_CANCEL) {
setTag("0");
}
if (event.getAction() == MotionEvent.ACTION_UP) {
setTag("0");
}
return super.onTouchEvent(event);
}
3.重写viewgroup的 public boolean onTouchEvent(MotionEvent event) 方法,目的是由于同一个子view可能会重复添加到viewgroup,故需要排他,当找到其中有tag为1的时候,其他所有子view的tag设为0。
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean isTouched = false;
int position = 0;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
for (int i = 0; i < vCard.getChildCount(); i++) {
View childView = vCard.getChildAt(i);
String tag = (String) childView.getTag();
if (tag != null && tag.equals("1")) {
isTouched = true;
position = i;
break;
}
}
if (isTouched) {
for (int i = 0; i < vCard.getChildCount(); i++) {
if (i == position) {
continue;
}
View childView = vCard.getChildAt(i);
childView.setTag("0");
}
}
} else if (event.getAction() == MotionEvent.ACTION_CANCEL
|| event.getAction() == MotionEvent.ACTION_UP) {
for (int i = 0; i < vCard.getChildCount(); i++) {
View childView = vCard.getChildAt(i);
childView.setTag("0");
}
}
return super.onTouchEvent(event);
}
public boolean onTouchEvent(MotionEvent event) {
boolean isTouched = false;
int position = 0;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
for (int i = 0; i < vCard.getChildCount(); i++) {
View childView = vCard.getChildAt(i);
String tag = (String) childView.getTag();
if (tag != null && tag.equals("1")) {
isTouched = true;
position = i;
break;
}
}
if (isTouched) {
for (int i = 0; i < vCard.getChildCount(); i++) {
if (i == position) {
continue;
}
View childView = vCard.getChildAt(i);
childView.setTag("0");
}
}
} else if (event.getAction() == MotionEvent.ACTION_CANCEL
|| event.getAction() == MotionEvent.ACTION_UP) {
for (int i = 0; i < vCard.getChildCount(); i++) {
View childView = vCard.getChildAt(i);
childView.setTag("0");
}
}
return super.onTouchEvent(event);
}
经过测试,达到了预期的需求,效果还可以,有问题还望耐心指正,新手..