赞
踩
根据项目需求,在搜索地理位置时输入关键子,当出现搜索结果时,搜索列表中的关键子变颜色其他的字体不变
代码如下
public class StringInterceptionUtil { /** * 字符串截取变红 * * @param ,可为null * @param string2 你要变色的字符 * @param string 整个字符串 * @param numtext */ public static void StringInterceptionChangeRed(TextView numtext, String string, String string2) { int fstart = string.indexOf(string2); int fend = fstart + string2.length(); if (fend!=0&&fstart!=-1){ SpannableStringBuilder style = new SpannableStringBuilder(string); style.setSpan(new ForegroundColorSpan(Color.argb(255,126,185,255)), fstart, fend, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); numtext.setText(style); } } }
adapter中代码如下
package com.langteng.courtesy.activity; import android.support.annotation.Nullable; import android.widget.TextView; import com.amap.api.services.core.PoiItem; import com.chad.library.adapter.base.BaseQuickAdapter; import com.chad.library.adapter.base.BaseViewHolder; import com.langteng.courtesy.R; import com.langteng.qingdao.commonutils.utils.StringInterceptionUtil; import java.util.List; public class AddressAdapter extends BaseQuickAdapter<PoiItem, BaseViewHolder> { private String name = ""; public AddressAdapter(int layoutResId, @Nullable List<PoiItem> data) { super(layoutResId, data); } @Override protected void convert(BaseViewHolder helper, PoiItem item) { helper.setText(R.id.name_tv, item.getTitle()); helper.setText(R.id.address_tv, item.getProvinceName() + item.getCityName() + item.getAdName() + item.getSnippet()); if (name.length() <= item.getTitle().length()) { StringInterceptionUtil.StringInterceptionChangeRed((TextView) helper.getView(R.id.name_tv), item.getTitle(), name); } if (name.length() <= (item.getProvinceName() + item.getCityName() + item.getAdName() + item.getSnippet()).length()) { StringInterceptionUtil.StringInterceptionChangeRed((TextView) helper.getView(R.id.address_tv), item.getProvinceName() + item.getCityName() + item.getAdName() + item.getSnippet(), name); } } public void updatePosition(String position) { name = position; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。