水平滚动视图一次滚动一个项目

时间:2013-09-03 16:53:15

标签: java android

我目前正在使用水平滚动视图实现包含水平列表视图的面板。因为我需要控制水平滚动视图的滚动并在时间滚动一个项目。

任何人都可以让我知道做这种事情。

1 个答案:

答案 0 :(得分:0)

我使用覆盖Horizo​​ntalScrollView并合并我从各种互联网示例中获得的代码来做到这一点。它工作正常。

我使用了gestureDetector和onFling方法来完成这项任务。

public void setCenter(int index) {

    ViewGroup parent = (ViewGroup) getChildAt(0);

    View preView = parent.getChildAt(prevIndex);
    //preView.setBackgroundColor(Color.parseColor("#64CBD8"));
    android.widget.LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    lp.setMargins(5, 0, 5, 0);
    preView.setLayoutParams(lp);

    View view = parent.getChildAt(index);
    //view.setBackgroundColor(Color.RED);

    int screenWidth = ((Activity) context).getWindowManager()
            .getDefaultDisplay().getWidth();

    int scrollX = (view.getLeft() - (screenWidth / 2))
            + (view.getWidth() / 2);
    this.smoothScrollTo(scrollX, 0);
    prevIndex = index;
    Log.d("ITEM", String.valueOf(prevIndex));
}

@Override
public boolean onDown(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    if (flingDisable)
        return false;
    boolean returnValue = false;
    float ptx1 = 0, ptx2 = 0;
    if (e1 == null || e2 == null)
        return false;
    ptx1 = e1.getX();
    ptx2 = e2.getX();
    // right to left

    if (ptx1 - ptx2 > SWIPE_MIN_DISTANCE
            && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
        if (activeItem < maxItem - 1)
            activeItem = activeItem + 1;

        returnValue = true;

    } else if (ptx2 - ptx1 > SWIPE_MIN_DISTANCE
            && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
        if (activeItem > 0)
            activeItem = activeItem - 1;

        returnValue = true;
    }
    //scrollTo = activeItem * itemWidth;
    //this.smoothScrollTo(0, scrollTo);
    setCenter(activeItem);
    return returnValue;
}



@Override
public boolean onTouch(View v, MotionEvent event) {
    if (gestureDetector.onTouchEvent(event)) {
        return true;
    }
    Boolean returnValue = gestureDetector.onTouchEvent(event);

    int x = (int) event.getRawX();

    switch (event.getAction()) {
    case MotionEvent.ACTION_MOVE:
        if (start) {
            this.prevScrollX = x;
            start = false;
        }
        break;
    case MotionEvent.ACTION_UP:
        start = true;
        this.currentScrollX = x;
        int minFactor = itemWidth / SWIPE_PAGE_ON_FACTOR;

        if ((this.prevScrollX - this.currentScrollX) > minFactor) {
            if (activeItem < maxItem - 1)
                activeItem = activeItem + 1;

        } else if ((this.currentScrollX - this.prevScrollX) > minFactor) {
            if (activeItem > 0)
                activeItem = activeItem - 1;
        }
        Log.d("ITEM", String.valueOf(activeItem));
        setCenter(activeItem);
        returnValue = true;
        break;
    }
    return returnValue;
}