Changing LayoutParams of always visible Chat Heads to not always visible

后端 未结 1 1963
星月不相逢
星月不相逢 2021-01-19 00:47

I am attempting to add a view I am generating via a service. The code I am using is based on Facebook Chatheads which are always visible, regardless of the apps state. They

相关标签:
1条回答
  • 2021-01-19 01:06

    "I now wish to constrain the chat head to the active app."

    I see two options. As a simple hack (keeping the Service) use Option 1.
    Option 2 means copying BubblesService.java to BubblesLocal.java and BubblesManager.java to BubblesManagerLocal.java, and hacking out all the Service code. I suggest Option 1 is what you want (much easier, and you can turn it on and off).

    Option 1

    Simply hide the bubbles when your application is not active.
    Add the following code to your project (tested, working):

    MainActivity.java:

    //update ActionBarActivity to AppCompatActivity 
    `public class MainActivity extends AppCompatActivity //ActionBarActivity`
    private boolean mStarted = false;
     @Override
    protected void onCreate(Bundle savedInstanceState) {
    ...
            initializeBubblesManager();
            mStarted = true;
    //------------------------------------------------------------------------------------------------
        @Override
        protected void onResume() 
        {
            Log.i("MainActivity:","onResume");
            super.onResume();
            if(mStarted) bubblesManager.showBubbles();
        }
    //------------------------------------------------------------------------------------------------
        @Override
        protected void onPause() 
        {
            Log.i("MainActivity:","onPause");
            super.onPause();
            if(mStarted) bubblesManager.hideBubbles();
        }
    //------------------------------------------------------------------------------------------------
    

    BubblesManager.java:

    //------------------------------------------------------------------------------------------------
        public void showBubbles()
        {
            if(bounded && bubbleServiceConnection != null)bubblesService.showBubbles();
        }//showBubbles
    //------------------------------------------------------------------------------------------------
        public void hideBubbles()
        {
            if(bounded && bubbleServiceConnection != null)bubblesService.hideBubbles();
        }//hideBubbles
    //------------------------------------------------------------------------------------------------
    

    BubblesService.java:

    //------------------------------------------------------------------------------------------------
        public void showBubbles()
        {
            if(bubbles.size() > 0)
            {
                for (BubbleLayout bubble : bubbles) 
                {
                    bubble.showBubble();
                }
            }
        }//showBubbles
    //------------------------------------------------------------------------------------------------
        public void hideBubbles()
        {
            if(bubbles.size() > 0)
            {
                for (BubbleLayout bubble : bubbles) 
                {
                    bubble.hideBubble();
                }
            }
        }//hideBubbles
    //------------------------------------------------------------------------------------------------
    

    BubbleLayout.java:

    //------------------------------------------------------------------------------------------------
        public void showBubble()
        {
                //View.GONE This view is invisible, and it doesn't take any space for layout purposes.
                //View.INVISIBLE This view is invisible, but it still takes up space for layout purposes.
    
            getRootView().setVisibility(View.VISIBLE);
        }//showBubble
    //------------------------------------------------------------------------------------------------
        public void hideBubble()
        {
            getRootView().setVisibility(View.INVISIBLE);
        }//hideBubble
    //------------------------------------------------------------------------------------------------
    
    0 讨论(0)
提交回复
热议问题