我想做什么
我想在聊天活动中提及个人和群组。当用户输入@时,我想要弹出一个包含用户和频道数量的列表,以便他可以提及个人和频道。
我已经做了什么
我从这个问题Android: Autocomplete TextView Similar To The Facebook App得到了帮助,在这里我已经包含了我的代码。
1. 一个包含用户的ArrayList。
ArrayList<People> users = new ArrayList<User>();
for (People user : SocketSingleton.userMap.values()) {
if (user.getId() != loggedUserId) {
users.add(user);
}
}
2.拥有组列表的ArrayList
ArrayList<Group> groups = new ArrayList<Group>();
for (Group channel : SocketSingleton.listgroups.values()) {
groups.add(channel);
}
3.视图适配器
final UserAdapter Adapter = new UserAdapter(getActivity(), R.layout.all_user_list_item, users);
final GroupAdapter Adapter1 = new GroupAdapter(getActivity(), R.layout.all_groups_list_item, Groups);
4.MultiAutocompletetextview
textinput = (MultiAutoCompleteTextView) view.findViewById(R.id.message_input);
textinput.setAdapter(Adapter);
textinput.setThreshold(0);
textinput.setTokenizer(new MultiAutoCompleteTextView.Tokenizer() {
@Override
public CharSequence terminateToken(CharSequence text) {
int i = text.length();
while (i > 0 && text.charAt(i - 1) == ' ') {
i--;
}
if (i > 0 && text.charAt(i - 1) == ' ') {
return text;
} else {
if (text instanceof Spanned) {
SpannableString sp = new SpannableString(text + " ");
TextUtils.copySpansFrom((Spanned) text, 0, text.length(), Object.class, sp, 0);
return sp;
} else {
return text + " ";
}
}
}
@Override
public int findTokenStart(CharSequence text, int cursor) {
int i = cursor;
while (i > 0 && text.charAt(i - 1) != '@') {
i--;
}
//Check if token really started with @, else we don't have a valid token
if (i < 1 || text.charAt(i - 1) != '@') {
return cursor;
}
return i;
}
@Override
public int findTokenEnd(CharSequence text, int cursor) {
int i = cursor;
int len = text.length();
while (i < len) {
if (text.charAt(i) == ' ') {
return i;
} else {
i++;
}
}
return len;
}
});
textinput.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Layout layout = textinput.getLayout();
int pos = textinput.getSelectionStart();
int line = layout.getLineForOffset(pos);
int baseline = layout.getLineBaseline(line);
int bottom = textinput.getHeight();
textinput.setDropDownVerticalOffset(baseline - bottom);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
textinput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int id, KeyEvent event) {
if (id == R.id.send || id == EditorInfo.IME_NULL) {
messageSend();
return true;
}
return false;
}
});
我遇到的问题
使用这段代码,我只能提及个人。我只能为自动完成文本视图设置adapter1或adapter2。因此,它只显示用户或组。 我希望在单个适配器中显示所有列表,以便用户可以提及人员和组。 我是新手,请帮我找到一种方法。