Включение не отправка для TextView внутри LinearLayout внутри представления переработчика, - PullRequest
1 голос
/ 15 января 2020

Итак, я делаю простое приложение для создания списка на 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>

1 Ответ

0 голосов
/ 18 января 2020

Оказывается, вам нужно переместить инициализацию 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());
        }
    });
}
...