如何在framelayout上注册onTouchListener?

时间:2014-06-26 20:03:27

标签: android ontouchevent

我正在使用FrameLayout,但有些视图高于其他视图 我想在这个布局上注册onTouchListener 我让活动实现了OnTouchListener 但是当我触摸屏幕时,“onTouch”方法没有调用!! 这是我的代码

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);

}

1 个答案:

答案 0 :(得分:0)

我找到了答案 处理运动事件的方法是进行扩展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;
    }