2016-03-23 112 views
0

我創建了一個名單,我想實現一個clickListener 但點擊沒有捕獲的問題Recyclerview點擊

public static class MyViewHolder extends RecyclerView.ViewHolder 
     implements View.OnClickListener { 

    public TextView design, ip, port, number, id; 

    public MyViewHolder(View view) { 
     super(view); 
     view.setOnClickListener(this); 
     design = (TextView) view.findViewById(R.id.txt_device_designation); 
     port = (TextView) view.findViewById(R.id.txt_device_port); 
     ip = (TextView) view.findViewById(R.id.txt_device_ip); 
     number = (TextView) view.findViewById(R.id.txt_device_number); 
     id = (TextView) view.findViewById(R.id.txt_device_id); 
    } 

    @Override 
    public void onClick(View v) { 
     Log.i("Test", "Test"); 
    } 
} 

現在我想,如果我點擊菜單圖標,我希望有一個菜單,會出現底部 我設法按鈕和位置的點擊,但你能不能幫我調出菜單

My Screen

public MyViewHolder(View view) { 
     super(view); 
     view.setOnClickListener(this); 
     design = (TextView) view.findViewById(R.id.txt_device_designation); 
     port = (TextView) view.findViewById(R.id.txt_device_port); 
     ip = (TextView) view.findViewById(R.id.txt_device_ip); 
     number = (TextView) view.findViewById(R.id.txt_device_number); 
     id = (TextView) view.findViewById(R.id.txt_device_id); 
     imageViewContextMenu = (ImageView) view.findViewById(R.id.iv_context_menu); 
     linearLayoutDeviceClick = (LinearLayout) view.findViewById(R.id.linearLayoutDeviceClick); 

     imageViewContextMenu.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.i("Test", String.valueOf(getPosition())); //Test succes 
      } 
     }); 

     linearLayoutDeviceClick.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.i("TestLayout", String.valueOf(getPosition())); //Test succes 

      } 
     }); 
    } 
+0

檢查此:http://www.littlerobots.nl/blog/Handle-Android-RecyclerView-Clicks/ –

回答

1

,如果你想創建點擊爲整個項目可以findViewById根佈局樣的RelativeLayout或LinearLayout中等等,然後設置你必須setOnClickListener後findViewById

  1. onclicklistner
  2. 如果你想創建點擊特定視圖你可以findViewById視圖,然後設置onclicklistner
1

添加onItemTouchListener到您的回收視圖。

mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(mActivity, mRecyclerView, new RecyclerItemClickListener.OnItemClickListener() { 
        @Override 
        public void onItemClick(View view, int position) 
        { 
         ///////// add your task 
        } 


        }); 
+0

非常感謝你們,它的作品。 –

+0

如果它對你有幫助。接受我的回答。 – KDeogharkar

0

可以在onBindViewHolder進行點擊監聽器()或onCreateViewHolder(您recyclerview適配器)。

@Override 
public void onBindViewHolder(ViewHolder holder, final int position) { 
holder.itemView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
     //perform your operations here 
     } 
}); 

} 

ItemView控件在哪裏你爲每個項目視圖中recyclerview.Use

holder.buttonName.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
     //perform your operations here 
     } 
}); 

} 

執行視圖中單擊按鈕。