简体   繁体   中英

Input Doesn't Work as Expected When Overlaying a Fragment in Main activity

So I used this code to overlay a fragment over all of my views.

statusList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Fragment fragment = new DetailedInformerFragment();

            FragmentManager fm = getActivity().getSupportFragmentManager();
            FragmentTransaction transaction = fm.beginTransaction();
            transaction.add(android.R.id.content,fragment);
            transaction.commit();
        }
    });

The fragment gets overlayed but when I start touching the screen, it goes through the fragment. For example, there was a textbox in the main activity. When I overlay the fragment over the main activity, I touch where the textbox was, even though I don't see it and I am not supposed to, the keyboard will pop up.

My question is how do I prevent user input to the other views except the fragment which is the only thing the user can see.

Instead of using,

transaction.add(android.R.id.content, fragment);

try using,

transaction.replace(android.R.id.content, fragment);

Because you have added a fragment on-top of other content they are essentially both there, which is why your interface is getting confused. By using replace, you replace any existing fragment with the new one so they do not both exist on the interface at the same time.

A FrameLayout is probably the best practice for a container for a fragment because a FrameLayout will only allow a single child element thus enforcing this behaviour of not allowing multiple fragments/layouts into the same UI space.

Further reading:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM