Can you make it like this?

Asked 1 years ago, Updated 1 years ago, 107 views

To be honest, I think it's easy to make this shape. The problem is that I want to put a button in that window and move on to another fragment or activity when I press the button.

Currently, the way to make it as below is whether you use Popup Window or create a layout and Intent on top of it,

If you make a Popup Window, I don't know how to make the Fragment or Activity that comes up when you press the button inside popup window only,

I can't command the Handler to automatically close after a few seconds if there's no touch or movement...

I tried Theme.dialog, but it didn't work. I think this part works in Java, but not in C#. Maybe I'm not good at it

So to recap, this is what we're trying to make today.

What should I do?ㅠ<

android popupwindow layout fragment

2022-09-22 10:55

3 Answers

Using Popup Window To place layout and change visibility How to show and hide Fragments Using BottomSheet Dialog Using Theme.Dialog There may be additional methods, but I think it will depend on whether the screen appears frequently on UX and how the UI layout is.

Override the appropriate method of handling the touch event and process it in the following direction.

public class TestLayoutActivity extends AppCompatActivity {
    private final static long TRIGGER_TIME = 10000;
    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_layout);
        start();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            restart();
        }
        return super.dispatchTouchEvent(ev);
    }

    private void start() {
        mHandler.postDelayed(runnable, TRIGGER_TIME);
    }

    private void restart() {
        mHandler.removeCallbacks(runnable);
        start();
    }

    private Runnable runnable = new Runnable() {

        @Override
        public void run() {
            somethingView.setVisibility(View.GONE);
        }
    };
}


2022-09-22 10:55

What I was going to do was, as I asked, Fragment has 1, 2, 3 and I'm going to set it up automatically when I start, so that 1 is the current Fragmnet 1 window and 2 is the window where the buttons are gathered instead of the window in the picture. That way, when TextView is pressed Button1 and Button2, it should be changed to a different text, so if you don't want to recommend this part, can you do it like a picture?<

When you press Button, the menu window appears on the small screen, and the menu window tries to look like a picture.


2022-09-22 10:55

MasterFrameFragment

Fragment to be displayed in layout1. Consists of Title Area + SlaveFragmentContainer. MasterFrameFragment appears when the Show button is pressed as shown below.

findViewById(R.id.showBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final FragmentTransaction tr = getSupportFragmentManager().beginTransaction();
                if (mMasterFrameFragment.isAdded()) {
                    if (mMasterFrameFragment.isHidden()) {
                        tr.show(mMasterFrameFragment);
                    } } else {
                        tr.hide(mMasterFrameFragment);
                    }
                } } else {
                    tr.add(R.id.fragmentContainer, mMasterFrameFragment);
                }
                tr.addToBackStack(null);
                tr.commit();
            }
        });

It also implements behavior for the back button. The backstack between Slave Fragments is implemented below, so check the stack count to implement it.

getView().findViewById(R.id.backBtn).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final FragmentManager cm = getChildFragmentManager();
            if (cm.getBackStackEntryCount() > 0) {
                cm.popBackStack();
            } } else {
                if (getActivity() != null) {
                    getActivity().onBackPressed();
                }
            }
        }
    });

SlaveChoiceFragment

Slave Fragment that occupies the bottom of the title area. There are Button1 and Button2, so choose SlaveFragment1 or SlaveFragment2. SlaveChoiceFragment is a fragment that is located at the top of the SlaveContainer, so it starts by adding it to the onActivityCreated() of the MasterFrameFragment.

onActivityCreated() in MasterFrameFragment

 @Override
 public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    final FragmentTransaction tr = getChildFragmentManager().beginTransaction();
    tr.add(R.id.slaveFragmentContainer, mSlaveChoiceFragment);
    tr.commit();
}

Now, when Button1 and Button2 are pressed, the Click Listener is implemented so that SlaveFragment1 and SlaveFragment2 are shown, respectively. Since the Fragment Container is located in the MasterFrameFragment, it implements the Transaction behavior by importing the fragment manager of the parent fragment.

@Override
    public void onClick(View v) {
        final FragmentTransaction tr = getParentFragment().getChildFragmentManager().beginTransaction();
        tr.hide(this);
        switch (v.getId()) {
            case R.id.Button1:
                if (mSlaveFragment1.isAdded()) {
                    tr.show(mSlaveFragment1);
                } } else {
                    tr.add(R.id.slaveFragmentContainer, mSlaveFragment1);
                }
                break;

            case R.id.Button2:
                if (mSlaveFragment2.isAdded()) {
                    tr.show(mSlaveFragment2);
                } } else {
                    tr.add(R.id.slaveFragmentContainer, mSlaveFragment2);
                }
                break;
        }
        tr.addToBackStack(null);
        tr.commit();
    }

Here, addToBackStack() will process the SlaveChoiceFragment to appear again when the Back button in the title area is pressed.

SlaveFragment1

This is the fragment that will be displayed when Button1 is pressed

SlaveFragment2

This is the fragment that will be displayed when Button2 is pressed


2022-09-22 10:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.