是否可以在Action Bar上的单个项目上实现Gesture Listener?我想在搜索项目上实现Gesture监听器,即在滑动搜索按钮上打开一个不同的活动,并且仅点击一次,它会显示默认搜索选项。 我是否需要创建自定义操作栏,或者可以使用默认操作栏完成。
答案 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>
创建Gesture监听器类: -
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的多个问题的代码