因此,我正在制作一个简单的Android上的待办事项列表应用程序,并尝试使事情变得更棘手。
我要开始工作的一件事是detecting swipes。现在,我希望能够检测到放置在swipe on a TextView内的linear layout。这种线性布局是一个RecyclingView item的视图持有者。

令我困惑的是,检测到onDown和onScroll但未检测到onFling。

这是我的代码

class ViewHolder extends RecyclerView.ViewHolder {
    public TextView toDoText;
    public Button checkButton;

    public ViewHolder(View itemView){
        super(itemView);
        toDoText = itemView.findViewById(R.id.toDoText);
        checkButton = itemView.findViewById(R.id.checkButton);
    }
}


class ToDoAdapter extends RecyclerView.Adapter<ViewHolder> {


    List<String> toDos;
    public ToDoAdapter(List<String> toDos){
        this.toDos = toDos;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int id){
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        View toDoView = inflater.inflate(R.layout.to_do_row, parent, false);
        ViewHolder viewHolder = new ViewHolder(toDoView);
        return viewHolder;
    }


    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position){
        String toDo = toDos.get(position);
        final TextView textView = viewHolder.toDoText;
        textView.setText(toDo);

        textView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                GestureDetector detector;
                GestureDetector.OnGestureListener listener = new GestureDetector.OnGestureListener() {
                    @Override
                    public boolean onDown(MotionEvent e) {
                        System.out.println("DOWN");
                        return true;
                    }

                    @Override
                    public void onShowPress(MotionEvent e) {
                        System.out.println("SHOW PRESS");

                    }

                    @Override
                    public boolean onSingleTapUp(MotionEvent e) {
                        System.out.println("SINGLE TAP UP");
                        return true;

                    }

                    @Override
                    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                        System.out.println("SCROLL");
                        return true;
                    }

                    @Override
                    public void onLongPress(MotionEvent e) {
                        System.out.println("LONG PRESS");

                    }

                    @Override
                    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                        System.out.println("FLING");
                        return true;
                    }
                };
                detector = new GestureDetector(listener);
                detector.setIsLongpressEnabled(false);
                return detector.onTouchEvent(event);
            }
        });
        Button button = viewHolder.checkButton;
        button.setEnabled(true);
        button.setText("Check");
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                toDos.remove(position);
                ToDoAdapter.this.notifyItemRemoved(position);
                notifyItemRangeChanged(position, toDos.size());
            }
        });
    }

    @Override
    public int getItemCount(){
        return toDos.size();
    }
}


public class MainActivity extends AppCompatActivity {
    ArrayList<String> items;
    RecyclerView rvItems;
    EditText editText;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rvItems = findViewById(R.id.itemListRecycler);
        items = new ArrayList<>();
        items.add("Test this todo out");
        final ToDoAdapter adapter = new ToDoAdapter(items);
        rvItems.setAdapter(adapter);
        final RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        rvItems.setLayoutManager(layoutManager);
        Button addItem = findViewById(R.id.addNewItemButton);
        editText = findViewById(R.id.newItemText);
        addItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String input = editText.getText().toString();
                if(input == null) {
                    input = "";
                }
                items.add(input);
                adapter.notifyItemInserted(items.size()-1);
                layoutManager.scrollToPosition(items.size()-1);
            }
        });
    }
}


这是我主要活动的XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/addNewItemButton"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="16dp"
        android:text="Add Item"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/newItemText"
        app:layout_constraintTop_toBottomOf="@+id/itemListRecycler"
        app:layout_constraintVertical_bias="1.0" />

    <EditText
        android:id="@+id/newItemText"
        android:layout_width="0dp"
        android:layout_height="44dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="16dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/addNewItemButton"
        app:layout_constraintStart_toStartOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/itemListRecycler"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="16dp"
        android:scrollbars="vertical"
        app:layout_constraintBottom_toTopOf="@+id/newItemText"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

</androidx.constraintlayout.widget.ConstraintLayout>


这是线性布局的XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:id="@+id/toDoItemLayout"
    android:layout_height="wrap_content"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    >

    <TextView
        android:id="@+id/toDoText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="20dp"/>

    <Button
        android:id="@+id/checkButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:textSize="10sp"
        />
</LinearLayout>

最佳答案

原来,您必须像这样在onTouchListener的匿名子类外移动GestureListener和detector的初始化

 public void onBindViewHolder(ViewHolder viewHolder, final int position){
    String toDo = toDos.get(position);
    final TextView textView = viewHolder.toDoText;
    textView.setText(toDo);
    final GestureDetector detector;
    final GestureDetector.OnGestureListener listener = new GestureDetector.OnGestureListener() {
        @Override
        public boolean onDown(MotionEvent e) {
            System.out.println("DOWN");
            return true;
        }

        @Override
        public void onShowPress(MotionEvent e) {
            System.out.println("SHOW PRESS");

        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            System.out.println("SINGLE TAP UP");
            return true;

        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            System.out.println("SCROLL");
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            System.out.println("LONG PRESS");

        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            System.out.println("FLING");
            return true;
        }
    };
    detector = new GestureDetector(listener);
    detector.setIsLongpressEnabled(false);

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

            return detector.onTouchEvent(event);
        }
    });
    Button button = viewHolder.checkButton;
    button.setEnabled(true);
    button.setText("Check");
    button.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            toDos.remove(position);
            ToDoAdapter.this.notifyItemRemoved(position);
            notifyItemRangeChanged(position, toDos.size());
        }
    });
}

10-04 20:04