Android在RelativeLayout中左右滑动

Android在RelativeLayout中左右滑动,第1张

概述我正在尝试在RelativeLayout中向左或向右滑动.我写了一些代码,但无法向左或向右滑动.我正在使用GestureDetector 这是我的来源 private GestureDetector gesturedetector = null;private RelativeLayout swipelayout;@SuppressLint("UseValueOf")@Overridep 我正在尝试在relativeLayout中向左或向右滑动.我写了一些代码,但无法向左或向右滑动.我正在使用GestureDetector
这是我的来源

private GestureDetector gesturedetector = null;private relativeLayout swipelayout;@Suppresslint("UseValueOf")@OverrIDepublic VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) {    VIEw rootVIEw = inflater.inflate(R.layout.sTrada_menu_result_loadmore,container,false);    swipelayout = (relativeLayout) rootVIEw.findVIEwByID(R.ID.swipelayout);    gesturedetector = new GestureDetector(new MyGestureListener());    swipelayout.setontouchListener(new OntouchListener() {        @OverrIDe        public boolean ontouch(VIEw v,MotionEvent event) {        gesturedetector.ontouchEvent(event);        return true;        }        });    return rootVIEw;}public boolean dispatchtouchEvent(MotionEvent ev){    return gesturedetector.ontouchEvent(ev);}class MyGestureListener extends GestureDetector.SimpleOnGestureListener {    private static final int SWIPE_MIN_disTANCE = 150;    private static final int SWIPE_MAX_OFF_PATH = 100;    private static final int SWIPE_THRESHolD_VELociTY = 100;    @OverrIDe    public boolean onFling(MotionEvent e1,MotionEvent e2,float veLocityX,float veLocityY) {        float dX = e2.getX() - e1.getX();        float dY = e1.getY() - e2.getY();        if (Math.abs(dY) >= SWIPE_THRESHolD_VELociTY                && Math.abs(veLocityX) >= SWIPE_THRESHolD_VELociTY &&                Math.abs(dX) >= SWIPE_MIN_disTANCE) {            if (dX > 0) {                Toast.makeText(getActivity(),"Right Swipe",Toast.LENGTH_SHORT).show();            } else {                Toast.makeText(getActivity(),"left Swipe",Toast.LENGTH_SHORT).show();            }            return true;        }        return false;    }}

我究竟做错了什么?如果有人知道解决方案,请帮助我
谢谢

解决方法 你只需要一个OntouchListener,这应该是简单的答案.看一看.
我在一些工作项目中使用了这个代码,所以一切都应该正常.但请告诉我是否有遗漏并且不起作用.

yourrelativeLayout.setontouchListener(new OntouchListener() {    int downX,upX;     @OverrIDe     public boolean ontouch(VIEw v,MotionEvent event) {         if (event.getAction() == MotionEvent.ACTION_DOWN) {             downX = (int) event.getX();              Log.i("event.getX()"," downX " + downX);             return true;         }          else if (event.getAction() == MotionEvent.ACTION_UP) {             upX = (int) event.getX();              Log.i("event.getX()"," upX " + upX);             if (upX - downX > 100) {                 // swipe right             }              else if (downX - upX > -100) {                 // swipe left             }             return true;             }             return false;         }     });
@H_404_33@ 总结

以上是内存溢出为你收集整理的Android在RelativeLayout中左右滑动全部内容,希望文章能够帮你解决Android在RelativeLayout中左右滑动所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1130055.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存