java - Onfling 未在回收器 View 内的 LinearLayout 内调度 TextView,

标签 java android swipe gesture onfling

因此,我正在 Android 上制作一个简单的待办事项列表应用程序,并尝试掌握其中的窍门。 我正在尝试做的一件事是检测滑动。现在,我希望能够检测到放置在线性布局内的 TextView 上的滑动。此线性布局是一个 RecyclingView 项 的 View 持有者。

令我困惑的是,检测到了 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);
            }
        });
    }
}

这是我的主要 Activity 的 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>

最佳答案

事实证明,您必须像这样将 GestureListener 和检测器的初始化移到 onTouchListener 的匿名子类之外

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

关于java - Onfling 未在回收器 View 内的 LinearLayout 内调度 TextView,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59745919/

相关文章:

android - 在 Android 中使用按钮滑动

android - android appium 1.3.4 版本是否支持 Swipe 方法?

ios - 如何从 UITableView 滑动触发 segue 以快速编辑

html - Galaxy S5 上的 Cordova CSS 问题

java - Jackson 序列化忽略使用非 jackson 注释注释的字段

java - 找不到 websphere deployment.xml 组成单元

java - java JRE tarball 和 EXE 有什么区别

java - 如何使用spring的DefaultMessageListenerContainer进行手动提交?

android - 线程 "main"java.lang.UnsatisfiedLinkError : no swt-win32-3550 中的异常

android - 如何动态更改适配器布局?