java - 根据滑动方向更改 View 颜色

标签 java android itemtouchhelper

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

@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);
    }

问题是,虽然某些 View 具有我正在尝试设计的属性,但其他 View 则没有,它们保持相同的颜色并且 TextView 保持可见。

我没有在 onChildDrawOver 中实现代码,因为我没有理由这样做

最佳答案

下面的代码会在向左或向右滑动时更改 View 的颜色。它使用 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;
}}

下面是布局 xml 代码:

<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>

关于java - 根据滑动方向更改 View 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49136010/

相关文章:

javascript - 在钛javascript中将屏幕移动到图像上

java - 仅知道目录的相对路径时如何读取目录内的文件

java - Java 记录是否支持 "with"语法?

javascript - 有人知道如何检测 Nativescript 的方向变化吗?

android - 如何使用android Canvas 绘制一个只有左上角和右上角的矩形?

android - 向左/向右滑动 RecyclerView 的部分行

具有多种 View 类型和 ItemTouchHelper 的 Android RecyclerView

android - 如何在 Recyclerview Android 中以两种颜色滑动删除和滑动存档

java - JVM 堆 - 为什么 S0/S1 的大小会随着时间的推移而减小?

java - 如何在 TableLayout 中将按钮相互粘贴?