1

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

我搜索了很多东西。但是我找到了类似 twitter 的搜索功能示例,例如 twitter

但是在这种情况下,当用户可以写 @ 时,不会显示列表的弹出窗口。用户可以在@之后写一些东西,然后根据输入,弹出窗口将显示搜索结果。

我想展示这样的东西:

先谢谢了。

在此处输入图像描述

4

3 回答 3

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 以查看其他选项。

于 2021-01-21T10:10:36.150 回答
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);
    }

}
于 2016-11-10T05:25:27.463 回答
0

请参阅TokenAutoComplete,希望对您有所帮助

于 2016-11-07T08:52:29.757 回答