Linkify, ListView & ActionMode. Plain text is no longer clickable

So, I have a list that shows random texts. A textual representation can sometimes be plain text, but it can also be URLs, etc. I added the following line to the BaseAdapter code:

Linkify.addLinks(mHolder.content, Linkify.WEB_URLS); 

Where mHolder is an instance of ViewHolder to speed up the display of listView, and the content is a TextView inside this viewHolder, and I'm ready to specify only web addresses.

First problem

With the addition of this line of code, some elements that have only plain text, such as "asdfg", are no longer highlighted when pressed. And just for clarification. When I mean “highlighted,” I mean only the regular highlighting that occurs when I click on the listView element, regardless of whether the code was turned on to handle the onClick event.

I know that the problem is that the line of code, because when you delete it, all elements in the listView element are highlighted. I tried to describe it through a screenshot

Second problem

I added a MultiChoiceModeListener to the listView to handle the ActionMode; however, given that links inside text elements are now clickable and launch the browser, they no longer function properly in ActionMode, since they don’t care that we are in ActionMode and are still launching the browser instead of following the code inside onItemCheckedStateChanged .

For example, when in an ActionMode each element that the user clicks is added to an ArrayList named itemsChecked , which is later used through onActionItemClicked . However, when using linkify, this is more unviable, because when a user clicks on a ListView element that has a web url as text, the application will be sent to the background as the browser is called to process the link.

My questions What can I do to fix these problems in my code?

I want the links to be highlighted and accessible by click, but not at this price. Should I use something other than Linkify to highlight links and make them clickable? Or, this “interactivity” will always interfere with the regular behavior of the ListView.

FYI, the code that I run for ActionMode and ListView, is as follows: ( Keep in mind that the code works properly when NOT using Linkify

 private MultiChoiceModeListener modeListener = new MultiChoiceModeListener() { @SuppressLint("NewApi") @Override public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { String text = listItems.get(position); if (checked) { itemsChecked.add(text); } else { int index = itemsChecked.indexOf(text); itemsChecked.remove(index); } } @SuppressLint("NewApi") @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { switch (item.getItemId()) { case 0: mode.finish(); return true; case R.id.menu_merge: mergeStrings(); mode.finish(); return true; case R.id.menu_star: return false; default: return false; } } @SuppressLint("NewApi") @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { inActionMode = true; itemsChecked = new ArrayList<String>(); MenuInflater inflater = mode.getMenuInflater(); inflater.inflate(R.menu.context_menu, menu); return true; } @SuppressLint("NewApi") @Override public void onDestroyActionMode(ActionMode mode) { inActionMode = false; } @SuppressLint("NewApi") @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; } }; @SuppressLint("NewApi") private void initViews () { listView = (ListView) findViewById(R.id.home_list_view); if (isOldAPI) { registerForContextMenu(listView); } else { listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); listView.setMultiChoiceModeListener(modeListener); } mAdapter = new MyAdapter(this, listItems, isOldAPI); listView.setAdapter(mAdapter); } 
+4
source share
1 answer

Good, so after a while, hiding in the shadow for an answer, I finally got it thanks to the AOSP MMS code. Rascarlo's loan for publishing on github, although I think it can also be accessed through CM and other projects.

So the problem was that I used this line of code inside the adapter:

 Linkify.addLinks(mHolder.content, Linkify.WEB_URLS); 

When all that can be better addressed in the XML of the text field, using the pre-built properties of android: autoLink , which should be tuned to any scheme that you prefer - the network in my case; and android: linksClickable , for which SHOULD set FALSE if you want clicks to highlight a position. Setting it to true will also be performed, the context menu via longclick works, but it simply does not stand out. Please note that by setting it to false, you will have to handle the opening of such links using the code, and not through a click.

Here is the XML code for my ListView element:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/clip_list_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_margin="4dp" android:autoLink="web" android:linksClickable="false" android:textAppearance="?android:attr/textAppearanceMedium" /> 

Hope this helps those who could not understand it.

0
source

All Articles