2016-12-07 117 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