1

I am using https://github.com/florent37/TutoShowcase this showcaseview library in my code.

It works fine in activity and fragment.But when I call in recyclerview item it shows multiple popups and gets blackout.

mRecyclerViewList.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    Logger.log("Call");

                    TextView textView = (TextView) mRecyclerViewList.getChildAt(0).findViewById(R.id.txt_add_tocart_btn);
                    Logger.log("Textview" + textView);
                    textView.setFocusableInTouchMode(true);
                    TutoShowcase.from((Activity) context).setContentView(R.layout.tuto_showcase_tuto_sample)
                            .setFitsSystemWindows(true).on(textView).addRoundRect(35).showOnce("1").show();
                    // unregister listener (this is important)
                    if (Build.VERSION.SDK_INT < 16) {
                        mRecyclerViewList.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    } else {
                        mRecyclerViewList.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    }
                }
            });

How can I avoid multiple popup's?

Anamika Chavan
  • 149
  • 1
  • 3
  • 14
  • If your code is not calling multiple times then it should be a library issue . Ask library contributer on github. – ADM Jan 12 '18 at 06:59

2 Answers2

1

Your question how to avoid Multiple popup's:

Just set a boolean value to avoid showing multiple times.

boolean isShown = false;

mRecyclerViewList.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    Logger.log("Call");
                    if(!isShown){
                      TextView textView = (TextView) mRecyclerViewList.getChildAt(0).findViewById(R.id.txt_add_tocart_btn);
                      Logger.log("Textview" + textView);
                      textView.setFocusableInTouchMode(true);
                      TutoShowcase.from((Activity) context).setContentView(R.layout.tuto_showcase_tuto_sample)
                            .setFitsSystemWindows(true).on(textView).addRoundRect(35).showOnce("1").show();
                       isShown = true;
                     }
                    // unregister listener (this is important)
                    if (Build.VERSION.SDK_INT < 16) {
                        mRecyclerViewList.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    } else {
                        mRecyclerViewList.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    }
                }
            });
Bhuvanesh BS
  • 13,474
  • 12
  • 40
  • 66
0

As described in documentation of OnGlobalLayoutListener:

to be invoked when the global layout state or the visibility of views within the view tree changes.

That's why you got many and many showcase views. Every time ViewTree changed, you generate showcase.

You don't need for ViewTreeObserver and GlobalLayoutListener.

Move

TutoShowcase.from

to onViewCreated for example.

Northern Poet
  • 1,955
  • 1
  • 12
  • 17
  • I am using viewTreeObserver because mRecyclerViewList.getChildAt(0) gets null.As getChildAt(0) gets call in some listener. – Anamika Chavan Jan 12 '18 at 07:08
  • In `Fragment.onViewCreated` all views are constructed and allocated. Do `getChildAt` inside. And it's better to use LayoutManager's `findFirstCompletelyVisibleItemPosition` prior. – Northern Poet Jan 12 '18 at 07:19