带有自定义标记器的Android MultiAutoCompleteTextView,如whatsapp GroupChat

时间:2016-11-07 08:46:30

标签: java android tokenize whatsapp multiautocompletetextview

我想为@sh创建自定义标记器,如whatspp功能(当打开组并写入@然后打开弹出列表,用户可以选择任何用户。也可以删除该字符串@。

我搜索了很多东西。但我发现Twitter就像搜索功能Example like twitter

但在此,当用户可以写@时,不要显示列表的弹出窗口。用户可以在@之后编写soemthing然后根据输入,弹出窗口将显示搜索结果。

我想展示这样的东西:

先谢谢。

enter image description here

3 个答案:

答案 0 :(得分:0)

请参阅TokenAutoComplete,希望有所帮助

答案 1 :(得分:0)

我的问题得到了解决方案。

我已经为multiautocompletetextview创建了自己的自定义视图,并在@sign之后为打开的弹出窗口添加了performFiltering方法。

public class KcsMultiAutoCompleteTextView extends MultiAutoCompleteTextView {
    public KcsMultiAutoCompleteTextView(Context context) {
        super(context);
    }

    public KcsMultiAutoCompleteTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public KcsMultiAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void performFiltering(CharSequence text, int start, int end, int keyCode) {
        if (text.charAt(start) == '@') {
            start = start + 1;
        } else {
            text = text.subSequence(0, start);
            for (int i = start; i < end; i++) {
                text = text + "*";
            }
        }
        super.performFiltering(text, start, end, keyCode);
    }

}

答案 2 :(得分:0)

今天我正在寻找这个。我使用了 android-multiautocomplete 库。检查与库一起提供的演示。简而言之,您必须从库中扩展 2 个类,一个 tokenFilter(描述您的模式,在本例中为 @ 字符)和一个 ViewBinder(用于自定义建议行,例如将用户图像放置在某个位置等) )。所以对我来说,这是 TokenFilter :

import androidx.annotation.NonNull;
import ridmik.one.entities.MucOptions;
import ridmik.one.ui.components.autocomplete.filter.HandleTokenFilter;

public class MucUserTokenFilter extends HandleTokenFilter<MucOptions.User> {

  public MucUserTokenFilter() {
    this('@');
  }
  public MucUserTokenFilter(char handleChar) {
    super(handleChar);
  }

  @Override
  protected boolean matchesConstraint(@NonNull MucOptions.User user, @NonNull CharSequence constraint) {
    return user.getComparableName().toLowerCase().contains(constraint.toString().toLowerCase());
  }

  @Override
  public @NonNull CharSequence toTokenString(@NonNull MucOptions.User user) {
    return handleChar +user.getComparableName(); // Add handler at the begining! this is the trick!
  }
}

这是ViewBinder

public class MucUserViewBinder implements AutoCompleteViewBinder<MucOptions.User> {
  public static final String TAG = MucUserViewBinder.class.getSimpleName();
  @Override
  public long getItemId(@NonNull @NotNull MucOptions.User user) {
    try{
      if(user!=null) {
        return user.userId;  // ei line e crash kore :/
      }
    }catch (Exception x) {
      Timber.tag(TAG).e("Exception "+x.getMessage());
    }


    return 0;
  }

  @Override
  public int getItemLayoutId() {
    return R.layout.simple_autocomplete_row;
  }

  @NonNull
  @NotNull
  @Override
  public AutoCompleteViewHolder getViewHolder(@NonNull @NotNull View view) {
    return new MucUserViewHolder(view);
  }

  @Override
  public void bindData(@NonNull @NotNull AutoCompleteViewHolder viewHolder, @NonNull @NotNull MucOptions.User user,
                       @Nullable @org.jetbrains.annotations.Nullable CharSequence constraint) {
    MucUserViewHolder itemViewHolder = (MucUserViewHolder) viewHolder;
    itemViewHolder.textView.setText(user.getComparableName());
    String smallAvatar = user.image + "_sm.png";
    Timber.tag(TAG).e("user.image = "+smallAvatar);
    Picasso.get().load(smallAvatar)
        .placeholder(ContextCompat.getDrawable(itemViewHolder.rootView.getContext(),
            R.drawable.ic_avatar_placeholder)).into(itemViewHolder.avatar);
  }

  static class MucUserViewHolder extends AutoCompleteViewHolder {
    public final TextView textView;
    public final CircleImageView avatar;
    public final View rootView;

    protected MucUserViewHolder(@NonNull @NotNull View view) {
      super(view);
      this.rootView = view;
      this.textView = view.findViewById(R.id.textView);
      this.avatar = view.findViewById(R.id.avatar);
    }
  }
}

之后,您转到您的活动/片段,然后执行以下操作:


private MultiAutoComplete multiAutoComplete = null; // A field in your Activity / Fragment class, initialize it to null.

void initAutoSuggestions() {
  
ArrayList<MucOptions.User> someList = getUsers(); // <--- suggestions dataset depending on your code.
        AutoCompleteTypeAdapter<MucOptions.User> nameTypeAdapter =
            AutoCompleteTypeAdapter.Build.from(new MucUserViewBinder(), new MucUserTokenFilter());


        nameTypeAdapter.setItems(someList);

        this.multiAutoComplete = new MultiAutoComplete.Builder()
            .tokenizer(new PrefixTokenizer('@'))
            .addTypeAdapter(nameTypeAdapter)
            // .delayer(constraint -> { return 10; }) // eta ki bujhi ni
            .build();
        
        this.multiAutoComplete.onViewAttached(binding.textinput); // binding.textinput is your multiAutoCompleteView 
}

然后在 onCreate(用于 Activity)或 onViewCreated(用于片段)中调用它。 最后,在 onDestroy 内部:

    @Override
    public void onDestroy() {
        super.onDestroy();
          if(multiAutoComplete!=null) {
            this.multiAutoComplete.onViewDetached(); // needed for @mention that thing
        }
}

您还可以查看库内的 Demo 以查看其他选项。