admin
2021-07-06 abce02c7a61820f5d580f87364d542e817be429c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.tejia.lijin.app.ui.recommend;
 
import android.content.Context;
import android.text.SpannableStringBuilder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
 
import com.tejia.lijin.app.R;
import com.tejia.lijin.app.util.wordUtil;
 
import java.util.List;
 
 
public class SearchAdapter extends BaseAdapter {
    private Context context;
    private List<String> mUserList;
    private String[] searchContent = new String[1];//用户搜索的内容
//    private ArrayFilter mFilter;
//    private ArrayList<T> mOriginalValues;
//    private final Object mLock = new Object();
//    private List<T> mObjects;
 
    public SearchAdapter(Context context, List<String> mUserList) {
        this.context = context;
        this.mUserList = mUserList;
    }
 
    public void setList(List<String> list, String key) {
        this.mUserList = list;
        searchContent[0] = key;
    }
 
    @Override
    public int getCount() {
        return mUserList == null ? 0 : mUserList.size();
    }
 
    @Override
    public Object getItem(int position) {
        return mUserList.get(position);
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(context).inflate(R.layout.item_search_list, null);
        //获取控件
        TextView textViewNumber = convertView.findViewById(R.id.search_list_tv);
        View vi = convertView.findViewById(R.id.search_list_vi);
        if (position == 0) {
            vi.setVisibility(View.VISIBLE);
        } else {
            vi.setVisibility(View.GONE);
        }
//设置控件的值
        //搜索高亮显示
        if (mUserList.get(position) != null && mUserList.get(position).length() > 0) {
            SpannableStringBuilder number = wordUtil.matcherSearchContent(mUserList.get(position) + "", searchContent);
            textViewNumber.setText(number);
        } else {
            textViewNumber.setText(mUserList.get(position) + "");
        }
 
        return convertView;
    }
//
//    @Override
//    public Filter getFilter() {
//        if (mFilter == null) {
//            mFilter = new ArrayFilter();
//        }
//        return mFilter;
//    }
//
//    /**
//     * <p>An array filter constrains the content of the array adapter with
//     * a prefix. Each item that does not start with the supplied prefix
//     * is removed from the list.</p>
//     */
//    private class ArrayFilter extends Filter {
//        @Override
//        protected FilterResults performFiltering(CharSequence prefix) {
//            final FilterResults results = new FilterResults();
//
//            if (mOriginalValues == null) {
//                synchronized (mLock) {
//                    mOriginalValues = new ArrayList<>(mObjects);
//                }
//            }
//
//            if (prefix == null || prefix.length() == 0) {
//                final ArrayList<T> list;
//                synchronized (mLock) {
//                    list = new ArrayList<>(mOriginalValues);
//                }
//                results.values = list;
//                results.count = list.size();
//            } else {
//                final String prefixString = prefix.toString().toLowerCase();
//
//                final ArrayList<T> values;
//                synchronized (mLock) {
//                    values = new ArrayList<>(mOriginalValues);
//                }
//
//                final int count = values.size();
//                final ArrayList<T> newValues = new ArrayList<>();
//
//                for (int i = 0; i < count; i++) {
//                    final T value = values.get(i);
//                    final String valueText = value.toString().toLowerCase();
//
//                    // First match against the whole, non-splitted value
//                    if (valueText.startsWith(prefixString)) {
//                        newValues.add(value);
//                    } else {
//                        final String[] words = valueText.split(" ");
//                        for (String word : words) {
//                            if (word.startsWith(prefixString)) {
//                                newValues.add(value);
//                                break;
//                            }
//                        }
//                    }
//                }
//
//                results.values = newValues;
//                results.count = newValues.size();
//            }
//
//            return results;
//        }
//
//        @Override
//        protected void publishResults(CharSequence constraint, FilterResults results) {
//            //noinspection unchecked
//            mObjects = (List<T>) results.values;
//            if (results.count > 0) {
//                notifyDataSetChanged();
//            } else {
//                notifyDataSetInvalidated();
//            }
//        }
//    }
}