当前位置:   article > 正文

Android 改变指定的文字颜色_basequickadapter更改loadmore颜色

basequickadapter更改loadmore颜色

根据项目需求,在搜索地理位置时输入关键子,当出现搜索结果时,搜索列表中的关键子变颜色其他的字体不变

代码如下

 

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;
    }
}
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/170834
推荐阅读
相关标签
  

闽ICP备14008679号