2017-04-26 150 views
2

我有,我不能設置onClickListener上recyclerview一個簡單的問題,我不希望設置onClickListener上recyclerview的項目,只是recyclerview本身,我嘗試了普通方式,它沒「科技工作如何設置onClickListener上RecyclerView

(我試圖父佈局設置onClickListener但奇怪的事情不會因爲我對recyclerview設置LinearLayoutManager的工作:|)

這裏是我的代碼:

mRecyclerView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Log.d("onClick","Clicked"); 
     } 
    }); 

適配器:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> { 
private List<Message> mMessage; 

public static class ViewHolder extends RecyclerView.ViewHolder{ 
    private TextView txtSender; 
    private TextView txtMessage; 

    public ViewHolder(View v) { 
     super(v); 
     txtSender = (TextView) v.findViewById(R.id.txtSender); 
     txtMessage = (TextView) v.findViewById(R.id.txtText); 
    } 

} 
@Override 
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View inflatedView = LayoutInflater.from(parent.getContext()) 
      .inflate(R.layout.recyclerview_item_row, parent, false); 
    return new ViewHolder(inflatedView); 
} 


public RecyclerAdapter(List<Message> messages) { 
    mMessage = messages; 
} 

@Override 
public void onBindViewHolder(ViewHolder holder, int position) { 
    Message msg = mMessage.get(position); 
    holder.txtSender.setText(msg.getSender()); 
    holder.txtMessage.setText(msg.getmessage()); 
} 

@Override 
public int getItemCount() { 
    return mMessage.size(); 
} 
+0

發表您的適配器代碼... – rafsanahmad007

+0

您是否嘗試過在一個FrameLayout裏嵌入,並設置在監聽器FrameLayout裏? – ifiok

+0

嗯,這段代碼應該可以工作,但這似乎很奇怪,因爲行視圖仍然會消耗觸摸事件。 –

回答

2

根據您的需要,您需要修改R.layout.recyclerview_item_row。使用一個android LinearLayoutTextViewR.id.txtSenderR.id.txtText)都放在裏面。

修改您的如下ViewHolder

public static class ViewHolder extends RecyclerView.ViewHolder 
{ 
private LinearLayout lvContainer; 
private TextView txtSender; 
private TextView txtMessage; 

public ViewHolder(View v) { 
    super(v); 
    lvContainer = (LinearLayout) v.findViewById(R.id.lv_container); // R.id.lv_container this should be in your R.layout.recyclerview_item_row 
    txtSender = (TextView) v.findViewById(R.id.txtSender); 
    txtMessage = (TextView) v.findViewById(R.id.txtText); 
} 
} 

修改您的onBindViewHolder如下

@Override 
public void onBindViewHolder(ViewHolder holder, int position) { 
Message msg = mMessage.get(position); 
holder.txtSender.setText(msg.getSender()); 
holder.txtMessage.setText(msg.getmessage()); 
holder.lvContainer.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     //Handle your click here 
    } 
}); 
} 
+0

感謝,但我並不需要該項目的onClickListener,有項目之間的空間,我需要覆蓋整個屏幕 – Milad

+0

整個畫面意味着覆蓋單列點擊還是什麼? – Geek

+0

將一次整行點擊工作 – Geek

相關問題