Recently, there was a demand in the project development process: the Child control TextView in the ListView Item should set hyperlinks, specify text highlighting, and then click the hyperlink to jump to the webpage of the specified URL. This is easy to jump to a hyperlink. You only need to set the SpannableString object for TextView, that is, the following code:
TextView TV = (TextView) findViewById (R. id. TV); SpannableString sp = new SpannableString ("content displayed on TV"); sp. setSpan (new URLSpan ("http://www.baidu.com"), 3, 8, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); TV. setText (sp); TV. setMovementMethod (LinkMovementMethod. getInstance ());
After this setting, you can click on the hyperlink to jump to the webpage of the specified URL. However, you may find that there is no response when you click the Item in the ListView, why? I still remember the preceding statement when setting the hyperlink: TV. setMovementMethod (LinkMovementMethod. getInstance (); now let's comment out this statement ...... Run App, and the response is returned when you click the Item. Unfortunately, the hyperlink becomes invalid again, and no response is returned even if you click the Item. Ah, it is really "the fish and the bear's paw cannot have both ends ". Now I have determined the cause of the Item failure. Why does this statement invalidate the Item click? The source code is often a sharp tool to uncover various qaba problems in the program. Let's track the source code ...... Open the source code file of TextView. The captured key code snippets are as follows:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/140107/0Q4021139-0.jpg "title =" TextView source code snippet "alt =" wKioL1LHkTyjKLGQAACZ9Ux9MIg927.jpg "/>
This code snippet is row 1,421st of the TextView source code (Android4.2.2). We find that we cannot rewrite this method to solve the problem we are facing. Continue to follow the LinkMovementMethod class, the key source code is as follows:
@ Overridepublic boolean onTouchEvent (TextView widget, Spannable buffer, MotionEvent event) {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 (); Layout layout = widget. GetLayout (); int line = layout. getLineForVertical (y); int off = layout. getOffsetForHorizontal (line, x); // gets the Hyperlink Object in textview and uses the ClickableSpan array to hold the ClickableSpan [] link = buffer. getSpans (off, off, ClickableSpan. class); if (link. length! = 0) {// This is to process the hyperlink click event ...... 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 true after processing ends. Note that when the return value is true, the click event has been clicked, it will no longer pass} else {Selection. removeSelection (buffer) ;}} return super. onTouchEvent (widget, buffer, event );}
The above source code is the 188th line of the LinkMovementMethod class. The reason why the Item click fails is found because the onTouch method always returns true and does not process it according to different situations. At this time, you may want to directly rewrite this method to solve the problem. I want to rewrite this method, and return true or false based on the actual situation. Unfortunately, this method still has no effect, and the output log information is not visible in the rewrite method, the reason is unknown for the moment. Please give us your advice! It's time to see the light, but it's still under attack ...... Don't be discouraged. There will always be bread. If you don't give up, haha. Google the keywords to find a foreign website. The title of the above article is:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/140107/0Q4026491-1.jpg "title =" 2.png" alt = "wkiom1lh1_xalytaaa2uld9jrk531.jpg"/>
At this moment, the mood is truly treasure ...... Next let's take a look ...... It is found that some people solve the problem as follows:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/140107/0Q4024b4-2.jpg "title =" 3.png" alt = "wKioL1LHldywWR1tAAE4WGfZtn4031.jpg"/>
The essence of this idea is that we started to analyze the same idea, but he put the code to be processed in the onTouch method of TextView, and then return true or false according to different situations. set the corresponding TextView in the getView () method, Run app ...... It's time to witness the miracle ...... Haha, The result really works, yeah!
Link: http://stackoverflow.com/questions/8558732/listview-textview-with-linkmovementmethod-makes-list-item-unclickable
After the robbery, I found out why foreign technologies are always much more advanced than domestic ones. I am here not to belittle my colleagues, not to support Chinese products, but to copy me from Chinese technologies, I copied your copy, and some even didn't store the original link ...... You can see how the foreign technical website solves the problem. It is step-by-step and step-by-step, not only to let you know what it is.
Welcome to QQ discussion group: 285077071
This article from the "Yan Fukun-Programming Technology" blog, please be sure to keep this source http://kinbos.blog.51cto.com/2092114/1348407