Domanda

I've been stuck on a problem for a few days now and my Google searches started to run dry of possible solutions. I am trying to make a class that adds a View and receives onTouch events or onClick events much like the UndoBar library. The problem is the onTouch/onClick events are never called unless I add the View to a ViewGroup instead of a WindowManager. Here's my non working code below:

    final LayoutInflater mLayoutInflater = (LayoutInflater) 
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    final View mView = mLayoutInflater
            .inflate(R.layout.test, null);


    final TextView clickTextView = (TextView) 
            mView.findViewById(R.id.clickTextView);  


    WindowManager mWindowManager = (WindowManager)
            context.getSystemService(Context.WINDOW_SERVICE);

    final WindowManager.LayoutParams params = new WindowManager.LayoutParams();

    params.height = WindowManager.LayoutParams.WRAP_CONTENT;
    params.width = WindowManager.LayoutParams.WRAP_CONTENT;

    params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;

    params.format = PixelFormat.TRANSLUCENT;      
    params.type = WindowManager.LayoutParams.TYPE_TOAST;

    mWindowManager.addView(mView, params);

    clickTextView.setOnTouchListener(new OnTouchListener() 
    {

        @Override
        public boolean onTouch(View v, MotionEvent event) 
        {

            Log.i("onTouch", "onTouch has been called");

            return false;

        }
    });

}

Please note: The Context passed through here is an Activity Context.

To make this code work I need to get the root layout of the Activity and add the View to that. My question, and it may be a noob question, is why can't I get the onTouch/onClick of an event of a View that is added to the WindowManager instead?

Thanks for any help.

È stato utile?

Soluzione

Here's the code to make this work, still didn't find an explination to my question though.

    final View mView = mLayoutInflater
            .inflate(R.layout.test, activityViewGroup, false);

    activityViewGroup.addView(mView);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top