Java Onfling未调度TextView内部LinearLayout内部回收器视图,
所以我在android上制作了一个简单的待办事项列表应用程序,并试图掌握其中的诀窍。 我正在尝试的一件事是Java Onfling未调度TextView内部LinearLayout内部回收器视图,,java,android,swipe,gesture,onfling,Java,Android,Swipe,Gesture,Onfling,所以我在android上制作了一个简单的待办事项列表应用程序,并试图掌握其中的诀窍。 我正在尝试的一件事是检测刷卡。现在,我希望能够检测到线性布局中放置的TextView上的滑动。此线性布局是一个回收视图项目的视图支架 让我困惑的是onDown和onScroll被检测到了,但是onFling没有 这是我的密码 class ViewHolder extends RecyclerView.ViewHolder { public TextView toDoText; public Bu
检测刷卡
。现在,我希望能够检测到线性布局中放置的TextView
上的滑动。此线性布局是一个回收视图项目的视图支架
让我困惑的是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);
}
});
}
}
class ViewHolder扩展了RecyclerView.ViewHolder{
公共文本视图到文本;
公共按钮检查按钮;
公共视图持有者(视图项视图){
超级(项目视图);
toDoText=itemView.findviewbyd(R.id.toDoText);
checkButton=itemView.findviewbyd(R.id.checkButton);
}
}
类ToLoadapter扩展了RecyclerView.Adapter{
列出待办事项;
公共待办事项(列出待办事项){
this.toDos=toDos;
}
@凌驾
public ViewHolder onCreateViewHolder(视图组父级,int-id){
Context=parent.getContext();
LayoutFlater充气机=LayoutFlater.from(上下文);
视图toDoView=充气机。充气(R.layout.to_do_行,父级,false);
ViewHolder ViewHolder=新的ViewHolder(toDoView);
返回视图持有者;
}
@SuppressLint(“ClickableViewAccessibility”)
@凌驾
公共无效onBindViewHolder(ViewHolder ViewHolder,最终int位置){
字符串toDo=toDos.get(位置);
final TextView TextView=viewHolder.toDoText;
设置文本(toDo);
textView.setOnTouchListener(新视图.OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
手势检测器;
GestureDetector.OnGestureListener侦听器=新建GestureDetector.OnGestureListener(){
@凌驾
公共布尔onDown(运动事件e){
System.out.println(“向下”);
返回true;
}
@凌驾
在ShowPress上公开作废(运动事件e){
System.out.println(“显示印刷机”);
}
@凌驾
公共布尔onSingleTapUp(运动事件e){
System.out.println(“单点启动”);
返回true;
}
@凌驾
公共布尔onScroll(MotionEvent e1、MotionEvent e2、浮点距离X、浮点距离Y){
System.out.println(“滚动”);
返回true;
}
@凌驾
公开无效在线新闻(运动事件e){
系统输出打印(“长按”);
}
@凌驾
公共布尔onFling(MotionEvent e1、MotionEvent e2、float-velocityX、float-velocityY){
System.out.println(“FLING”);
返回true;
}
};
检测器=新的手势检测器(侦听器);
检测器。设置为长按启用(假);
返回检测器。开孔(事件);
}
});
按钮按钮=viewHolder.checkButton;
按钮。setEnabled(真);
按钮。设置文本(“检查”);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
toDos.移除(位置);
ToDoAdapter.this.notifyItemRemoved(位置);
notifyItemRangeChanged(位置,toDos.size());
}
});
}
@凌驾
public int getItemCount(){
返回toDos.size();
}
}
公共类MainActivity扩展了AppCompatActivity{
数组列表项;
回收物品;
编辑文本编辑文本;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rvItems=findviewbyd(R.id.itemListRecycler);
items=newarraylist();
添加(“测试此待办事项”);
最终ToDoAdapter适配器=新ToDoAdapter(项目);
rvItems.setAdapter(适配器);
final RecyclerView.LayoutManager LayoutManager=新的LinearLayoutManager(此);
rvItems.setLayoutManager(layoutManager);
Button addItem=findViewById(R.id.addNewItemButton);
editText=findViewById(R.id.newItemText);
addItem.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
字符串输入=editText.getText().toString();
如果(输入==null){
输入=”;
}
项目。添加(输入);
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>
事实证明,您必须像这样将GestureListener和detector的初始化移到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());
}
});
}
事实证明,您必须像这样将GestureListener和detector的初始化移到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());
}
});
}