首页 文章

ListView Fling无效

提问于
浏览
0

在我的应用程序中,我创建了Bottom选项卡 . 片段用于显示每个选项卡的内容 . 如果我点击Tabs,一切正常 . 但我想实现"Swipe Left/Right" gestrue,以便用户可以滑动以从一个Tab切换到另一个 .
我创建了自定义 GestureDetector 来处理所有MotionEvents [主要是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.
} }

我在片段中有ListView . 我的Coustom手势检测器被分配给所有列表项,因为我想对列表项执行Click事件 . 将我的手势检测器分配给listview不会对列表项执行“onItemClick”事件 .

我希望我的ListView应该滚动以及检测滑动手势 . 我们在WhatsApp应用程序中观察到同样的情况,其中ListView平滑滚动以及平滑滑动发生 . (我知道,在我的情况下,Tabs是定制的并且处于底部位置 . )

问题是,即使我执行向左/向右滑动,listview也会检测滚动 . 当我尝试使用此gestrue 10次或更多次时,它会执行一次或两次轻扫 . 在模拟器上它工作得很完美,但在设备上面临这个问题 .

任何人都可以告诉我为什么每次执行滑动操作时都没有调用onFling()?我肯定错过了什么 . 任何帮助表示赞赏 . 提前致谢 .

2 回答

  • 0

    对于滑动选项卡,您可以使用viewPager,有一些已编写的代码,您可以从SlidingTabLayoutSidingTabStrip获取 . 复制android项目中的这两个文件,然后创建一个适配器 .

    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 = new ViewPagerAdapter(getSupportFragmentManager(),Title,NumberOfTabs);

    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(viewPagerAdapter);
    
    tabs = (SlidingTabLayout) findViewById(R.id.tabs);
    tabs.setDistributeEvenly(true);
    
    • 在xml文件中添加SlidingTabLayout和viewPager,对应于要放置这些滑动选项卡的活动 .

    <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 />

  • 0

    没有必要使用 GestureDetector . 尝试使用ViewPager,它拥有你想要的一切 . 使用PagerTitleStrip查看ViewPager的完整示例,或者您可以使用SlidingTabStrip .

相关问题