SelectTextHelper- High imitation wechat chat message list free copy text, double-click to view the text content

Denver address Making the address

SelectTextHelper creates a framework for the entire web to most closely copy wechat chat messages freely, double-click to view the text content. A useful Helper that integrates the underlying TextView framework and principles. Such powerful functionality is achieved with just two class implementations and is super simple to use.

Project presentations

Message page effect Viewing content Effects
Message page selection Message page free copy magnifier
Message page selected text Check the content

Features and functions:

  • Support free selection of text
  • You can customize the following text types: cursor color, cursor size, and selected text color
  • You can select all or two characters by default
  • Support sliding to still display pop-ups
  • Support magnifier function
  • Supports custom pop-ups in the case of full selection
  • Supports pop-ups: number of lines, picture, text, listening callback, popup color, arrow picture
  • Support AndroidX

Demo

Download the APK – Demo

portal

  • Examples of imitation
  • A magnifying glass

The main implementation

By mimicking the example and improving the popover coordinate position, size plus EventBus implementation

Simple use case

1. Import code

Add selecttext Module from this project to your project

2. Create a Helper and listener for your TextView

SelectTextHelper mSelectableTextHelper=new SelectTextHelper
        .Builder(textView)// Put your textView here!!
        .setCursorHandleColor(0xFF1379D6/*mContext.getResources().getColor(R.color.colorAccent)*/)// Cursor color default 0xFF1379D6
        .setCursorHandleSizeInDp(24)// The cursor size is dp default 24
        .setSelectedColor(0xFFAFE1F4/*mContext.getResources().getColor(R.color.colorAccentTransparent)*/)// Select the color of the text default 0xFFAFE1F4
        .setSelectAll(true)// Whether to select all in the first selection default true
        .setScrollShow(true)// Whether to continue to display default true while scrolling
        .setSelectedAllNoPop(true)// The onSelectAllShowCustomPop method will be called default false if true is set
        .setMagnifierShow(true)// Magnifying glass default true
        .addItem(0/* Item icon */."Copy"/* Item description */.// Operate each item of the popover
        ()->Log.i("SelectTextHelper"."Copy")/*item callback */)
        .build();

mSelectableTextHelper.setSelectListener(new SelectTextHelper.OnSelectListener(){
        /** * click callback */
        @Override
        public void onClick(View v){
            // clickTextView(textView.getText().toString().trim());
        }

        /** * long press callback */
        @Override
        public void onLongClick(View v){
            // postShowCustomPop(SHOW_DELAY);
        }

        /** * Select text callback */
        @Override
        public void onTextSelected(CharSequence content){
            // selectedText = content.toString();
        }

        /** * Popover close callback */
        @Override
        public void onDismiss(a){}/** * Click the url callback */ in the TextView
        @Override
        public void onClickUrl(String url){}/** * Display custom popover callback */
        @Override
        public void onSelectAllShowCustomPop(a){
            // postShowCustomPop(SHOW_DELAY);
        }

        /** * reset callback */
        @Override
        public void onReset(a){
            // SelectTextEventBus.getDefault().dispatch(new SelectTextEvent("dismissOperatePop"));
        }

        /** * Remove the custom popover callback */
        @Override
        public void onDismissCustomPop(a){
            // SelectTextEventBus.getDefault().dispatch(new SelectTextEvent("dismissOperatePop"));
        }

        /** * is a rolling callback */
        @Override
        public void onScrolling(a){
        // removeShowSelectView();}});Copy the code

3. Demo provides SelectTextDialog to view text content and MainActivity to copy message lists freely

Method for viewing text content:

  • This method is relatively simple. Put textView into SelectTextHelper as in Step 2, and call reset() of SelectTextHelper in dismiss.
@Override
public void dismiss(a){
    mSelectableTextHelper.reset();
    super.dismiss();
}
Copy the code

High imitation wechat chat message list free copy method:

  • RecycleView + Adapter + the use of multiple layout is not described here, please see this project demo.

  • Refer to Step 2 to place the textView in the ViewHolder of text type in the Adapter into the SelectTextHelper and register the SelectTextEventBus.

  • SelectTextEventBus adds functions to EventBus. Register classes and methods to unregister all SelectTextEventBus eventBuses when Activity/Fragment Destroy.

  • Text ViewHolder adds the EventBus listener

/** * Custom SelectTextEvent hidden cursor */
@Subscribe(threadMode = ThreadMode.MAIN)
public void handleSelector(SelectTextEvent event){
    if(null==mSelectableTextHelper){
        return;
    }
    String type=event.getType();
    if(TextUtils.isEmpty(type)){
        return;
    }
    switch(type){
        case"dismissAllPop":
            mSelectableTextHelper.reset();
        break;
        case"dismissAllPopDelayed":
            postReset(RESET_DELAY);
        break; }}Copy the code
  • Override the onViewRecycled method in adapter, which is called when the View is recycled
@Override
public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder){
    super.onViewRecycled(holder);
    if(holder instanceof ViewHolderText){
        / / logoutSelectTextEventBus.getDefault().unregister(holder); }}Copy the code
  • Image stabilization
/** * Delay display CustomPop * anti-shake */
private void postShowCustomPop(int duration){
    textView.removeCallbacks(mShowCustomPopRunnable);
    textView.postDelayed(mShowCustomPopRunnable,duration);
}

private final Runnable mShowCustomPopRunnable=
    ()->showCustomPop(text_rl_container,textMsgBean);

/** * delay reset * to support sliding does not reset */
private void postReset(int duration){
    textView.removeCallbacks(mShowSelectViewRunnable);
    textView.postDelayed(mShowSelectViewRunnable,duration);
}

private void removeShowSelectView(a){
    textView.removeCallbacks(mShowSelectViewRunnable);
}

private final Runnable mShowSelectViewRunnable=
    ()->mSelectableTextHelper.reset();
Copy the code

If you use AndroidX to add gradle.properties first, you must add both lines

android.useAndroidX=true
android.enableJetifier=true
Copy the code