自定义View中的ImageView中接收不到OnTouch事件,求怎么解决????悬赏!!!!100分

wangtao4226 2011-10-12 10:28:49
Activity中手势滑动的代码

/**
* 向左划动
*/
private void loadPageByLeft() {
if (pageIndxe < simpleAdapter.getCount() - 1) {

pageIndxe++;

loadGrid(pageIndxe);
page.setCheckPage(pageIndxe);//
viewFlipper.showNext();//
mCurrentLayoutState = pageIndxe;
}
}

/**
* 向右划动
*/
private void loadPageByRight() {
if (pageIndxe > 0) {
pageIndxe--;

loadGrid(pageIndxe);
page.setCheckPage(pageIndxe);
viewFlipper.showPrevious();
mCurrentLayoutState = pageIndxe;
}
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {

if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE
&& Math.abs(velocityX) > FLING_MIN_VELOCITY) {
loadPageByLeft();

} else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE
&& Math.abs(velocityX) > FLING_MIN_VELOCITY) {

loadPageByRight();
}
tableRowId = -1;
tableColumnId = -1;
appCount = adapter.getCount();
return false;
}

@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub

}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}

@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return mGestureDetector.onTouchEvent(event);
}



下面是自定义View的代码:


package com.yaomei.widget;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yaomei.activity.adapter.AppsAdapter;
import com.yaomei.activity.info.R;

public class gridViewExt extends LinearLayout {
public List<HashMap<String, Object>> tableRowsList;
private List<HashMap<String, Object>> app = new ArrayList<HashMap<String, Object>>();
private AppsAdapter adapter;

onItemClickListener onItemClickEvent;
onLongPressExt onLongPress;
int checkRowID = -1; // 选中行的下标
int checkColumnID = -1; // 选中列的下标
int lastRowCount = -1; // 最后一行的总数
private int ColumnCount; // 每列的总数

public void setColumnCount(int count) {
this.ColumnCount = count;
}

public int getColumnCount() {
return ColumnCount;
}

public interface onItemClickListener {
public boolean onItemClick(int position, MotionEvent event, View view);
}

public interface onLongPressExt {
public boolean onLongPress(View view);
}

public gridViewExt(Context context) {
this(context, null);
// TODO Auto-generated constructor stub
}

public gridViewExt(Context context, AttributeSet attrs) {
super(context, attrs);
int resouceID = -1;
TypedArray typedArray = context.obtainStyledAttributes(attrs,
R.styleable.GridViewExt);
int N = typedArray.getIndexCount();
for (int i = 0; i < N; i++) {
int attr = typedArray.getIndex(i);
switch (attr) {
case R.styleable.GridViewExt_ColumnCount:
resouceID = typedArray.getInt(
R.styleable.GridViewExt_ColumnCount, 0);
setColumnCount(resouceID);
break;

}
}
typedArray.recycle();
}

public void setOnItemClickListener(onItemClickListener click) {
this.onItemClickEvent = click;
}

public void setOnLongPressListener(onLongPressExt longPress) {
this.onLongPress = longPress;
}

public void NotifyDataChange() {
removeAllViews();
}

void bindView() {
removeAllViews();
int count = adapter.getCount();
TableCell[] cell = null;
int j = 0;
LinearLayout layout;
tableRowsList = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < count; i++) {
j++;
final int position = i;
if (j > getColumnCount() || i == 0) {
cell = new TableCell[getColumnCount()];
}

final View view = adapter.getView(i, null, null);

view.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
unCheckPressed();
checkRowID = -1;
checkColumnID = -1;
if (onItemClickEvent != null) {

onItemClickEvent.onItemClick(position, event, view);
}
return false;
}
});

view.setOnLongClickListener(new OnLongClickListener() {

@Override
public boolean onLongClick(View v) {
if (onLongPress != null) {
onLongPress.onLongPress(v);
}
return true;
}
});
cell[j - 1] = new TableCell(view);
if (j == getColumnCount()) {
lastRowCount = j;
j = 0;
HashMap<String, Object> map = new HashMap<String, Object>();
TableRow tr = new TableRow(cell);
map.put("tableRow", tr);
tableRowsList.add(map);
layout = new LinearLayout(getContext());
addLayout(layout, cell, tr.getSize(), tr);

} else if (i >= count - 1 && j > 0) {
lastRowCount = j;
HashMap<String, Object> map = new HashMap<String, Object>();
TableRow tr = new TableRow(cell);
map.put("tableRow", tr);
tableRowsList.add(map);
layout = new LinearLayout(getContext());
addLayout(layout, cell, j, tr);
}

}

}

private void addLayout(LinearLayout layout, TableCell[] cell, int size,
TableRow tr) {

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(180,
150);
layout.setGravity(Gravity.LEFT);

layout.setOrientation(LinearLayout.HORIZONTAL);
for (int k = 0; k < size; k++) {
View remoteView = (View) tr.getCellValue(k).getValue();
layout.addView(remoteView, k, params);
}
LinearLayout.LayoutParams firstParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
firstParams.leftMargin = 60;
addView(layout, firstParams);
}

public void setAdapter(AppsAdapter appsAdapter) {
this.adapter = appsAdapter;
this.setOrientation(LinearLayout.VERTICAL);
bindView();
}

public void checkPressed(int tableRowId, int tableRowColumnId) {
ViewGroup view = (ViewGroup) this.getChildAt(tableRowId);

checkColumnID = tableRowColumnId;
checkRowID = tableRowId;
changeImageState(view.getChildAt(tableRowColumnId), app);

}

public void onClick(int tableRowId, int tableRowColumnId, Context context) {
LinearLayout view = (LinearLayout) ((ViewGroup) this
.getChildAt(tableRowId)).getChildAt(tableRowColumnId);

TextView tv = (TextView) view.findViewById(R.id.folder);
final String[] name = tv.getText().toString().split("-");
Intent intent = null;
if (name[0].toString().equals("com.android.contacts")) {
if (name[1].toString().equals(
"com.android.contacts.DialtactsActivity")) {
intent = new Intent(Intent.ACTION_DIAL);
}
if (name[1].toString().equals(
"com.android.contacts.DialtactsContactsEntryActivity")) {
intent = new Intent(Intent.ACTION_CALL_BUTTON);
}
} else {
intent = getContext().getPackageManager()
.getLaunchIntentForPackage(name[0].toString());
}
context.startActivity(intent);

}

/**
* 改变图片状态
*
* @param v
* @param list
*/
private void changeImageState(View v, List<HashMap<String, Object>> list) {
int size = list.size();
for (int i = 0; i < size; i++) {
View view = (View) list.get(i).get("touch");
view.setPressed(false);
list.remove(i);
}
v.setPressed(true);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("touch", v);
list.add(map);

}

public void unCheckPressed() {
if (checkColumnID != -1 && checkRowID != -1) {
ViewGroup view = (ViewGroup) this.getChildAt(checkRowID);
view.getChildAt(checkColumnID).setPressed(false);

}
}

public class TableRow {
private TableCell[] cell;

public TableRow(TableCell[] cell) {
this.cell = cell;
}

public int getSize() {
return cell.length;
}

public TableCell getCellValue(int index) {
if (index >= getSize()) {
return null;
}
return cell[index];
}

public int getCellCount() {

return cell.length;

}

public int getLastCellCount() {
return lastRowCount;
}
}

static public class TableCell {
private Object value;

public TableCell(Object value) {
this.value = value;
}

public Object getValue() {
return value;
}
}

}





图片下面有一个textView,上图是没有显示出来的,在textview上面可以获取到OnTouch事件,为什么图片上获取不到呢????我在图片的事件中setOnTouch(){ return flase;}
...全文
1177 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
evil_mf 2012-09-20
  • 打赏
  • 举报
回复
不知道楼主是怎么解决的,我现在的问题是我自定义了一个view继承ImageView,在这个view中有onTouch事件,用于平移放大后的图片,而在Activity中我也有onToch事件,另外主要的因也有手势监听,onFling和onSingleTapup事件,现在我显示一张图片(没有放大),我在上面滑动,是view里的onTouch事件响应,而Activity中的onFling和onSingleTapup一点反应也没有。而我想要的是onFling事件响应
harhart 2012-08-20
  • 打赏
  • 举报
回复
LZ把解决方法贴出来啊
  • 打赏
  • 举报
回复
那里出错了呢?看了半天来了一个貌似解决了,还保密??
下次看谁还给你分析
wangtao4226 2011-10-13
  • 打赏
  • 举报
回复
此问题貌似已经解决了,结贴啦,非常感谢楼上各位!
念茜 2011-10-12
  • 打赏
  • 举报
回复
这个真是个有意思的问题。我没有接触过这块,高亮一下帖子,希望有做过的朋友分享下经验。
wangtao4226 2011-10-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xyh9717 的回复:]
楼主要疯了,等高人解决
[/Quote]

哈哈,是急疯了!!
wangtao4226 2011-10-12
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 yinnideai 的回复:]
顶一下、、、、
[/Quote]

陈敏不是我我同学么??
yinnideai 2011-10-12
  • 打赏
  • 举报
回复
顶一下、、、、
xyh9717 2011-10-12
  • 打赏
  • 举报
回复
楼主要疯了,等高人解决
wangtao4226 2011-10-12
  • 打赏
  • 举报
回复

如上图所示




关键是这是一个自定义的View,如果是一般在在.xml文件布局的图片都可以获取到Ontouch事件
想怎么滑动都是没问题的,碰到自定义的View,从Activity中的dispatchTouchEvent-------->View中Ontouch事件---------->传到View中图片的OnTOuch事件就莫名奇妙的拦截掉了,见鬼了还?????
wangtao4226 2011-10-12
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 fishmen26 的回复:]
你要是解决了,记得给我说一声哪里错了哈。
或者你可以把整个project发到我邮箱。 Jinfu_chen@qq.com
[/Quote]

绝对没问题啊,我的QQ:865508275
fishmen26 2011-10-12
  • 打赏
  • 举报
回复
你要是解决了,记得给我说一声哪里错了哈。
或者你可以把整个project发到我邮箱。 Jinfu_chen@qq.com
wangtao4226 2011-10-12
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 fishmen26 的回复:]
哥,代码好乱啊!!!

你说的自定义view的 onTouch事件就是这里吧?

view.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub……
[/Quote]

哥啊,非常感谢你啊!!等我把问题解决了,这100分肯定给你了,这个View的OnTouch事件貌似都没用??我已经把他全部注释掉了!
fishmen26 2011-10-12
  • 打赏
  • 举报
回复
哥,代码好乱啊!!!

你说的自定义view的 onTouch事件就是这里吧?

view.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
unCheckPressed();
checkRowID = -1;
checkColumnID = -1;
if (onItemClickEvent != null) {

onItemClickEvent.onItemClick(position, event, view);
}
return false;
}
});

这里return false 然后再传给对应的imageView 和TextView 做判断?
你可以在每个onTouch 和 onClick之类的判断动作的方法里面打log出来看。
看问题到底出在哪里
wangtao4226 2011-10-12
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 fishmen26 的回复:]
引用 13 楼 wangtao4226 的回复:
在图片上滑动,还是得不到OnTouch()事件???


那个OnTouch()事件? activity中的还是自定义view中的?
[/Quote]
自定义View中封装了ImageView和TextView
View发送OnTouch()事件--->ImageView接受从View传来的OnTouch事件,但是他接受不到,
View发送OnTouch()事件--->TextView接受从View传来的OnTouch事件,textView能接收到OnTouch()事件,不知道为什么???


fishmen26 2011-10-12
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 wangtao4226 的回复:]
在图片上滑动,还是得不到OnTouch()事件???
[/Quote]

那个OnTouch()事件? activity中的还是自定义view中的?
wangtao4226 2011-10-12
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 bibingyan 的回复:]
系统不是有实现手势识别吗。。
[/Quote]


这不是实现不了吗???黔驴技穷啊!!SOS
Jarrys 2011-10-12
  • 打赏
  • 举报
回复
系统不是有实现手势识别吗。。
wangtao4226 2011-10-12
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 fishmen26 的回复:]
你的imageview 设置
android:focusable="true"
android:focusableInTouchMode="true"
这两个属性没? 不设置是focus不到的

还有能多贴点代码不?
[/Quote]


<LinearLayout android:layout_width="match_parent"
android:orientation="horizontal" android:gravity="center"
android:layout_height="match_parent">
<com.yaomei.widget.ViewFlipper
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/myFlipper"></com.yaomei.widget.ViewFlipper>
</LinearLayout>

这是自定义ViewFlipper,添加一个一个View进行滑动!!!
wangtao4226 2011-10-12
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 fishmen26 的回复:]
你的imageview 设置
android:focusable="true"
android:focusableInTouchMode="true"
这两个属性没? 不设置是focus不到的

还有能多贴点代码不?
[/Quote]

还是获取不到,真是邪门了啊!!!
加载更多回复(5)

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧