Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.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
Android 更改列表视图项的背景色跟随我的手指_Android_Swipe_Gesture - Fatal编程技术网

Android 更改列表视图项的背景色跟随我的手指

Android 更改列表视图项的背景色跟随我的手指,android,swipe,gesture,Android,Swipe,Gesture,现在我开发了一个关于联系人的应用程序。 我想,当用户在列表视图中向左滑动一个项目时,他可以用那个号码打电话,当向右滑动时发送消息。 一切都好,但我想当向左滑动时,项目背景变为绿色,跟随用户的手指,当向右滑动时,项目背景变为黄色 或 我在OnTouchListener中的代码是: public class OnSwipeTouchListener implements OnTouchListener { private final GestureDetector gestureDetector;

现在我开发了一个关于联系人的应用程序。 我想,当用户在列表视图中向左滑动一个项目时,他可以用那个号码打电话,当向右滑动时发送消息。 一切都好,但我想当向左滑动时,项目背景变为绿色,跟随用户的手指,当向右滑动时,项目背景变为黄色

我在OnTouchListener中的代码是:

public class OnSwipeTouchListener implements OnTouchListener {

private final GestureDetector gestureDetector;

public OnSwipeTouchListener (android.content.Context ctx){
    gestureDetector = new GestureDetector(ctx, new GestureListener());

}

private final class GestureListener extends SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;
    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
            } else {
                if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeBottom();
                    } else {
                        onSwipeTop();
                    }
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public void onSwipeRight() {
}

public void onSwipeLeft() {
}

public void onSwipeTop() {
}

public void onSwipeBottom() {
}
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
    return gestureDetector.onTouchEvent(arg1);
}
}

在自定义阵列适配器中使用:

Row.setOnTouchListener(new OnSwipeTouchListener(context)
    {
        @Override
        public void onSwipeLeft() 
        {
            CallAndMsgManager call = new CallAndMsgManager();
            call.CallHandler(context, "123456789");
            super.onSwipeLeft();
        }
    });

我必须使用选择器创建选择器,并在其中添加下面的代码行

<item android:state_hovered="true" android:drawable="@drawable/vertical_line"></item>

谢谢。

我终于找到了解决这个问题的好办法:

我在那里使用布局0-1-2,布局1是默认的,当向左滑动布局2时显示,当向右滑动布局0时显示

首先,我们需要一个如下所示的viewflipper:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ViewFlipper
    android:id="@+id/vfContactInfo"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- Child 1   -->
    <LinearLayout
        android:id="@+id/llcallContact"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/msg">
            <TextView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:text="Message"
        android:textSize="20sp"
        android:textColor="#FFFFFF"/>

    </LinearLayout>

    <!-- Child 2 -->

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#44ffffff"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/imgPhoneEmailAddressType"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp" />

        <TextView
            android:id="@+id/lblPhoneEmailAddress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:layout_marginTop="12dp"
            android:layout_marginLeft="7dp"
            android:layout_marginRight="10dp"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout> 

    <!-- Child 3 -->

    <LinearLayout
    android:id="@+id/llMsgContact"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/call">
        <TextView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:text="Call"
        android:textSize="20sp"
        android:textColor="#FFFFFF"/>

    </LinearLayout>
</ViewFlipper>

</LinearLayout>
现在您必须创建OnSwipeTouchListener类来侦听touch事件,在此类中您必须重写ontouch函数:

     public boolean onTouch(View arg0, MotionEvent touchevent) {
    try {

    viewFlipper = (ViewFlipper) arg0.findViewById(R.id.vfLogRow);
    int width = viewFlipper.getWidth();
    switch (touchevent.getAction())
    {
           // when user first touches the screen to swap
            case MotionEvent.ACTION_DOWN: 
            {
                lastX = touchevent.getX();
                if (viewFlipper.getDisplayedChild() > 0)
                {
                    View leftChild = viewFlipper.getChildAt(viewFlipper.getDisplayedChild() - 1);
                    leftChild.setVisibility(View.INVISIBLE);
                    leftChild.layout(-width, 
                            leftChild.getTop(), 0, 
                            leftChild.getBottom());
                }
                if (viewFlipper.getDisplayedChild() < 2)
                {
                    View rightChild = viewFlipper.getChildAt(viewFlipper.getDisplayedChild() + 1);
                    rightChild.setVisibility(View.INVISIBLE);
                    rightChild.layout(width, 
                            rightChild.getTop(), width * 2, 
                            rightChild.getBottom());
                }
                break;
           }
            case MotionEvent.ACTION_MOVE: 
            {
                View currentView = viewFlipper.getCurrentView();
                currentView.layout((int)(touchevent.getRawX() - lastX), 
                        currentView.getTop(), (int)(touchevent.getRawX() - lastX) + width,
                        currentView.getBottom());
                if (viewFlipper.getDisplayedChild() > 0)
                {
                    View leftChild = viewFlipper.getChildAt(viewFlipper.getDisplayedChild() - 1);
                    leftChild.layout((int)(touchevent.getRawX() - lastX - width), 
                            leftChild.getTop(), (int)(touchevent.getRawX() - lastX), 
                            leftChild.getBottom());

                    //Sets the left view to visible so it shows
                    if (leftChild.getVisibility() == View.INVISIBLE)
                    {
                        leftChild.setVisibility(View.VISIBLE);
                    }
                }
                if (viewFlipper.getDisplayedChild() < 2)
                {
                    View rightChild = viewFlipper.getChildAt(viewFlipper.getDisplayedChild() + 1);
                    rightChild.layout((int)(touchevent.getRawX() - lastX + width), 
                            rightChild.getTop(), (int)(touchevent.getRawX() - lastX + (width * 2)), 
                            rightChild.getBottom());

                    if (rightChild.getVisibility() == View.INVISIBLE)
                    {
                        rightChild.setVisibility(View.VISIBLE);
                    }
                }
                break;
            }
            case MotionEvent.ACTION_UP:
            {
                float releasePoint = touchevent.getRawX();
                float mid = MainActivity.densityDpi / 2; 
                int x1  = 3, x2 = 3 ; 
                if(lastX < mid)
                    x1 = 0;
                else
                    x1 = 1;
                if(releasePoint < mid)
                    x2  = 0;
                else
                    x2 = 1;
                if(x1 == 0 && x2 == 1)
                {
                    viewFlipper.setInAnimation(MainActivity.animCallMsg);
                    viewFlipper.showPrevious();
                    onSwipeRight();
                }
                if(x1 == 1 && x2 == 0)
                {                
                     viewFlipper.setInAnimation(MainActivity.animCallMsg);
                     viewFlipper.showNext();
                     onSwipeLeft();
                }
                viewFlipper.setInAnimation(MainActivity.animCallMsg);
                viewFlipper.setDisplayedChild(1);
                break;
            }
            case MotionEvent.ACTION_CANCEL:
            {
                viewFlipper.setInAnimation(MainActivity.animCallMsg);
                viewFlipper.setDisplayedChild(1);
                break;
            }

    }
    } catch (Exception e) {
    }
    finally
    {
        return gestureDetector.onTouchEvent(touchevent);
    }
}
}

垂直线呢?工具手指如何跟随?如何从右向左指示黄色,从左向右指示绿色?非常感谢。
     public boolean onTouch(View arg0, MotionEvent touchevent) {
    try {

    viewFlipper = (ViewFlipper) arg0.findViewById(R.id.vfLogRow);
    int width = viewFlipper.getWidth();
    switch (touchevent.getAction())
    {
           // when user first touches the screen to swap
            case MotionEvent.ACTION_DOWN: 
            {
                lastX = touchevent.getX();
                if (viewFlipper.getDisplayedChild() > 0)
                {
                    View leftChild = viewFlipper.getChildAt(viewFlipper.getDisplayedChild() - 1);
                    leftChild.setVisibility(View.INVISIBLE);
                    leftChild.layout(-width, 
                            leftChild.getTop(), 0, 
                            leftChild.getBottom());
                }
                if (viewFlipper.getDisplayedChild() < 2)
                {
                    View rightChild = viewFlipper.getChildAt(viewFlipper.getDisplayedChild() + 1);
                    rightChild.setVisibility(View.INVISIBLE);
                    rightChild.layout(width, 
                            rightChild.getTop(), width * 2, 
                            rightChild.getBottom());
                }
                break;
           }
            case MotionEvent.ACTION_MOVE: 
            {
                View currentView = viewFlipper.getCurrentView();
                currentView.layout((int)(touchevent.getRawX() - lastX), 
                        currentView.getTop(), (int)(touchevent.getRawX() - lastX) + width,
                        currentView.getBottom());
                if (viewFlipper.getDisplayedChild() > 0)
                {
                    View leftChild = viewFlipper.getChildAt(viewFlipper.getDisplayedChild() - 1);
                    leftChild.layout((int)(touchevent.getRawX() - lastX - width), 
                            leftChild.getTop(), (int)(touchevent.getRawX() - lastX), 
                            leftChild.getBottom());

                    //Sets the left view to visible so it shows
                    if (leftChild.getVisibility() == View.INVISIBLE)
                    {
                        leftChild.setVisibility(View.VISIBLE);
                    }
                }
                if (viewFlipper.getDisplayedChild() < 2)
                {
                    View rightChild = viewFlipper.getChildAt(viewFlipper.getDisplayedChild() + 1);
                    rightChild.layout((int)(touchevent.getRawX() - lastX + width), 
                            rightChild.getTop(), (int)(touchevent.getRawX() - lastX + (width * 2)), 
                            rightChild.getBottom());

                    if (rightChild.getVisibility() == View.INVISIBLE)
                    {
                        rightChild.setVisibility(View.VISIBLE);
                    }
                }
                break;
            }
            case MotionEvent.ACTION_UP:
            {
                float releasePoint = touchevent.getRawX();
                float mid = MainActivity.densityDpi / 2; 
                int x1  = 3, x2 = 3 ; 
                if(lastX < mid)
                    x1 = 0;
                else
                    x1 = 1;
                if(releasePoint < mid)
                    x2  = 0;
                else
                    x2 = 1;
                if(x1 == 0 && x2 == 1)
                {
                    viewFlipper.setInAnimation(MainActivity.animCallMsg);
                    viewFlipper.showPrevious();
                    onSwipeRight();
                }
                if(x1 == 1 && x2 == 0)
                {                
                     viewFlipper.setInAnimation(MainActivity.animCallMsg);
                     viewFlipper.showNext();
                     onSwipeLeft();
                }
                viewFlipper.setInAnimation(MainActivity.animCallMsg);
                viewFlipper.setDisplayedChild(1);
                break;
            }
            case MotionEvent.ACTION_CANCEL:
            {
                viewFlipper.setInAnimation(MainActivity.animCallMsg);
                viewFlipper.setDisplayedChild(1);
                break;
            }

    }
    } catch (Exception e) {
    }
    finally
    {
        return gestureDetector.onTouchEvent(touchevent);
    }
}
}