我正在尝试将 OnItemClickListener 添加到适用于 android 的 Coverflow 应用程序中,其中图像可以来回滚动,而我想要添加的部分是单击侦听器,因此您可以单击其中一个图像并转到另一个活动。我猜它的工作方式与将 OnItemClick 添加到列表视图或填充图像的网格视图中的方式相同。我知道这个问题之前已经被问过,但是这种情况是不同的。应用程序中有没有意义的附加代码。这是代码来自http://www.inter-fuser.com/2010/01/android-coverflow-widget.html的地方 ,这个应用程序有以下四个java类:
CoverFlow.java // 扩展 CoverAbsSpinner 并实现 GestureDetector.OnGestureListener
CoverAdapterView.java // 类扩展了 Adapter 和 ViewGroup 类
CoverAbsSpinner.java // 这个类扩展了 CoverAdapterView.java
CoverFlowExample.java // 扩展 Activity
奇怪的是我在 CoverAdapterView.java 类中找到的下面显示的代码。将看似无用的接口卡在类中间是没有意义的。它说“单击此 AdapterView 中的项目时要调用的回调方法。”
这就是方法的奥秘。你怎么“称呼”这个?他们真的准备了 OnItemClickListener 吗?我要覆盖这件事还是什么?
并且由于尚未理解此代码的原因?有谁知道这样做的目的是什么?无论如何,它是一个您必须实现或覆盖的接口。所以我质疑这样做的目的。
public interface OnItemClickListener {
/**
* Callback method to be invoked when an item in this AdapterView has
* been clicked.
* <p>
* Implementers can call getItemAtPosition(position) if they need
* to access the data associated with the selected item.
*
* @param parent The AdapterView where the click happened.
* @param view The view within the AdapterView that was clicked (this
* will be a view provided by the adapter)
* @param position The position of the view in the adapter.
* @param id The row id of the item that was clicked.
*/
void onItemClick(CoverAdapterView<?> parent, View view, int position, long id);
}
/**
* Register a callback to be invoked when an item in this AdapterView has
* been clicked.
*
* @param listener The callback that will be invoked.
*/
public void setOnItemClickListener(OnItemClickListener listener) {
mOnItemClickListener = listener;
}
/**
* @return The callback to be invoked with an item in this AdapterView has
* been clicked, or null id no callback has been set.
*/
public final OnItemClickListener getOnItemClickListener() {
return mOnItemClickListener;
}
/**
* Call the OnItemClickListener, if it is defined.
*
* @param view The view within the AdapterView that was clicked.
* @param position The position of the view in the adapter.
* @param id The row id of the item that was clicked.
* @return True if there was an assigned OnItemClickListener that was
* called, false otherwise is returned.