android listview actionmode,Linkify, ListView & ActionMode. Regular Text no longer clickable

So I have a listview that shows random texts. The textview may sometimes be regular text, but can also be URLs and the like. I added the following line to my BaseAdapter's code:

所以我有一個列表視圖,顯示隨機文本。 textview有時可以是常規文本,但也可以是URL等。我在BaseAdapter的代碼中添加了以下行:

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

Where mHolder is an instance of a ViewHolder to speed the listView up, content being the TextView inside that viewHolder, and I am only willing to highlight Web Urls.

其中mHolder是ViewHolder的一個實例,用於加速listView,內容是viewHolder中的TextView,我只想突出顯示Web Urls。

First Problem,

第一個問題,

Since adding that line of code some of the items that only have regular text like "asdfg" are now no longer highlighted when clicked. And just to clarify, When I mean "highlighted" I only mean the regular highlight that happens when a listView item is clicked, whether or not there was code supplied to handle the onClick event.

由於添加了該行代碼,因此單擊時只會顯示一些僅包含常規文本(如“asdfg”)的項目。只是為了澄清,當我的意思是“突出顯示”時,我只是指單擊listView項時發生的常規突出顯示,無論是否提供代碼來處理onClick事件。

I know the issue here is that line of code, since removing it makes all items on the listView highlight when clicked. I tried to describe this via this screenshot

我知道這里的問題是代碼行,因為刪除它會使listView上的所有項目在單擊時突出顯示。我試圖通過這個截圖描述這個

Second Problem,

第二個問題,

I added a MultiChoiceModeListener to the listView to handle ActionMode; however, given that the links inside the textViews are now clickable and will launch the browser, they are no longer functioning properly when in ActionMode, as they do not care that we are in ActionMode and still launch the browser instead of following the code inside the onItemCheckedStateChanged method.

我在listView中添加了一個MultiChoiceModeListener來處理ActionMode;但是,鑒於textViews中的鏈接現在可以單擊並啟動瀏覽器,它們在ActionMode中不再正常工作,因為它們並不關心我們是否在ActionMode中並且仍然啟動瀏覽器而不是跟隨內部的代碼onItemCheckedStateChanged方法。

For example, when in ActionMode, every item that the user clicks is added to an ArrayList called itemsChecked that is later used via onActionItemClicked. However, when using linkify this is no longer viable, as when the user clicks a ListView item that has a web Url as text, the app will be sent to the background as the browser is called to handle the link.

例如,在ActionMode中,用戶單擊的每個項目都會添加到名為itemsChecked的ArrayList中,該列表稍后將通過onActionItemClicked使用。但是,當使用linkify時,這不再可行,因為當用戶單擊具有Web Url作為文本的ListView項時,應用程序將在調用瀏覽器處理鏈接時發送到后台。

My questions is What can I do to potentially fix these issues in my code?

我的問題是我可以做些什么來在我的代碼中修復這些問題?

I want to have links highlighted and clickable, but not at this cost. Should I use something else other than Linkify to highlight links and make them clickable? Or is that "clickability" always going to interfere with the regular behavior of a ListView item.

我想要突出顯示和點擊鏈接,但不是這個成本。我應該使用除Linkify之外的其他內容來突出顯示鏈接並使其可點擊嗎?或者“可點擊性”總是會干擾ListView項的常規行為。

FYI, the code that I am running for the ActionMode and ListView are the following: (Keep in mind that the code works appropriately when NOT using Linkify

僅供參考,我為ActionMode和ListView運行的代碼如下:(請記住,在不使用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();

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);

}

1 个解决方案

#1

0

Alright, so after some time lurking in the shadows for an answer, I finally got it thanks to the AOSP MMS code. Credit goes to Rascarlo for posting on github, although i think I may also be available through CM and other projects.

好吧,所以經過一段時間潛伏在陰影中尋找答案后,我終於得到了它,感謝AOSP MMS代碼。感謝Rascarlo在github上發布,盡管我認為我也可以通過CM和其他項目獲得。

So, the problem was that I was using this line of code inside my adapter:

所以,問題是我在我的適配器中使用了這行代碼:

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

When all that can be better address on the XML of the textview, by using the pre-built properties android:autoLink, which must be set to whatever scheme you prefer - web in my case; and android:linksClickable, which MUST be set to FALSE if you want the clicks to highlight the line item. Setting it to true will do as well, context menu via longclick works, but it just doesn't highlight. Please note that by setting it to false, you will have to handle the opening of such links via code, and not via click.

當所有這些都可以更好地解決textview的XML時,通過使用預構建的屬性android:autoLink,它必須設置為你喜歡的任何方案 - 在我的情況下web;和android:linksClickable,如果你想讓點擊突出顯示行項目,必須設置為FALSE。將它設置為true也可以,通過longclick工作的上下文菜單,但它不會突出顯示。請注意,通過將其設置為false,您必須通過代碼處理此類鏈接的打開,而不是通過單擊來處理。

Here is the XML code for my ListView item:

這是我的ListView項的XML代碼:

android:id="@+id/RelativeLayout1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

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 that helps those who could not figure it out.

希望能幫助那些無法理解的人。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值