2011-10-31 20 views
18

के साथ टेक्स्टव्यू के लिए OnItemClick प्राप्त नहीं होता है मेरे पास एक सूची दृश्य है जिसमें कई अन्य दृश्यों के अलावा प्रत्येक पंक्ति में एक टेक्स्ट व्यू शामिल है। TextView एचटीएमएल सामग्री प्रस्तुत करता है जिसमें लिंक हो सकते हैं।एंड्रॉइड - ListView को क्लिक करने योग्य लिंक

नीचे कोड कोड एडाप्टर में दिखाई देता है। m_textview.setMovementMethod (LinkMovementMethod.getInstance()); m_textview.setText (Html.fromHtml (myhtmlcontent));

यह सूची दृश्यों पर अब प्राप्त नहीं होने का कारण बनता है। मैंने एडाप्टर द्वारा बदले जाने वाले दृश्य पर सूची ऑनक्लिक कोड डालने का निर्णय लिया। यह वांछित के रूप में काफी काम नहीं करता है। जब मैं टेक्स्टव्यू को छोड़कर पंक्ति में कहीं भी क्लिक करता हूं तो अब मैं एक और गतिविधि लॉन्च कर सकता हूं। मैं चाहता हूं कि उपयोगकर्ता टेक्स्टव्यू के गैर-लिंक भाग पर क्लिक करें और दूसरी गतिविधि लॉन्च करें।

यदि मैं अपने मूल दृश्य के बजाय टेक्स्टव्यू पर ऑनक्लिक स्थानांतरित करता हूं, तो यह काम करता है लेकिन अब लिंक पर एक क्लिक दो घटनाओं को चलाता है - एक लिंक पर क्लिक के लिए और दूसरा टेक्स्टव्यू (जो वांछित नहीं है) के लिए।

मैंने देखा है कि google + और एंड्रॉइड काम पर जिस तरह से मैं चाहता हूं। मुझे यकीन नहीं है कि यह कैसे हासिल किया जा सकता है।

उत्तर

1

ListView आइटम के अंदर फ़ोकस करने योग्य दृश्य ListView आइटम का चयन करने की क्षमता अक्षम कर देंगे। TextView में android:focusable="false" को लागू करने से OnItemClick को फिर से काम करने की अनुमति मिल जाएगी। ट्रैकबॉल को लिंक को अनदेखा करने के लिए आपको android:focusableInTouchMode="false" को भी लागू करने की आवश्यकता हो सकती है क्योंकि ListView में फ़ोकस करने योग्य तत्व पर ट्रैकबॉल पर क्लिक करके दोनों लिंक और ListView आइटम पर क्लिक कर सकते हैं।

21

यह वास्तव में BUG है। इसे हल करने के लिए आप android:descendantFocusability="blocksDescendants" जोड़ सकते हैं, आप ListView's पंक्तियों लेआउट xml जोड़ सकते हैं। के लिए उदाहरण,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:descendantFocusability="blocksDescendants" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/lblStatusMessage" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:autoLink="web" 
    android:focusable="false" 
    android:textSize="15sp" /> 
</LinearLayout> 

स्रोत: this और this

गुड लक :)

+0

के आधार पर यह बहुत देर हो चुकी हो सकता है .. लेकिन मैं एक ही मुद्दा हो रही थी और इस अनुत्तरित पद मिल गया। :) –

0

आप एक setOnItemClickListener देखने सूची में संलग्न कर सकते हैं।

0

मुझे एक ही समस्या थी और इनमें से कोई भी जवाब मेरे लिए काम नहीं करता था। आखिरकार, मैंने विशेषता android:inputType="textMultiLine" को हटाकर समस्या को ठीक करने में कामयाब रहे।

0

टेक्स्ट व्यू जो केवल लिंक की घटनाओं को स्पर्श करने का जवाब देता है। https://stackoverflow.com/a/7327332/1768722

 public class AutoLinkTextView extends TextView { 

      public AutoLinkTextView(Context context, AttributeSet attrs, int defStyle) { 
       super(context, attrs, defStyle); 
       init(); 
      } 

      public AutoLinkTextView(Context context, AttributeSet attrs) { 
       super(context, attrs); 
       init(); 
      } 

      public AutoLinkTextView(Context context) { 
       super(context); 
       init(); 
      } 

      private void init() { 
       this.setAutoLinkMask(Linkify.ALL); 
      } 

      /** 
      * @Linkify applies to a movementMethod to the textView @LinkMovementMethod. 
      *   That movement method thought it implements a scrolling 
      *   vertically method it overrides any other scrolling method the 
      *   parent has. 
      * 
      *   Although touchEvent can be dispached to the parent, the specific 
      *   parent ScrollView needed the whole sequence ACTION_DOWN , 
      *   ACTION_MOVE, ACTION_UP to perform (sweep detection). So the 
      *   solution to this problem is after applying @Linkify we need to 
      *   remove the textView's scrolling method and handle the @LinkMovementMethod 
      *   link detection action in onTouchEvent of the textView. 
      */ 
      @Override 
      public boolean onTouchEvent(MotionEvent event) { 
       final TextView widget = (TextView) this; 
       final Object text = widget.getText(); 
       if (text instanceof Spannable) { 
        final Spannable buffer = (Spannable) text; 
        final int action = event.getAction(); 

        if (action == MotionEvent.ACTION_UP 
          || action == MotionEvent.ACTION_DOWN) { 
         int x = (int) event.getX(); 
         int y = (int) event.getY(); 

         x -= widget.getTotalPaddingLeft(); 
         y -= widget.getTotalPaddingTop(); 

         x += widget.getScrollX(); 
         y += widget.getScrollY(); 

         final Layout layout = widget.getLayout(); 
         final int line = layout.getLineForVertical(y); 
         final int off = layout.getOffsetForHorizontal(line, x); 

         final ClickableSpan[] link = buffer.getSpans(off, off, 
           ClickableSpan.class); 

         if (link.length != 0) { 
          if (action == MotionEvent.ACTION_UP) { 
           link[0].onClick(widget); 
          } else if (action == MotionEvent.ACTION_DOWN) { 
           Selection.setSelection(buffer, 
             buffer.getSpanStart(link[0]), 
             buffer.getSpanEnd(link[0])); 
          } 
          return true; 
         } 
        } 

       } 
       return false; 
      } 

      @Override 
      public void setText(CharSequence text, BufferType type) { 
       super.setText(text, type); 
       this.setMovementMethod(null); 
      } 
     } 
संबंधित मुद्दे