ListView Fling is not working
In my application, I have created Bottom Tabs. Fragments are used to display contents of each Tab. Everything works fine if I clicked on Tabs. But I want to implement "Swipe Left/Right" gestrue so that user can swipe to switch from one Tab to other.
I have created custom GestureDetector for handling all MotionEvents [mainly onFling()].
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.
} }
I have ListView in a Fragment. My Coustom gesture detector is assigned to all List Items as I want to perform Click event for list items. Assigning my gesture detector to listview is not performing "onItemClick" event for list items.
I want my ListView should scroll as well as detect swipe gesture. The same we observe in WhatsApp application, where ListView is smoothly scrolling as well as smooth swipe occurs. (I know, in my case, Tabs are customized and in Bottom position.)
The problem is, even though I perform swipe left/right, listview is detecting scroll. It performs Swipe once or twice when I try this gestrue for 10 or more times. On emulator it works perfect but facing this issue on devices.
Can anyone please tell me why onFling() is not getting called everytime when I perform swipe action? I must be missing something. Any help is appreciated. Thanks in advance.
For swipe tabs you can use viewPager there is some already written code and you can get that from SlidingTabLayout and SidingTabStrip. Copy those two files in your android project and 1. create an adapter.
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;
}
}
Create instance of that adapter in the acitivity where you want to place those sliding tabs
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(),Title, NumberOfTabs);
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(viewPagerAdapter);
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true);
add SlidingTabLayout and viewPager in your xml file corresponding to the activity in which you want to put those sliding tabs.
<path.to.package.SlidingTab.SlidingTabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabs"
android:background="@color/sliding_tab"/>
<path.to.package.SlidingTab.SlidingTabLayout/>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/pager"/>
<android.support.v4.view.ViewPager/>
There is no need to use GestureDetector
. Try to use ViewPager, it has everything you wanted to achive. Have a look at complete example of ViewPager with PagerTitleStrip or you can use SlidingTabStrip.
上一篇: 滑动检测列表视图的每一行
下一篇: ListView Fling不起作用