2016-12-07 58 views
0

在我的應用程序中,我創建了底部選項卡。片段用於顯示每個選項卡的內容。一切工作正常,如果我點擊標籤。但我想實現「向左/向右滑動」的格式,以便用戶可以輕掃以從一個Tab切換到另一個Tab。
我已經創建了自定義手勢檢測器用於處理所有MotionEvent [主要onFling()]。ListView投擲不起作用


public class OnSwipeTouchListener implements GestureDetector.OnGestureListener { 

private static final int SWIPE_THRESHOLD = 100; 
private static final int SWIPE_VELOCITY_THRESHOLD = 100; 

@Override 
public boolean onDown(MotionEvent motionEvent) { 
    return true; 
} 

@Override 
public void onShowPress(MotionEvent motionEvent) { 

} 

@Override 
public boolean onSingleTapUp(MotionEvent motionEvent) { 
    return true ; 
} 

@Override 
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) { 
    return true; // No difference if I make it false 
} 

@Override 
public void onLongPress(MotionEvent motionEvent) { 

} 

@Override 
public boolean onFling(MotionEvent e1, MotionEvent e2, float dVelocityX, float dVelocityY) { 
    boolean bResult = false; 
    try 
    { 
     float dDifferenceY = e2.getY() - e1.getY(); 
     float dDifferenceX = e2.getX() - e1.getX(); 

     if(Math.abs(dDifferenceX) > Math.abs(dDifferenceY)) 
     { 
      if((Math.abs(dDifferenceX) > SWIPE_THRESHOLD) && (Math.abs(dVelocityX) > SWIPE_VELOCITY_THRESHOLD)) 
      { 
       if(dDifferenceX > 0) 
       { 
        onSwipeLeft(); 
       } 
       else 
       { 
        onSwipeRight(); 
       } 
      } 
      bResult = true; 
     } 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 

    return bResult; 
} 

public abstract void OnCustomClick(); 


public void onSwipeRight() 
{ 
    // Code for Right Swipe. 
} 

public void onSwipeLeft() 
{ 
    // Code for Left Swipe. 
} } 


我的ListView的片段。由於我想爲列表項目執行Click事件,因此我的Coustom手勢檢測器被分配給所有列表項目。將我的手勢檢測器分配給列表視圖不會對列表項執行「onItemClick」事件。

我想我的ListView應該滾動以及檢測滑動手勢。我們在WhatsApp應用程序中觀察到同樣的情況,其中ListView平滑滾動以及平滑滑動發生。 (我知道,在我的情況下,標籤是自定義和底部位置。)

問題是,即使我執行向左/向右滑動,listview正在檢測滾動。它會執行一次或兩次刷卡,當我嘗試使用此密碼10次或更多次時。在仿真器上它可以很好地工作,但是在設備上面臨這個問題

任何人都可以告訴我爲什麼onFling()每次執行滑動操作時都不會調用?我肯定錯過了什麼。任何幫助表示讚賞。提前致謝。

回答

0

對於滑動標籤,你可以使用viewPager有一些已經寫好的代碼,你可以從SlidingTabLayoutSidingTabStrip得到。將這兩個文件複製到您的android項目中並創建一個適配器: 1.創建一個適配器。

public class ViewPagerAdapter extends FragmentStatePagerAdapter { 
private DetailFragmentOne tabOne; 
private DetailFragmentTwo tabTwo; 
private DetailFragmentThree tabThree; 

CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created 
int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created 


// Build a Constructor and assign the passed Values to appropriate values in the class 
public ViewPagerAdapter(FragmentManager fm, CharSequence mTitles[], int mNumbOfTabsumb) { 
    super(fm); 
    this.Titles = mTitles; 
    this.NumbOfTabs = mNumbOfTabsumb; 
} 

//This method return the fragment for the every position in the View Pager, This method is called only when we slide 
@Override 
public Fragment getItem(int position) { 
    if(position == 0) 
    { 
     tabOne = new DetailFragmentOne(); 
     return tabOne; 
    } 
    else if(position == 1)    
    { 
     tabTwo = new DetailFragmentTwo(); 
     return tabTwo; 
    } 
    else if(position == 2) 
    { 
     tabThree = new DetailFragmentThree(); 
     return tabThree; 
    } 
    else { 
     return null; 
    } 


} 

// This method return the titles for the Tabs in the Tab Strip 

@Override 
public CharSequence getPageTitle(int position) { 
    return Titles[position]; 
} 

// This method return the Number of tabs for the tabs Strip 

@Override 
public int getCount() { 
    return NumbOfTabs; 
} 

} 
  • 要放置那些滑動標籤創建的胡亞蓉該適配器的情況下

    viewPagerAdapter =新ViewPagerAdapter(getSupportFragmentManager(),標題,NumberOfTabs) ;

    pager = (ViewPager) findViewById(R.id.pager); 
    pager.setAdapter(viewPagerAdapter); 
    
    tabs = (SlidingTabLayout) findViewById(R.id.tabs); 
    tabs.setDistributeEvenly(true); 
    
  • 相當於要在其中把那些滑動選項卡中的活動添加SlidingTabLayout和viewPager在XML文件中。

    < path.to.package.SlidingTab.SlidingTabLayout
    機器人:layout_width = 「match_parent」
    機器人:layout_height = 「WRAP_CONTENT」
    機器人:ID = 「@ + ID /選項卡」
    機器人:背景= 「@顏色/ sliding_tab」/ >
    < path.to.package.SlidingTab.SlidingTabLayout/>

    < android.support.v4.view。ViewPager
    機器人:layout_width = 「match_parent」
    機器人:layout_height = 「match_parent」
    機器人:layout_weight = 「1」
    機器人:ID = 「@ + ID /尋呼機」/ >
    < android.support。 v4.view.ViewPager/>

  • +0

    Thaks爲您的答覆。在我的情況下,標籤也是自定義的,他們有標籤圖標,而不是標題。我將檢查是否可以自定義「SlidingTabLayout」的佈局。不過,如果你能爲我的場景建議我一些東西,那將非常有幫助。 – KavitaDev

    +0

    我嘗試過這種方式,並且只用了很少的定製工作。非常感謝。 – KavitaDev