0

我正在使用onFling功能来刷卡ScrollView。我面临的问题是,当我从上到下滑动/滚动时,它会将我的标签从一个向右/向左滑动到另一个。所以我需要当我滚动时我的向左/向右滑动功能不起作用。

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        final RelativeLayout detail = (RelativeLayout) findViewById(R.id.layout_detail);
        final RelativeLayout photo = (RelativeLayout) findViewById(R.id.layout_detail_photo);
        final RelativeLayout pod = (RelativeLayout) findViewById(R.id.layout_detail_pod);
        final ToggleButton btn_detail = (ToggleButton) findViewById(R.id.btn_detail);
        final ToggleButton btn_pod = (ToggleButton) findViewById(R.id.btn_pod);
        final ToggleButton btn_photo = (ToggleButton) findViewById(R.id.btn_photo);
        // right to left swipe

        if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

            if ((detail.getVisibility() == View.VISIBLE)
                    && (pod.getVisibility() == View.GONE)
                    && (photo.getVisibility() == View.GONE)) {
                detail.setVisibility(View.GONE);
                photo.setVisibility(View.GONE);
                pod.setVisibility(View.VISIBLE);
                btn_photo.setSelected(false);
                btn_pod.setSelected(true);
                btn_detail.setSelected(false);
            } else if ((detail.getVisibility() == View.GONE)
                    && (pod.getVisibility() == View.VISIBLE)
                    && (photo.getVisibility() == View.GONE)) {
                detail.setVisibility(View.GONE);
                photo.setVisibility(View.VISIBLE);
                pod.setVisibility(View.GONE);
                btn_photo.setSelected(true);
                btn_pod.setSelected(false);
                btn_detail.setSelected(false);
            } else if ((detail.getVisibility() == View.GONE)
                    && (pod.getVisibility() == View.GONE)
                    && (photo.getVisibility() == View.VISIBLE)) {
                detail.setVisibility(View.VISIBLE);
                photo.setVisibility(View.GONE);
                pod.setVisibility(View.GONE);
                btn_photo.setSelected(false);
                btn_pod.setSelected(false);
                btn_detail.setSelected(true);
            }
        }
        // left to right swipe
        else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

            if ((detail.getVisibility() == View.VISIBLE)
                    && (pod.getVisibility() == View.GONE)
                    && (photo.getVisibility() == View.GONE)) {
                detail.setVisibility(View.GONE);
                photo.setVisibility(View.VISIBLE);
                pod.setVisibility(View.GONE);
                btn_photo.setSelected(true);
                btn_pod.setSelected(false);
                btn_detail.setSelected(false);
            } else if ((detail.getVisibility() == View.GONE)
                    && (pod.getVisibility() == View.GONE)
                    && (photo.getVisibility() == View.VISIBLE)) {
                detail.setVisibility(View.GONE);
                photo.setVisibility(View.GONE);
                pod.setVisibility(View.VISIBLE);
                btn_photo.setSelected(false);
                btn_pod.setSelected(true);
                btn_detail.setSelected(false);
            } else if ((detail.getVisibility() == View.GONE)
                    && (pod.getVisibility() == View.VISIBLE)
                    && (photo.getVisibility() == View.GONE)) {
                detail.setVisibility(View.VISIBLE);
                photo.setVisibility(View.GONE);
                pod.setVisibility(View.GONE);
                btn_photo.setSelected(false);
                btn_pod.setSelected(false);
                btn_detail.setSelected(true);
            }
        }
        return true;
    }
4

2 回答 2

0

你的问题不是很清楚,但如果我理解正确,你会问为什么你的代码只响应右/左甩而不响应上/下甩。您只有处理左/右甩动的代码。它怎么可能做任何事情来响应向上/向下一掷?代码如下...

if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && 
    Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY)

... 应该响应向上/向下甩动。

于 2014-03-02T05:33:56.103 回答
0

我的建议是使用ViewPager,它允许您将滚动视图作为页面插入,您可以通过左右滑动来滑动浏览。这实际上是我所做的,使用视图寻呼机,我创建了一个返回列表视图的PagerAdapter。在您的情况下,您可以返回滚动视图。我附加了一些代码作为您需要插入到视图寻呼机的寻呼机的基础。

public class ViewpagerAdapter extends PagerAdapter {


private Context mContext;

public ViewpagerListviewsAdapter(Context mContext) {

    this.mContext = mContext;
}
    @Override
    public Object instantiateItem(ViewGroup group, int position) {


            ListView lView = new ListView(mContext);
                            group.addView(lView);
            return lView;



    }

    @Override
    public void destroyItem(ViewGroup group, int position, Object object) {

        View view = (View) object;
        group.removeView(view);
        view = null;

    }

    @Override
    public int getCount() {
        return (number of views in pager);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {

        return view==object;

    }


}
于 2013-07-18T01:14:46.867 回答