简体   繁体   English

Onfling 不为回收器视图内的 LinearLayout 内的 TextView 调度,

[英]Onfling not dispatching for TextView inside LinearLayout inside recycler view,

So I'm making a simple to do list app on android and trying to get the hang of things.因此,我正在 Android 上制作一个简单的待办事项列表应用程序,并尝试掌握窍门。 One thing I'm trying to get working is detecting swipes .我试图开始工作的一件事是detecting swipes Right now, I want to be able to detect a swipe on a TextView that is placed inside a linear layout .现在,我希望能够检测到放置linear layout内的swipe on a TextView This linear layout is the view holder for one RecyclingView item .这种线性布局是一个RecyclingView item的视图持有者。

Whats confusing me is that onDown and onScroll are detected but not onFling.让我感到困惑的是检测到 onDown 和 onScroll 但没有检测到 onFling。

Here's my code这是我的代码

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

Heres my xml for the main 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>

Here's my xml for the linear layout这是我的线性布局的 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>

Turns out you gotta move the initialization of GestureListener and detector outside the anonymous subclassing of onTouchListener like this原来你必须像这样将 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());
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM