如何在搜索过滤器中突出显示搜索到的文本?

用户名

我正在尝试进行搜索,以便所有“可见”搜索字母都应突出显示。我尝试使用spannable,但没有成功,也许我没有做对吗?基于此:在ListView项中突出显示搜索到的文本如何突出显示可见的文本?这是我的过滤器:

private LayoutInflater mInflater;

        private ValueFilter valueFilter;

        public MySimpleArrayAdapter(Activity context) {

            this.context = context;
            mInflater = LayoutInflater.from(context);

        }
        private class ValueFilter extends Filter {


            //Invoked in a worker thread to filter the data according to the constraint.
            @Override
            protected synchronized FilterResults performFiltering(CharSequence constraint) {

                FilterResults results = new FilterResults();

                if (constraint != null && constraint.length() > 0) {

                    ArrayList<Integer> filterList = new ArrayList<>();

                    int iCnt = listItemsHolder.Names.size();
                    for (int i = 0; i < iCnt; i++) {
                        if(listItemsHolder.Types.get(i).toString().indexOf("HEADER_")>-1){
                            continue;
                        }
                        if (listItemsHolder.Names.get(i).matches(getRegEx(constraint))||(listItemsHolder.Names.get(i).toLowerCase().contains(constraint.toString().toLowerCase()))) {
                            if(filterList.contains(i))
                                continue;

                            filterList.add(i);

                        }
                        }

                    results.count = filterList.size();

                    results.values = filterList;
                }else {
                String prefixString = getRegEx(constraint);
                mSearchText = prefixString;
                    results.count = listItemsHolder.Names.size();

                    ArrayList<Integer> tList = new ArrayList<>();
                    for(int i=0;i<results.count;i++){
                        tList.add(i);
                    }

                    results.values = tList;

                }

                return results;


}


                //Invoked in the UI thread to publish the filtering results in the user interface.
                @SuppressWarnings("unchecked")
                @Override
                protected void publishResults(CharSequence constraint, FilterResults results) {
                    ArrayList<Integer> resultsList = (ArrayList<Integer>)results.values;
                    if(resultsList != null) {
                        m_filterList = resultsList;
                    }
                    notifyDataSetChanged();
                }

            }

            public String getRegEx(CharSequence elements){
                String result = "(?i).*";
                for(String element : elements.toString().split("\\s")){
                    result += element + ".*";
                }
                result += ".*";
                return result;
            }

Thanks in advance! 

这是我的观点

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View rowView = convertView;
            ViewHolder holder;
            if(filtering && m_filterList != null && m_filterList.size() > position)
                position = m_filterList.get(position);

            if (rowView == null) {
                holder = new ViewHolder();

                mInflater = context.getLayoutInflater();
                rowView = mInflater.inflate(R.layout.rowlayout, null);
                // configure view holder
                holder.text = (TextView) rowView.findViewById(R.id.label);
                holder.text.setTextColor(Color.WHITE);
                holder.text.setSingleLine();
                holder.text.setTextSize(15);
                holder.text.setEllipsize(TextUtils.TruncateAt.END);
                holder.text.setPadding(2, 2, 6, 2);
                Typeface label = Typeface.createFromAsset(holder.text.getContext().getAssets(),
                        "fonts/arial-bold.ttf");
                holder.text.setTypeface(label);
                holder.image = (ImageView) rowView.findViewById(R.id.icon);
                holder.image.setPadding(6, 4, 0, 4);
                holder.image.getLayoutParams().height = (int) getResources().getDimension(R.dimen.icon_width_height);
                holder.image.getLayoutParams().width = (int) getResources().getDimension(R.dimen.icon_width_height);
                rowView.setBackgroundResource(R.drawable.row_border);
                rowView.setPadding(2, 2, 6, 2);
                rowView.setTag(holder);
            }else {

                // fill data
                holder = (ViewHolder) rowView.getTag();
            }

            String id  = listItemsHolder.getid(position);
            String name = listItemsHolder.getName(position);
            holder.image.setVisibility(View.VISIBLE);


            if (name != null) {
                holder.text.setText(listItemsHolder.getName(position));
                ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) holder.text.getLayoutParams();
                params.leftMargin = 20;
            }else{
                holder.text.setText(id);
            }
            String fullText = listItemsHolder.getName(position);
            // highlight search text
            if (mSearchText != null && !mSearchText.isEmpty()) {
                int startPos = fullText.toLowerCase(Locale.US).indexOf(mSearchText.toLowerCase(Locale.US));
                int endPos = startPos + mSearchText.length();
                if (startPos != -1) {
                    Spannable spannable = new SpannableString(fullText);
                    ColorStateList blueColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{Color.BLUE});
                    TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, blueColor, null);
                    spannable.setSpan(highlightSpan, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    holder.text.setText(spannable);
                } else {
                    holder.text.setText(fullText);
                }
            } else {
                holder.text.setText(fullText);
            }
            return rowView;
        }
银行

假设您已经创建了一个自定义适配器,那么您可以参考以下代码:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        TextView text;

        if (convertView == null) {
            view = mInflater.inflate(mResource, parent, false);
        } else {
            view = convertView;
        }

        try {
            if (mFieldId == 0) {
                //  If no custom field is assigned, assume the whole resource is a TextView
                text = (TextView) view;
            } else {
                //  Otherwise, find the TextView field within the layout
                text = (TextView) view.findViewById(mFieldId);
            }
        } catch (ClassCastException e) {
            Log.e("ArrayAdapter", "You must supply a resource ID for a TextView");
            throw new IllegalStateException(
                    "ArrayAdapter requires the resource ID to be a TextView", e);
        }
        String item = getItem(position);
        text.setText(item);

        String fullText = getItem(position);
        // highlight search text
        if (mSearchText != null && !mSearchText.isEmpty()) {
            int startPos = fullText.toLowerCase(Locale.US).indexOf(mSearchText.toLowerCase(Locale.US));
            int endPos = startPos + mSearchText.length();

            if (startPos != -1) {
                Spannable spannable = new SpannableString(fullText);
                ColorStateList blueColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{Color.BLUE});
                TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, blueColor, null);
                spannable.setSpan(highlightSpan, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                text.setText(spannable);
            } else {
                text.setText(fullText);
            }
        } else {
            text.setText(fullText);
        }

        return view;
    }

mSearchText会在下列内被更新performFilteringArrayFilter类。

String prefixString = prefix.toString().toLowerCase();
mSearchText = prefixString;

您可以在此处的示例代码GitHub(最新更新)中找到更多详细信息

这是屏幕截图

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在php中实现多个搜索过滤器

来自分类Dev

如何在JIRA中自动将从搜索过滤器检索到的问题导出为xml

来自分类Dev

突出显示django中搜索到的文本

来自分类Dev

如何在vim编辑器中突出显示搜索到的文本?

来自分类Dev

如何在javascript中突出显示搜索文本记录?

来自分类Dev

搜索后如何在Vim中编辑突出显示的文本

来自分类Dev

如何在DataGridView中突出显示搜索文本?

来自分类Dev

如何在特定列的 DataGridView 中突出显示搜索文本?

来自分类Dev

如何在Rails中突出显示搜索到的单词?

来自分类Dev

如何在HTML文本中搜索特定文本并用颜色突出显示搜索字符串

来自分类Dev

如何在HTML文本中搜索特定文本并用颜色突出显示搜索字符串

来自分类Dev

如何在 Excel VBA 中创建一个搜索过滤器,在键入单词时在 ListBox 中显示搜索结果?

来自分类Dev

如何在垫选中包括搜索/过滤器框

来自分类Dev

如何搜索不在过滤器中的JIRA问题

来自分类Dev

弹性搜索中如何应用过滤器?

来自分类Dev

弹性搜索中如何应用过滤器?

来自分类Dev

如何忽略LDPA搜索过滤器中的多余空格?

来自分类Dev

如何为对象中的搜索创建过滤器?

来自分类Dev

如何为php中的搜索创建过滤器

来自分类Dev

如何在Plone的LDAP插件中添加其他用户搜索过滤器?

来自分类Dev

如何在免费jqGrid的onCellSelect中获取选定的搜索过滤器?

来自分类Dev

如何在Vue.js中按标题创建搜索过滤器?

来自分类Dev

如何在React JS中创建动态搜索过滤器?

来自分类Dev

Elastic Search NEST-如何在搜索中具有多个级别的过滤器

来自分类Dev

如何在Windows 7索引搜索中为特定文件类型设置过滤器?

来自分类Dev

如何在弹性搜索 + PHP 中传递空过滤器

来自分类Dev

如何在 Angular 中对嵌套的 JSON 对象使用搜索过滤器?

来自分类Dev

“如何在 'django rest_framework' 中添加搜索过滤器”

来自分类Dev

突出显示包含输入中搜索到的文本的td

Related 相关文章

  1. 1

    如何在php中实现多个搜索过滤器

  2. 2

    如何在JIRA中自动将从搜索过滤器检索到的问题导出为xml

  3. 3

    突出显示django中搜索到的文本

  4. 4

    如何在vim编辑器中突出显示搜索到的文本?

  5. 5

    如何在javascript中突出显示搜索文本记录?

  6. 6

    搜索后如何在Vim中编辑突出显示的文本

  7. 7

    如何在DataGridView中突出显示搜索文本?

  8. 8

    如何在特定列的 DataGridView 中突出显示搜索文本?

  9. 9

    如何在Rails中突出显示搜索到的单词?

  10. 10

    如何在HTML文本中搜索特定文本并用颜色突出显示搜索字符串

  11. 11

    如何在HTML文本中搜索特定文本并用颜色突出显示搜索字符串

  12. 12

    如何在 Excel VBA 中创建一个搜索过滤器,在键入单词时在 ListBox 中显示搜索结果?

  13. 13

    如何在垫选中包括搜索/过滤器框

  14. 14

    如何搜索不在过滤器中的JIRA问题

  15. 15

    弹性搜索中如何应用过滤器?

  16. 16

    弹性搜索中如何应用过滤器?

  17. 17

    如何忽略LDPA搜索过滤器中的多余空格?

  18. 18

    如何为对象中的搜索创建过滤器?

  19. 19

    如何为php中的搜索创建过滤器

  20. 20

    如何在Plone的LDAP插件中添加其他用户搜索过滤器?

  21. 21

    如何在免费jqGrid的onCellSelect中获取选定的搜索过滤器?

  22. 22

    如何在Vue.js中按标题创建搜索过滤器?

  23. 23

    如何在React JS中创建动态搜索过滤器?

  24. 24

    Elastic Search NEST-如何在搜索中具有多个级别的过滤器

  25. 25

    如何在Windows 7索引搜索中为特定文件类型设置过滤器?

  26. 26

    如何在弹性搜索 + PHP 中传递空过滤器

  27. 27

    如何在 Angular 中对嵌套的 JSON 对象使用搜索过滤器?

  28. 28

    “如何在 'django rest_framework' 中添加搜索过滤器”

  29. 29

    突出显示包含输入中搜索到的文本的td

热门标签

归档