我在recyclerView裏設置了一個按鈕,在那裏我點擊它Toast將顯示「RecyclerViewOnClick」而不是「RecyclerView裏面的按鈕」。recyclerView裏面的按鈕點擊不正常
如果我想顯示「按鈕裏面的RecyclerView」,我必須點擊按鈕longClick。
如何點擊recyclerView onClick中的按鈕,它會顯示「RecyclerView內的按鈕」而不是「RecyclerViewOnClick」?
任何幫助將不勝感激。
我用recyclerView點擊模式是這樣的:
public class RecyclerViewClickListener implements RecyclerView.OnItemTouchListener {
private GestureDetector mGestureDetector;
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(View view, int position);
void onItemLongClick(View view, int position);
}
public RecyclerViewClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener){
mListener = listener;
mGestureDetector = new GestureDetector(context,
new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onSingleTapUp(MotionEvent e) {
View childView = recyclerView.findChildViewUnder(e.getX(),e.getY());
if(childView != null && mListener != null){
mListener.onItemClick(childView,recyclerView.getChildLayoutPosition(childView));
return true;
}
return false;
}
@Override
public void onLongPress(MotionEvent e) {
View childView = recyclerView.findChildViewUnder(e.getX(),e.getY());
if(childView != null && mListener != null){
mListener.onItemLongClick(childView,recyclerView.getChildLayoutPosition(childView));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
if(mGestureDetector.onTouchEvent(e)){
return true;
}else
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
我recyclerView的onClick函數內部按鈕下MyAdapter擴展RecyclerView.Adapter:
public class MyViewHolder extends RecyclerView.ViewHolder{
private Button creditCardPayment;
public MyViewHolder(View itemView) {
super(itemView);
creditCardPayment=(Button)itemView.findViewById(R.id.creditCardPayment);
//I want to show this toast when i onClick button.
creditCardPayment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(),"Button inside RecyclerView",Toast.LENGTH_SHORT).show();
}
});
}
這是我的片段聲明recyclerView點擊模式:
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
recyclerView.addOnItemTouchListener(new RecyclerViewClickListener(getActivity(),recyclerView, new RecyclerViewClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
//When i click the button , the toast show "RecyclerViewOnClick" not "Button //inside recyclerView"
Toast.makeText(getActivity(),"RecyclerViewOnClick",Toast.LENGTH_SHORT).show();
}
@Override
public void onItemLongClick(View view, int position) {
}
}));
}
這是我的recyclerView佈局,我設置了一個按鈕:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:orientation="horizontal">
<LinearLayout
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/paymentStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="8dp"
android:text="@string/paymentStatus"
android:textColor="@android:color/black"
android:textSize="18dp" />
//onClick over here
<Button
android:id="@+id/creditCardPayment"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_gravity="center"
android:background="@drawable/corner_light_dark_blue"
android:text="@string/creditCardPayment"
android:textColor="@android:color/white" />
</LinearLayout>
</RelativeLayout>
不確定是什麼問題。如果你知道Toast被調用的地方,只需把你需要的代碼放在那裏。 – HaydenKai
檢查此示例http://www.truiton.com/2015/02/android-recyclerview-tutorial/ –
感謝您的回覆,可能是我把佈局代碼可以讓你瞭解我的問題,我更新了它。 –