Android 如何在framelayout上注册onTouchListener?
我使用FrameLayout在其他视图之上添加了一些视图 我想在这个布局上注册onTouchListener 我让活动实现在一个监听器上 但是当我触摸屏幕时,onTouch方法没有被调用!! 这是我的密码Android 如何在framelayout上注册onTouchListener?,android,touch-event,Android,Touch Event,我使用FrameLayout在其他视图之上添加了一些视图 我想在这个布局上注册onTouchListener 我让活动实现在一个监听器上 但是当我触摸屏幕时,onTouch方法没有被调用!! 这是我的密码 public class Mapactivity extends Activity implements OnClickListener, TranslationListner , OnTouchListener { @Override protected void
public class Mapactivity extends Activity implements OnClickListener,
TranslationListner , OnTouchListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_fragment);
FrameLayout mlayout;
mlayout = (FrameLayout) findViewById(R.id.map_root);
File Tfile = this.getFilesDir();
String Ttest = Tfile.getAbsolutePath();
StorageEntity entity = new StorageEntity("map", ".xml", null, "Shine"
+ File.separator + "Sites", false);
SiteMap map = null;
try {
map = MapParser.parseMap(entity, this);
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
boolean fm = map.equals(null);
MapView view = new MapView(this, map, 2);
view.registerTranslationListner(Mapactivity.this);
view.setBackgroundColor(Color.WHITE);
mlayout.addView(view);
AreaCentroid = map.getCenteroid(2);
for (Area area : map.getFloorMap(2).getAreas()) {
if (area.getAreaType() == Path.HALL) {
ImageView hIcon = new ImageView(this);
hIcon.setImageResource(R.drawable.hall);
hIcon.setPadding(area.polygon.centeroid().x,
area.polygon.centeroid().y, 0, 0);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
hIcon.setLayoutParams(lp);
icons.add(hIcon);
mlayout.addView(hIcon);
}
}
view.registerTranslationListner(Mapactivity.this);
mlayout.setOnTouchListener(Mapactivity.this);
}
我找到了答案
处理运动事件的方法是创建扩展framelayout的自定义布局
那样
public class MapLayout extends FrameLayout {
public MapLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MapLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public MapLayout(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
}
然后重写onInterceptTouchEvent,它调用此布局中任何子视图上发生的任何触摸事件
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
mScaleDetector.onTouchEvent(ev);
mMoveDetector.onTouchEvent(ev);
for (View childView : childerns) {
}
return false;
}
没有布局也很难排除故障。我建议确保您的框架布局已启用。此外,您还可以玩和