2015-07-05 6 views
8

मैं कस्टम लेआउट (टेक्स्ट व्यू और स्विच) और एक कस्टम एडाप्टर के साथ एक सरल रीसाइक्लिंग व्यू बना रहा हूं।रीसाइक्लिंगव्यू के अंदर वस्तुओं के लिए हैंडल टच इवेंट - एंड्रॉइड

मेरी समस्या यह है कि मैं स्विच के लिए क्लिक ईवेंट को संभाल नहीं सकता (जो इसे अपने राज्य को टॉगल करता है), मैंने रीसाइक्लिंगव्यू के लिए एक टच श्रोता बनाया है और जब भी मैं स्विच पर क्लिक करता हूं तो यह आग लगती है।

कई परीक्षणों के बाद स्विच टच श्रोता भी आग लगती है, लेकिन मैं रीसाइक्लर के लिए स्पर्श की घटना को अक्षम करना चाहता हूं यदि स्विच क्लिक किया गया है। यहाँ

public CategoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

    View row = layoutInflater.inflate(R.layout.single_row_category, parent, false); 
    CategoryViewHolder holder = new CategoryViewHolder(row); 

    return holder; 
} 

public void onBindViewHolder(CategoryViewHolder holder, final int position) { 
    AlarmCategory thisCat = categories.get(position); 

    holder.catName.setText(thisCat.getCategoryName()); 

    holder.catStatus.setChecked(thisCat.isOn()); 

    holder.catStatus.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View view, MotionEvent motionEvent) { 
      Log.d("fpp", view.getClass().getName() + " is clicked"); 
      return true; 
     } 
    }); 
} 

public class CategoryViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener { 

    public TextView catName; 
    public SwitchCompat catStatus; 

    public CategoryViewHolder(View itemView) { 
     super(itemView); 
     itemView.setOnCreateContextMenuListener(this); 

     catName = (TextView) itemView.findViewById(R.id.category_name); 
     catStatus = (SwitchCompat) itemView.findViewById(R.id.category_switch); 
    } 

    @Override 
    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) { 
     menu.add(0, 1001, 0, "Edit");//groupId, itemId, order, title 
     menu.add(0, 1002, 1, "Delete"); 
    } 
} 

और मेरे recyclerView स्पर्श हैंडलर है::

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="60.0dip" 
android:layout_weight="1.0" 
android:orientation="horizontal" 
android:paddingLeft="16dp"> 

<TextView 
    android:id="@+id/category_name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="false" 
    android:layout_centerVertical="true" 
    android:layout_gravity="center_vertical" 
    android:text="Category Name" 
    android:textAppearance="?android:textAppearanceLarge" /> 

<android.support.v7.widget.SwitchCompat 
    android:id="@+id/category_switch" 
    android:layout_width="80dp" 
    android:layout_height="wrap_content" 
    android:layout_alignParentEnd="true" 
    android:layout_alignParentRight="true" 
    android:layout_centerVertical="true" 
    android:layout_gravity="center_vertical" 
    android:checked="true" 
    android:clickable="true" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:paddingRight="16dp" /> 

</RelativeLayout> 

यह मेरा एडाप्टर कोड है:

यहाँ मेरी लेआउट कोड है

class RecyclerTouchListener implements RecyclerView.OnItemTouchListener { 

    private GestureDetector gestureDetector; 
    private ClickListener clickListener; 

    public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) { 

     this.clickListener = clickListener; 

     gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { 
      @Override 
      public boolean onSingleTapUp(MotionEvent e) { 
       return true; 
      } 

      @Override 
      public void onLongPress(MotionEvent e) { 
       View child = recyclerView.findChildViewUnder(e.getX(), e.getY()); 
       if (child != null && clickListener != null) { 
        clickListener.onLongClick(child, recyclerView.getChildPosition(child)); 
       } 
      } 
     }); 
    } 

    @Override 
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { 
     View child = recyclerView.findChildViewUnder(e.getX(), e.getY()); 
     if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) { 
      clickListener.onClick(child, rv.getChildPosition(child)); 

      Log.d("fpp", child.getClass().getName() + " is clicked"); 

      return false; 
     } 

     return false; 
    } 

    @Override 
    public void onTouchEvent(RecyclerView rv, MotionEvent e) { 

    } 

    @Override 
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { 

    } 
} 

और onCreate विधि मैं में इसे कॉल करें:

recyclerView.addOnItemTouchListener(new RecyclerTouchListener(this, recyclerView, new ClickListener() { 
     @Override 
     public void onClick(View view, int position) { 
      Intent i = new Intent(CategoriesList.this, AlarmList.class); 
      i.putExtra("catID", categories.get(position).getCategoryID()); 
      startActivity(i); 
     } 

     @Override 
     public void onLongClick(View view, int position) { 
      catPos = position; 
     } 
    })); 

कोई मदद कृपया, मैंने पहले ही बहुत सारे समाधानों की कोशिश की लेकिन खेद के लिए कुछ भी नहीं किया।

अग्रिम धन्यवाद।

उत्तर

7

एक तरीका है अपने धारक को आंतरिक रूप से किसी भी दृश्य क्लिक को संभालने के लिए अपडेट करना, उदाहरण के लिए

public class CategoryViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener, View.OnClickListener, CompoundButton.OnSetCheckedListener { 
    public CategoryViewHolder(View itemView) { 
     super(itemView); 
     itemView.setOnCreateContextMenuListener(this); 
     itemView.setOnClickListener(this); 

     catName = (TextView) itemView.findViewById(R.id.category_name); 
     catStatus = (SwitchCompat) itemView.findViewById(R.id.category_switch); 
     catStatus.setOnCheckChangedListener(this); 
    } 

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     // deal with catStatus change 
    } 

    public void onClick(View view) { 
     // deal with itemView click 
    } 
} 
+0

अपने जवाब के लिए धन्यवाद, लेकिन, मैं इस कोड लिखा था और 'जोड़ा Log.d (" FOPP ", view.getClass() getName() +" आंतरिक से क्लिक किया जाता है "।);' लेकिन के लिए कुछ भी नहीं हुआ क्षमा करें – Fareed

+0

आप एक इशारा डिटेक्टर का उपयोग कर रहे हैं जो टच इवेंट चुराता है और इसे किसी और चीज़ के लिए प्रचारित नहीं किया जाएगा। उत्तर में कोड काम करता है, लेकिन इसके लिए आपको 1) 'recyclerView.addOnItemTouchListener()' part और 2) इशारा डिटेक्टर –

+0

और लेआउट पर स्पर्श ईवेंट को कैसे संभालना है? जब स्विच को छोड़कर कुछ भी क्लिक किया जाता है तो मैं दूसरी गतिविधि पर नेविगेट करना चाहता हूं। – Fareed

संबंधित मुद्दे