Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 水平滚动视图一次滚动一个项目_Java_Android - Fatal编程技术网

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

Java 水平滚动视图一次滚动一个项目,java,android,Java,Android,我目前正在使用水平滚动视图实现一个包含水平列表视图的面板。因为我需要控制水平滚动视图的滚动,并且一次滚动一个项目 谁能告诉我做这种事情的想法。我通过重写HorizontalScrollView并合并从各种internet示例中获得的代码,成功地做到了这一点。它工作得很好 我使用了手势检测器和onFling方法来完成这项任务 public void setCenter(int index) { ViewGroup parent = (ViewGroup) getChildAt(0);

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


谁能告诉我做这种事情的想法。

我通过重写HorizontalScrollView并合并从各种internet示例中获得的代码,成功地做到了这一点。它工作得很好

我使用了手势检测器和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;
}
publicsvoidsetcenter(int索引){
ViewGroup parent=(ViewGroup)getChildAt(0);
视图预览=parent.getChildAt(prevIndex);
//preView.setBackgroundColor(Color.parseColor(#64CBD8”);
android.widget.LinearLayout.LayoutParams lp=新的LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_内容,
LinearLayout.LayoutParams.WRAP_内容);
lp.设定利润率(5,0,5,0);
preView.setLayoutParams(lp);
View=parent.getChildAt(索引);
//视图.setBackgroundColor(颜色.红色);
int screenWidth=((活动)上下文).getWindowManager()
.getDefaultDisplay().getWidth();
int scrollX=(view.getLeft()-(屏幕宽度/2))
+(view.getWidth()/2);
this.smoothScrollTo(scrollX,0);
指数=指数;
Log.d(“ITEM”,String.valueOf(prevIndex));
}
@凌驾
公共布尔onDown(运动事件e){
//TODO自动生成的方法存根
返回false;
}
@凌驾
公共布尔onFling(运动事件e1、运动事件e2、浮点速度X、,
浮动速度y){
如果(flingDisable)
返回false;
布尔返回值=false;
浮点数ptx1=0,ptx2=0;
如果(e1==null | | e2==null)
返回false;
ptx1=e1.getX();
ptx2=e2.getX();
//从右向左
如果(ptx1-ptx2>滑动最小距离
&&Math.abs(velocityX)>滑动阈值(速度){
如果(activeItem滑动最小距离
&&Math.abs(velocityX)>滑动阈值(速度){
如果(activeItem>0)
activeItem=activeItem-1;
returnValue=true;
}
//scrollTo=activeItem*itemWidth;
//this.smoothScrollTo(0,scrollTo);
设置中心(活动项);
返回值;
}
@凌驾
公共布尔onTouch(视图v,运动事件){
if(手势检测器onTouchEvent(事件)){
返回true;
}
布尔返回值=gestureDetector.onTouchEvent(事件);
int x=(int)event.getRawX();
开关(event.getAction()){
case MotionEvent.ACTION\u移动:
如果(启动){
这个.x=x;
开始=错误;
}
打破
case MotionEvent.ACTION\u UP:
开始=真;
这个.currentScrollX=x;
int minFactor=itemWidth/swip\u PAGE\u ON\u FACTOR;
if((this.prevScrollX-this.currentScrollX)>minFactor){
如果(activeItemminFactor){
如果(activeItem>0)
activeItem=activeItem-1;
}
Log.d(“ITEM”,String.valueOf(activeItem));
设置中心(活动项);
returnValue=true;
打破
}
返回值;
}

也许我误解了。听起来您正试图使HorizontalScrollView的行为方式与它的行为方式相同。如果是这种情况,您是否有不使用ViewPager的特定原因?谢谢。我已检查了ViewPager,但似乎无法将其连接到适配器。我有动态记录,因此我的解决方案必须有适配器。那么,您是否希望以编程方式制作滚动?如果是,你想什么时候做?或者当用户滚动视图时,您只想一次滚动一个项目?在这种情况下,这不是一种推荐的方式,用户可以随意滚动。但这是我得到的要求,所以我必须实现一些方法:)。我想控制滚动到一个项目时,现在滚动到两个项目。ViewPager确实使用了适配器。看这里:你好,兄弟!我也有这个问题,你能给我提供完整的代码示例吗????是的,我可以给你发主课,让我知道我怎么能发。