2011-09-26 1 views
2

탭이 3 개인 탭 기능이 있습니다. 탭 중 하나에는 ListActivity가 있고 다른 두 개의 탭에는 간단한 활동이 들어 있습니다. 그리고 나는 Fling 이벤트의 탭을 변경하기 위해 OnGestureListener를 구현했습니다.Android : 플링 제스처 이벤트의 탭 변경 - TabActivity 내부의 ListActivity에 대해 작동하지 않습니다.

onTouchEvent 및 'onFling'이벤트는 ListActivity가있는 탭에 대해 호출되지 않습니다. 그러나 Simple Activity (목록 없음)에 대해서는 정상적으로 작동합니다.

주요 활동 클래스 :

public class GestureTest extends TabActivity implements OnGestureListener { 
    private TabHost tabHost; 
    private GestureDetector gestureScanner; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.testmainlayoutforgesture); 
     gestureScanner = new GestureDetector(this); 
     addTabs(); 
    } 

    private void addTabs() { 
     Intent i1 = new Intent().setClass(this, SimpleListActivity.class); 
     Intent i2 = new Intent().setClass(this, SimpleActivity.class); 
     Intent i3 = new Intent().setClass(this, SimpleActivity.class); 

     tabHost = getTabHost(); 
     tabHost.addTab(tabHost.newTabSpec("First").setIndicator("First").setContent(i1)); 
     tabHost.addTab(tabHost.newTabSpec("Second").setIndicator("Second").setContent(i2)); 
     tabHost.addTab(tabHost.newTabSpec("Third").setIndicator("Third").setContent(i3)); 

     tabHost.setCurrentTab(0); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent me) { 
     return gestureScanner.onTouchEvent(me); 
    } 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     float dispX = e2.getX() - e1.getX(); 
     float dispY = e2.getY() - e1.getY(); 
     if (Math.abs(dispX) >= 200 && Math.abs(dispY) <= 100) { 
      // swipe ok 
      if (dispX > 0) { 
       // L-R swipe 
       System.out.println("L-R swipe"); 
       changeLtoR(); 
      } else { 
       // R-L swipe 
       System.out.println("R-L swipe"); 
      } 
     } 
     return true; 
    } 

    private void changeLtoR() { 
     int curTab = tabHost.getCurrentTab(); 
     int nextTab = ((curTab + 1) % 3); 
     tabHost.setCurrentTab(nextTab); 

    } 
//other interface methods  
} 

SimpleListActivity.java :

public class SimpleListActivity extends ListActivity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     List<String> list1Strings = new ArrayList<String>(); 
     list1Strings.add("List 11"); 
     list1Strings.add("List 12"); 
     list1Strings.add("List 13"); 
     list1Strings.add("List 14"); 
     setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings)); 
    } 
} 

SimpleActivity.java :

,691 여기

내 코드입니다 363,210
public class SimpleActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.simplelayouttest); 
    } 
} 

testmainlayoutforgesture.xml 레이아웃 :

<?xml version="1.0" encoding="utf-8"?> 
<TabHost 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 
     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
     </FrameLayout> 
    </LinearLayout> 
</TabHost> 

simplelayouttest.xml 레이아웃 :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView 
     android:id="@+id/helloTv" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Hello Hello" /> 
</LinearLayout> 

내가 여기에 잘못이 무엇인지 알아낼 수 없었다. 당신의 TabActivity이 추가

답변

6

보십시오 :이 구현

@Override 
public boolean dispatchTouchEvent(MotionEvent ev) { 
    if (gestureScanner != null) { 
     if (gestureScanner.onTouchEvent(ev)) 
      return true; 
    } 
    return super.dispatchTouchEvent(ev); 
} 

는 gestureScanner가 onFling 이벤트를 소비하는 장치를 알려줍니다. 다시

편집 : 당신은 또한 조금이에 onFling-방법을 변경해야합니다

:

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(MotionEvent e1, MotionEvent e2, float velocityX, 
      float velocityY) { 

     // Check movement along the Y-axis. If it exceeds SWIPE_MAX_OFF_PATH, then dismiss the swipe. 
     if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) 
      return false; 

     // Swipe from right to left. 
     // The swipe needs to exceed a certain distance (SWIPE_MIN_DISTANCE) and a certain velocity (SWIPE_THRESHOLD_VELOCITY). 
     if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE 
       && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
      // do stuff 
      return true; 
     } 

     // Swipe from left to right. 
     // The swipe needs to exceed a certain distance (SWIPE_MIN_DISTANCE) and a certain velocity (SWIPE_THRESHOLD_VELOCITY). 
     if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE 
       && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
      // do stuff 
      return true; 
     } 

     return false; 
    } 
}  
당신의 ListView는 '화면을 작성하지 않는 경우

또 다른 가능성은있을 수 빈 '공간을 클릭 할 수 없으며이 빈 공간에서 손가락을 떼면 터치 이벤트가 등록되고 발송되지 않습니다. ListView 위에 뛰어 다니면 작동합니까?

+0

감사합니다. 나는 너의 '또 다른 가능성'을 이해할 수 없었다. 네, 내 원래의 코드는 내 앱의 헤더 (앱 이름이 표시되는 섹션)에서 튀어 오르는 경우에 fling 이벤트에서 작동했습니다. – gtiwari333

+0

하지만이 방법으로 또 다른 문제점을 발견했습니다. 항목 중 아무 것도 터치 할 수 없습니다. 즉, 목록 항목과 탭을 선택 (터치) 할 수 없습니다. – gtiwari333

+0

여기에 솔루션을 게시 할 수 있습니까? – kaspermoerch

관련 문제