我已经实现了 Sherlock 操作栏选项卡视图。现在我想使用手势检测器滑动选项卡。我的选项卡视图工作正常。但是我想在使用手势列表器滑动屏幕时触发 tabselected 事件。这里是代码片段。
package com.cdotsys.newsfeed;
public class HomeActivity extends SherlockFragmentActivity implements OnTouchListener,ActionBar.TabListener {
ActionBar.Tab Tab1,Tab2;
GestureDetector gestureDetector;
View mainview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ActionBar actionBar = getSupportActionBar();
Tab1 = getSupportActionBar().newTab();
Tab2 = getSupportActionBar().newTab();
gestureDetector = new GestureDetector(this, new MyGestureDetector());
mainview = (View) findViewById(R.id.test);
mainview.setOnTouchListener(this);
// Hide Actionbar Icon
actionBar.setDisplayShowHomeEnabled(false);
// Hide Actionbar Title
actionBar.setDisplayShowTitleEnabled(false);
// Create Actionbar Tabs
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set Tab Icon and Titles
Tab1.setText("Tab1");
Tab2.setText("Tab2");
// Set Tab Listeners
Tab1.setTabListener(this);
Tab2.setTabListener(this);
// Add tabs to actionbar
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);
}
class MyGestureDetector extends SimpleOnGestureListener implements OnGestureListener {
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
@Override
public boolean onFling(final MotionEvent e1, final MotionEvent e2, final float velocityX, final float velocityY) {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) { return false; }
/* positive value means right to left direction */
final float distance = e1.getX() - e2.getX();
final boolean enoughSpeed = Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY;
if(distance > SWIPE_MIN_DISTANCE && enoughSpeed) {
// right to left swipe
return true;
} else {
// oooou, it didn't qualify; do nothing
return false;
}
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (gestureDetector.onTouchEvent(event)){
System.out.println("Working!!");
return true;
}
return false;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
Log.d("SimpleActionBarTabsActivity","tab "
+ String.valueOf(tab.getPosition()) + " clicked");
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
Log.d("SimpleActionBarTabsActivity","tab "
+ String.valueOf(tab.getPosition()) + " un-clicked");
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
这是我的 XML 文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".HomeActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
这里我使用了 View mainview = (View) findViewById(R.id.test);
这里的测试是我用作 setContentView 的相对布局的 id。
这是对的吗?为了实现使用gesturelistner触发的tabselected事件,我需要进行哪些修改?
请帮忙 !!