java - 如何从另一个类的活动/类中获取视图?
问题描述
所以我有一个表格布局,在每一行中我都有一个 setOnTouchListener 类,它允许我检测行中的滑动,它已经检测到了,但我想要做的是当人交换行时,行已删除,我尝试了几件事,但我认为我需要获取其他类中的视图才能获取行索引。我的 Swipe Class 全局实例为:
ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(this);
每当我创建一行时,我只需添加: tr.setOnTouchListener(activitySwipeDetector);
tr 是该行这就是 ActivitySwipeDetector,注释部分是我想要做的:
public class ActivitySwipeDetector implements View.OnTouchListener {
static final String logTag = "ActivitySwipeDetector";
private Activity activity;
static final int MIN_DISTANCE = 100;
private float downX, downY, upX, upY;
Invoices invoices = new Invoices();
NavManager nav = new NavManager();
public ActivitySwipeDetector(Activity activity){
this.activity = activity;
}
public void onRightSwipe(){
System.out.println("Swiped Correctly from Right to Left");
int index = (int)nav.getCurrentFocus().getTag();
invoices.data.remove(index);
System.out.println("Eliminado Correctamente");
}
public void onLeftSwipe(){
}
public void onDownSwipe(){
}
public void onUpSwipe(){
}
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN: {
downX = event.getX();
downY = event.getY();
return true;
}
case MotionEvent.ACTION_UP: {
upX = event.getX();
upY = event.getY();
float deltaX = downX - upX;
float deltaY = downY - upY;
// swipe horizontal?
if(Math.abs(deltaX) > Math.abs(deltaY))
{
if(Math.abs(deltaX) > MIN_DISTANCE){
// left or right
if(deltaX > 0) { this.onRightSwipe(); return true; }
if(deltaX < 0) { this.onLeftSwipe(); return true; }
}
else {
Log.i(logTag, "Horizontal Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
return false; // We don't consume the event
}
}
// swipe vertical?
else
{
if(Math.abs(deltaY) > MIN_DISTANCE){
// top or down
if(deltaY < 0) { this.onDownSwipe(); return true; }
if(deltaY > 0) { this.onUpSwipe(); return true; }
}
else {
Log.i(logTag, "Vertical Swipe was only " + Math.abs(deltaX) + " long, need at least " + MIN_DISTANCE);
return false; // We don't consume the event
}
}
return true;
}
}
return false;
}
在此先感谢,我真的很感激至少有一些关于该做什么的想法!*编辑:表格由数据填充:一个 ArrayList
解决方案
创建一个监听器:
public interface SwipeCallback {
void onRightSwipe();
void onLeftSwipe();
void onDownSwipe();
void onUpSwipe();
}
将该侦听器添加到您的构造函数并将其设置为全局变量:
private SwipeCallback swipeCallback;
public ActivitySwipeDetector(Activity activity, SwipeCallback callback) {
this.activity = activity;
swipeCallback = callback;
}
然后在对应的方法中调用监听器的方法:
public void onRightSwipe() {
listener.onRightSwipe();
//...
}
//etc
构建检测器时:
ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(
this,
new SwipeCallback() {
//handle the swipes
@Override
public void onRightSwipe() {}
@Override
public void onLeftSwipe() {}
@Override
public void onUpSwipe() {}
@Override
public void onDownSwipe() {}
}
);
推荐阅读
- regex - 用于替换除空白和指定目标之外的所有内容的正则表达式 Scala
- c# - 转换日期时间时,字符串未被识别为有效的日期时间
- python - 打开一个文本文件并创建一个字典变量
- ios - 无法在表视图内的集合视图上正确填充数组数据
- excel - 如何在数据库中上传时处理excel中的空白值还希望在excel中下载表而不在jsp中指定列名
- c++ - 没有初始化器的 constexpr 静态数据成员
- selenium - 如何为复选框选择定位器
- sql - 根据外部列表选择 MySQL 数据库上的数据
- c# - 如何收集选定的 Gridview 项目以移动到下一个片段进行付款
- powerbi - PowerBI 中去年的数据