如何在android中为这个coverflow应用程序实现onItemClickListener

时间:2012-10-04 02:45:51

标签: android onclick abstract-class coverflow onitemclicklistener

我无法让onItemClickListener在我的代码中运行。测试它我在onClick中放了一个Toast,如果其中一个视图(封面图中的图像)被点击,但没有结果,则在屏幕上显示一些文字。我可能做错了什么。我该怎么办?

下面是代码的两部分。第一部分来自我用于实现coverflow活动的coverFlowExample.java类文件,下面的代码的第二部分来自另一个名为AbstractAdapterView的类文件,它具有OnItemClickListener的抽象方法。

以下代码位于coverflowactivity活动的oncreate方法中。

我必须创建“int = xu位置”,因为编译器不允许我直接使用int位置变量,该变量是onItemClick方法中采用的参数的一部分

   // inner class inside of onCreate method for implememting OnIteimClickListener

        class ClickOnImage implements OnItemClickListener {  

        @Override
        public void onItemClick(CoverAdapterView<?> parent, View view,
                int position, long id) {

            int xu = position;

            Toast.makeText(CoverFlowExample.this, "clicked on one of  the images " + xu, Toast.LENGTH_SHORT).show();

        }

    }

接下来是与其他类文件中的OnItemClickListener有关的代码

  public abstract class CoverAdapterView<T extends Adapter> extends ViewGroup {

  // inside this abstract class the below code is refers to the OnItemClickListener

  /**
  * The listener that receives notifications when an item is clicked.
  */
  OnItemClickListener mOnItemClickListener;

   /**
  * Interface definition for a callback to be invoked when an item in this
  * AdapterView has been clicked.
  */
   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.
   */
    public boolean performItemClick(View view, int position, long id) {
        if (mOnItemClickListener != null) {
        playSoundEffect(SoundEffectConstants.CLICK);
        mOnItemClickListener.onItemClick(this, view, position, id);
        return true;
    }

    return false;
   }

2 个答案:

答案 0 :(得分:2)

您是否在视图对象上设置了您希望点击的OnItemClickListener?

ListView mListView;

mListView = (ListView) findViewById(R.id.xml_layout_view_name);

mListView.setOnItemClickListener(new ClickOnImage())

我的第一个猜测是你的ClickOnImage类没有连接到被点击的对象。

答案 1 :(得分:1)

尝试使用 setOnItemClickListener 方法,比如...

    CoverFlow  coverFlow;
    coverFlow = new CoverFlow (this);  

    coverFlow.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(CoverAdapterView<?> parent, View view,
                int position, long id) {

             System.out.println("----------------->"+ position );


        }

    });   
祝你好运。