Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.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_Itemtouchhelper - Fatal编程技术网

Java 根据滑动方向更改视图颜色

Java 根据滑动方向更改视图颜色,java,android,itemtouchhelper,Java,Android,Itemtouchhelper,我正在尝试使用ItemTouchHelper的onChildDraw()方法根据滑动getsure的方向更改视图的颜色,但它不起作用: @Override public void onChildDraw(Canvas c, RecyclerView recyclerView, ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { final View

我正在尝试使用ItemTouchHelper的onChildDraw()方法根据滑动getsure的方向更改视图的颜色,但它不起作用:

@Override
    public void onChildDraw(Canvas c, RecyclerView recyclerView, ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        final View foregroundView = ((com.lab1.ac01220.bloomv2.ViewHolder) viewHolder).getForeground();

            if(dX < 0){
                this.mData.getViewHolder().getDeleteText().setVisibility(View.VISIBLE);
                this.mData.getViewHolder().getCompleteText().setVisibility(View.INVISIBLE);
                this.mData.getViewHolder().getBackground().setBackgroundColor(getResources().getColor(R.color.colorAccent));
            } else if(dX >0){
                this.mData.getViewHolder().getCompleteText().setVisibility(View.VISIBLE);
                this.mData.getViewHolder().getDeleteText().setVisibility(View.INVISIBLE);
                this.mData.getViewHolder().getBackground().setBackgroundColor(getResources().getColor(R.color.colorComplete));
            }


        ItemTouchHelper.SimpleCallback.getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX, dY,
                actionState, isCurrentlyActive);
    }
@覆盖
public void onchildraw(Canvas c、RecyclerView、RecyclerView、ViewHolder、float dX、float dY、int actionState、boolean isCurrentlyActive){
最终视图前景视图=((com.lab1.ac01220.bloomv2.ViewHolder)ViewHolder.get前台();
if(dX<0){
this.mData.getViewHolder().getDeleteText().setVisibility(View.VISIBLE);
this.mData.getViewHolder().getCompleteText().setVisibility(View.INVISIBLE);
this.mData.getViewHolder().getBackgroundColor().setBackgroundColor(getResources().getColor(R.color.colorAccent));
}如果(dX>0),则为else{
this.mData.getViewHolder().getCompleteText().setVisibility(View.VISIBLE);
this.mData.getViewHolder().getDeleteText().setVisibility(View.INVISIBLE);
this.mData.getViewHolder().getBackgroundColor().setBackgroundColor(getResources().getColor(R.color.colorComplete));
}
ItemTouchHelper.SimpleCallback.getDefaultUIUtil().onDraw(c、recyclerView、foregroundView、dX、dY、,
actionState,当前处于活动状态);
}
问题是,虽然有些视图具有我试图设计的属性,但其他视图没有,它们保持相同的颜色,文本视图保持可见


我没有在OnChildRawOver中实现该代码,因为我看不出有什么理由

当向左或向右滑动时,下面的代码会更改视图的颜色。它使用OnTouchListener和一些逻辑来区分左右滑动。我知道这与您的方法不同,但它可能会帮助您

public class demo4 extends AppCompatActivity{

private static final String TAG = "demo4";
private TextView tv;
private View demo4;

private float mDownMotionX = 0;
private float mDownMotionY = 0;

private final int SWIPE_SENSITIVITY = 10;

private final int SWIPE_Y_SENSITIVITY = 20;

private final int SWIPE_X_SENSITIVITY_MIN = 0;
private int SWIPE_X_SENSITIVITY_MAX = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.demo4);
    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    tv = (TextView) findViewById(R.id.tv);

    SWIPE_X_SENSITIVITY_MAX = getScreenWidth();
    Log.e("Swipe", ""+SWIPE_X_SENSITIVITY_MAX);

    tv.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent ev) {

            Log.e(TAG, "Touch View");
                final int action = ev.getAction();

                switch (action & MotionEvent.ACTION_MASK) {

                    case MotionEvent.ACTION_DOWN:
                        // Remember where the motion event started
                        mDownMotionX = ev.getX();
                        mDownMotionY = ev.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        // Scroll to follow the motion event
                        final float x = ev.getX();
                        final float y = ev.getY();
                        if (Math.abs(x - mDownMotionX) >= SWIPE_SENSITIVITY &&
                                (mDownMotionX > SWIPE_X_SENSITIVITY_MIN &&
                                        mDownMotionX <= SWIPE_X_SENSITIVITY_MAX) &&
                                Math.abs(y - mDownMotionY) <= SWIPE_Y_SENSITIVITY) {

                            if ((x - mDownMotionX) > 0) {
                                tv.setBackgroundColor(Color.RED);
                                Log.d(TAG, "Dragging right");
                            }
                        } else if ((x - mDownMotionX) < 0) {
                            tv.setBackgroundColor(Color.GRAY);
                            Log.d(TAG, "Dragging left");
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                    case MotionEvent.ACTION_CANCEL:
                        break;
                }
                return true;
            }
    });
}

public int getScreenWidth(){

    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    return displayMetrics.widthPixels;
}}
public类demo4扩展了AppCompative活动{
私有静态最终字符串TAG=“demo4”;
私家图文电视;
私有视图演示4;
私有float mDownMotionX=0;
私有float mDownMotionY=0;
私人最终整数滑动灵敏度=10;
私人最终整数滑动灵敏度=20;
私人最终整数滑动\u X\u灵敏度\u最小值=0;
私用整数刷卡\u X\u灵敏度\u MAX=0;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.demo4);
LayoutFlater充气器=(LayoutFlater)getApplicationContext().getSystemService(Context.LAYOUT\u充气器\u服务);
tv=(TextView)findviewbyd(R.id.tv);
滑动\u X\u灵敏度\u MAX=getScreenWidth();
Log.e(“滑动”,“滑动+最大灵敏度”);
tv.setOnTouchListener(新视图.OnTouchListener(){
@凌驾
公共布尔onTouch(视图v、运动事件ev){
Log.e(标签“触摸视图”);
final int action=ev.getAction();
开关(动作和动作事件.动作屏蔽){
case MotionEvent.ACTION\u DOWN:
//还记得运动事件从哪里开始吗
mDownMotionX=ev.getX();
mDownMotionY=ev.getY();
打破
case MotionEvent.ACTION\u移动:
//滚动以跟踪运动事件
最终浮点数x=ev.getX();
最终浮动y=ev.getY();
if(Math.abs(x-mDownMotionX)>=滑动灵敏度&&
(mDownMotionX>SWIPE\u X\u灵敏度\u MIN&&

mDownMotionX不幸的是,我认为实现这一点会浪费我已经准备好的资源,但我感谢你的回答
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<TextView
    android:id="@+id/tv"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@android:color/darker_gray"
    android:gravity="center"
    android:textSize="30sp"
    android:text="x"/>

</LinearLayout>