【问题标题】:Gesture Listener on Action Bar Android动作栏 Android 上的手势监听器
【发布时间】:2014-06-18 07:57:53
【问题描述】:

是否可以在操作栏上的单个项目上实现手势侦听器?我想在搜索项上实现手势侦听器,即在滑动搜索按钮上打开一个不同的活动,并且只点击一次,它会显示默认搜索选项。 我是否需要创建自定义操作栏,或者可以使用默认操作栏来完成。

【问题讨论】:

    标签: android android-actionbar gesture gesture-recognition


    【解决方案1】:

    以防万一有人在类似情况下需要帮助。这是解决方案: - 创建自定义操作栏。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_gravity="fill_horizontal"
    android:focusable="true" >
    
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|fill_horizontal" >
    
        <ImageView
            android:id="@+id/search_query"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:background="@drawable/ic_communities"
    
            />
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|center_vertical"
            android:src="@drawable/ic_launcher" />
    
    </FrameLayout>
    

    创建手势监听类:-

      public class Swipe_listener implements OnTouchListener {
    
    
    protected final GestureDetector gestureDetector;
    
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        return gestureDetector.onTouchEvent(event);
    }
    
    
    public Swipe_listener(Context context) {
        gestureDetector = new GestureDetector(context, new GestureListener());
    }
    
    public void onSwipeLeft() {
    }
    
    public void onSwipeRight() {
    }
    
    
    private final class GestureListener extends SimpleOnGestureListener {
    
        private static final int SWIPE_DISTANCE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;
    
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
    
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        //  Log.d("HELLOHELLOHELLOGELLO", "Here I am");
            float distanceX = e2.getX() - e1.getX();
            float distanceY = e2.getY() - e1.getY();
            if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                if (distanceX > 0)
                    onSwipeRight();
                else
                    onSwipeLeft();
                return true;
            }
            return false;
        }
    }
    }
    

    在主要活动中:-

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setIcon(R.drawable.ic_launcher);
    
        LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflator.inflate(R.layout.custom_action_bar, null);
    
    
        actionBar.setCustomView(v);
    
        v.setOnTouchListener(new Swipe_listener(getApplicationContext()){
    
            public void onSwipeRight() {
               // Toast.makeText(MainActivity.this, "right", Toast.LENGTH_SHORT).show();
                Intent miN=new Intent(MainActivity.this,Prefer_circular.class);
                startActivity(miN);
            }
            public void onSwipeLeft() {
             //   Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show();
            }
    
    
        public boolean onTouch(View v, MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
        });
    

    代码取自 stackoverflow.com 的多个问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-06
      • 1970-01-01
      • 1970-01-01
      • 2013-10-25
      • 2021-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多