Android FragmentTransaction: How to generate an overlay slide in and move existing Fragement to left

前端 未结 2 1635
囚心锁ツ
囚心锁ツ 2021-02-10 15:23

I\'m trying to do the following. Create a new Fragement B (Menu), slide it in from the right, and i want to move (no hide or replace!) the already shown Fragment A to the left.

2条回答
  •  梦谈多话
    2021-02-10 16:24

    Solution:

    Create Relative Layout with overlapping fragments:

    
    
        
    
            
        
    
        
    
            
            
    

    and simply call .translationX(-1 * mScreenWidth) (mScreenWidth is my ScreenWidth minus the gap you want).

                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
                    ((RelativeLayout) findViewById(R.id.front_frame)).animate()
                        .translationX(-1 * mScreenWidth);
                } else {
                    TranslateAnimation slideOut = new TranslateAnimation(0, -1
                        * mScreenWidth, 0, 0);
                    slideOut.setDuration(getResources().getInteger(
                        R.integer.animation_transistion_length_short));
                    slideOut.setFillEnabled(true);
    
                    slideOut.setAnimationListener(new AnimationListener() {
    
                        @Override
                        public void onAnimationStart(Animation animation) {}
    
    
                        @Override
                        public void onAnimationRepeat(Animation animation) {}
    
    
                        @Override
                        public void onAnimationEnd(Animation animation) {
                            RelativeLayout mView = (RelativeLayout) findViewById(R.id.front_frame);
                            int[] pos = { mView.getLeft() - mScreenWidth,
                                mView.getTop(), mView.getRight(),
                                mView.getBottom() };
                            mView.layout(pos[0], pos[1], pos[2], pos[3]);
                        }
                    });
    
                    ((RelativeLayout) findViewById(R.id.front_frame))
                        .startAnimation(slideOut);
                }
    

提交回复
热议问题