- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一些类可以为回收站 View 启用项目点击。他们都工作得很好。现在我已经将它们复制到一个新项目中,我得到了问题:
cannot inherit from final 'android.support.v4.GestureDetectorCompat'
现在我的类(class)可能出了什么问题?
ClickItemTouchListener.java
package com.stack.overflow.helperClasses;
import android.content.Context;
import android.os.Build;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.OnItemTouchListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
abstract class ClickItemTouchListener implements OnItemTouchListener {
private static final String LOGTAG = "ClickItemTouchListener";
private final GestureDetectorCompat mGestureDetector;
ClickItemTouchListener(RecyclerView hostView) {
mGestureDetector = new ItemClickGestureDetector(hostView.getContext(),
new ItemClickGestureListener(hostView));
}
private boolean isAttachedToWindow(RecyclerView hostView) {
if (Build.VERSION.SDK_INT >= 19) {
return hostView.isAttachedToWindow();
} else {
return (hostView.getHandler() != null);
}
}
private boolean hasAdapter(RecyclerView hostView) {
return (hostView.getAdapter() != null);
}
@Override
public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent event) {
if (!isAttachedToWindow(recyclerView) || !hasAdapter(recyclerView)) {
return false;
}
mGestureDetector.onTouchEvent(event);
return false;
}
@Override
public void onTouchEvent(RecyclerView recyclerView, MotionEvent event) {
// We can silently track tap and and long presses by silently
// intercepting touch events in the host RecyclerView.
}
abstract boolean performItemClick(RecyclerView parent, View view, int position, long id);
abstract boolean performItemLongClick(RecyclerView parent, View view, int position, long id);
private class ItemClickGestureDetector extends GestureDetectorCompat {
private final ItemClickGestureListener mGestureListener;
public ItemClickGestureDetector(Context context, ItemClickGestureListener listener) {
super(context, listener);
mGestureListener = listener;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
final boolean handled = super.onTouchEvent(event);
final int action = event.getAction() & MotionEventCompat.ACTION_MASK;
if (action == MotionEvent.ACTION_UP) {
mGestureListener.dispatchSingleTapUpIfNeeded(event);
}
return handled;
}
}
private class ItemClickGestureListener extends SimpleOnGestureListener {
private final RecyclerView mHostView;
private View mTargetChild;
public ItemClickGestureListener(RecyclerView hostView) {
mHostView = hostView;
}
public void dispatchSingleTapUpIfNeeded(MotionEvent event) {
// When the long press hook is called but the long press listener
// returns false, the target child will be left around to be
// handled later. In this case, we should still treat the gesture
// as potential item click.
if (mTargetChild != null) {
onSingleTapUp(event);
}
}
@Override
public boolean onDown(MotionEvent event) {
final int x = (int) event.getX();
final int y = (int) event.getY();
mTargetChild = mHostView.findChildViewUnder(x, y);
return (mTargetChild != null);
}
@Override
public void onShowPress(MotionEvent event) {
if (mTargetChild != null) {
mTargetChild.setPressed(true);
}
}
@Override
public boolean onSingleTapUp(MotionEvent event) {
boolean handled = false;
if (mTargetChild != null) {
mTargetChild.setPressed(false);
final int position = mHostView.getChildPosition(mTargetChild);
final long id = mHostView.getAdapter().getItemId(position);
handled = performItemClick(mHostView, mTargetChild, position, id);
mTargetChild = null;
}
return handled;
}
@Override
public boolean onScroll(MotionEvent event, MotionEvent event2, float v, float v2) {
if (mTargetChild != null) {
mTargetChild.setPressed(false);
mTargetChild = null;
return true;
}
return false;
}
@Override
public void onLongPress(MotionEvent event) {
if (mTargetChild == null) {
return;
}
final int position = mHostView.getChildPosition(mTargetChild);
final long id = mHostView.getAdapter().getItemId(position);
final boolean handled = performItemLongClick(mHostView, mTargetChild, position, id);
if (handled) {
mTargetChild.setPressed(false);
mTargetChild = null;
}
}
}
}
ItemClickSupport.java
package com.stack.overflow.helperClasses;
import android.support.v7.widget.RecyclerView;
import android.view.HapticFeedbackConstants;
import android.view.SoundEffectConstants;
import android.view.View;
import com.mego.smscloud.R;
public class ItemClickSupport {
/**
* Interface definition for a callback to be invoked when an item in the
* RecyclerView has been clicked.
*/
public interface OnItemClickListener {
/**
* Callback method to be invoked when an item in the RecyclerView
* has been clicked.
*
* @param parent The RecyclerView where the click happened.
* @param view The view within the RecyclerView that was clicked
* @param position The position of the view in the adapter.
* @param id The row id of the item that was clicked.
*/
void onItemClick(RecyclerView parent, View view, int position, long id);
}
/**
* Interface definition for a callback to be invoked when an item in the
* RecyclerView has been clicked and held.
*/
public interface OnItemLongClickListener {
/**
* Callback method to be invoked when an item in the RecyclerView
* has been clicked and held.
*
* @param parent The RecyclerView where the click happened
* @param view The view within the RecyclerView that was clicked
* @param position The position of the view in the list
* @param id The row id of the item that was clicked
*
* @return true if the callback consumed the long click, false otherwise
*/
boolean onItemLongClick(RecyclerView parent, View view, int position, long id);
}
private final RecyclerView mRecyclerView;
private final TouchListener mTouchListener;
private OnItemClickListener mItemClickListener;
private OnItemLongClickListener mItemLongClickListener;
private ItemClickSupport(RecyclerView recyclerView) {
mRecyclerView = recyclerView;
mTouchListener = new TouchListener(recyclerView);
recyclerView.addOnItemTouchListener(mTouchListener);
}
/**
* Register a callback to be invoked when an item in the
* RecyclerView has been clicked.
*
* @param listener The callback that will be invoked.
*/
public void setOnItemClickListener(OnItemClickListener listener) {
mItemClickListener = listener;
}
/**
* Register a callback to be invoked when an item in the
* RecyclerView has been clicked and held.
*
* @param listener The callback that will be invoked.
*/
public void setOnItemLongClickListener(OnItemLongClickListener listener) {
if (!mRecyclerView.isLongClickable()) {
mRecyclerView.setLongClickable(true);
}
mItemLongClickListener = listener;
}
public static ItemClickSupport addTo(RecyclerView recyclerView) {
ItemClickSupport itemClickSupport = from(recyclerView);
if (itemClickSupport == null) {
itemClickSupport = new ItemClickSupport(recyclerView);
recyclerView.setTag(R.id.recyclerview_item_click_support, itemClickSupport);
} else {
// TODO: Log warning
}
return itemClickSupport;
}
public static void removeFrom(RecyclerView recyclerView) {
final ItemClickSupport itemClickSupport = from(recyclerView);
if (itemClickSupport == null) {
// TODO: Log warning
return;
}
recyclerView.removeOnItemTouchListener(itemClickSupport.mTouchListener);
recyclerView.setTag(R.id.recyclerview_item_click_support, null);
}
public static ItemClickSupport from(RecyclerView recyclerView) {
if (recyclerView == null) {
return null;
}
return (ItemClickSupport) recyclerView.getTag(R.id.recyclerview_item_click_support);
}
private class TouchListener extends ClickItemTouchListener {
TouchListener(RecyclerView recyclerView) {
super(recyclerView);
}
@Override
boolean performItemClick(RecyclerView parent, View view, int position, long id) {
if (mItemClickListener != null) {
view.playSoundEffect(SoundEffectConstants.CLICK);
mItemClickListener.onItemClick(parent, view, position, id);
return true;
}
return false;
}
@Override
boolean performItemLongClick(RecyclerView parent, View view, int position, long id) {
if (mItemLongClickListener != null) {
view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
return mItemLongClickListener.onItemLongClick(parent, view, position, id);
}
return false;
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean b) {
}
}
}
ids.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="recyclerview_item_click_support" type="id"/>
<item name="recyclerview_item_selection_support" type="id"/>
</resources>
最佳答案
改变自
GestureDetectorCompat
到
GestureDetector
关于java - 不能从最终的 GestureDetectorCompat 继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37548116/
考虑需要与 iOS 5 和 iOS 6 兼容的应用。 有没有办法标记纯粹为了 iOS 5 兼容性而存在的代码,以便当部署目标最终更改为 iOS 6 时它显示为编译错误(或警告)? 像这样: #IF_D
我想我知道答案但是...有什么方法可以防止全局变量被稍后执行的 修改吗? ?我知道全局变量首先是不好的,但在必要时,有没有办法让它成为“最终”或“不可变”?欢迎黑客/创造性的解决方案。谢谢 最佳答案
class Foo { final val pi = 3 } 是否每Foo对象有一个 pi成员?因此我应该把 pi在伴生对象中? 最佳答案 如果您担心内存占用,您可以考虑将此字段移动到伴随对象中。
随着可用的 Web 开发框架种类繁多,似乎总是有一种“尝试新事物”的永久动机。因此,我们中的一些人发现自己用一个框架换另一个框架,从来没有对最终结果完全满意。当然,总会有一个特定的 Web 框架可以完
在MDN中指出, If the finally block returns a value, this value becomes the return value of the entire try
我正在尝试用 JavaScript 制作一个基本的井字棋类型游戏。尽管 x 和 y 值在 if 语句的范围内,但除最后一个之外的所有空格都有效。 我不知道为什么最后的 else if 语句不起作用。
我想知道如何使用PowerMock模拟kotlin最终类(class),以便进行测试。我按照指南测试了Java最终类,但仍然出现此错误 Cannot subclass final class 有什么办
考虑以下设置: // debugger class public class Debug { // setting public final static boolean DEBUG
给定以下类(class): public class SomeClass { private final int a; public SomeClass(int a) {
This question already has answers here: What does “final” do if you place it before a variable?
我有一个类PasswordEncryptor,它使用org.jasypt.util.password.StrongPasswordEncryptor作为其字段之一,因为我试图使应用程序“可集群”所有类
我今天有一个关于 StreamReader 类的问题。具体使用文件名参数初始化此类例如: TextReader tr = new StreamReader(fileName); 显然,当此操作完成后,
我想弄清楚什么是使用带锁的 try/finally 的最佳方式。 当我在同一个地方有 lock() 和 unlock() 时,我只使用 try/finally block 作为 JavaDoc还建议:
在 Java 中序列化后是否可以将 final transient 字段设置为任何非默认值?我的用例是一个缓存变量——这就是它是 transient 的原因。我还有一个习惯,就是制作不会改变的 Map
在this问题说 final transient 字段在序列化后不能设置为任何非默认值。那么,为什么我为 aVar1 变量设置了 3,为 aVar3 变量设置了 s3? import java.io.
在Xbox上进行开发时,我使用的是F#规范中最终工作流程的修改版。 Xbox上的.net框架似乎不支持尾部调用。因此,我必须在编译时禁用尾部调用优化。 尽管起初看来这种限制会阻止在计算表达式中使用任何
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我想让我的带有自定义对象的ArrayList成为最终对象,以便对象在设置后无法更改。 我试图这样声明它: private final ArrayList XML = new ArrayList();
我有一个场景,我需要类似于 .NET 的 try-catch-finally block 的内容。 在我的尝试中,我将创建一个#temp表,向其中插入数据并基于#temp处理其他数据集。 先是CATC
对此可能有一个简单的答案,但尝试充分使用 Butterknife,将一些 findViewById 转换为 @BindViews,并注意到我无法在需要声明为 Final 的 View 上使用 Bind
我是一名优秀的程序员,十分优秀!