2014-06-18 2 views
0

동작 표시 줄의 단일 항목에 제스처 수신기를 구현할 수 있습니까? 검색 항목에 제스처 수신기를 구현하고 싶습니다. 예를 들어 슬라이딩 검색 버튼을 사용하면 다른 동작이 열립니다. 기본 검색 옵션이 표시됩니다. 사용자 지정 작업 표시 줄을 만들거나 기본 작업 표시 줄을 사용하여 수행 할 수 있습니까?동작 표시 줄의 제스처 수신기 안드로이드

답변

0

비슷한 상황에서 누군가 도움이 필요할 경우를 대비해. 해결책입니다. - 사용자 지정 작업 표시 줄을 만듭니다.

<?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

에서