Moving a fragment partially off screen

青春壹個敷衍的年華 提交于 2019-12-10 10:18:02

问题


i have been stumped on this for quite a while now. I am trying to move my fragment which is contained in a FrameLayout, to the right, so that only the left 20% of the fragment is visible.

The problem I think I am running into is that the parent won't let the fragment move outside its bounds, or I do not know how to move it. Everything I have tried has just shoved the fragment up against the right wall, and scaling the fragment to fit. I need it to push the fragment outside the right wall. Any help would be more than appreciated.

The final effect I am trying to achieve, is that when you push a put-on the fragment translates into full view, and then with the push of a button translates back to 20% view. i have the animation all set up, just the actual moving of the fragment is stumping me.


回答1:


It means that although it animates, that does not mean the fragment itself is where the animation makes it appear to be. It will still receive OnClick events at the original position until it is physically moved.

You are probably using old-style animations (e.g., TranslateAnimation), which were designed for transient effects, not permanent ones.

The reason I pointed you to a related question of mine was that many of the solutions, including my own solution use the new animator framework, which is designed for longer-lasting effects.

Also that example does not help me, I am trying to move it partially off screen, not just scrunch it up against an edge.

Particularly with respect to the animator framework, there is no material difference between "move it partially off screen" and "scrunch it up against an edge". For example, if you had read my solution, you would have seen a translateWidgets() method demonstrating a horizontal slide of a set of widgets:

private void translateWidgets(int deltaX, View... views) {
    for (final View v : views) {
      v.setLayerType(View.LAYER_TYPE_HARDWARE, null);

      v.animate().translationXBy(deltaX).setDuration(ANIM_DURATION)
       .setListener(new AnimatorListenerAdapter() {
         @Override
         public void onAnimationEnd(Animator animation) {
           v.setLayerType(View.LAYER_TYPE_NONE, null);
         }
       });
    }
  }

In your case, deltaX would be whatever the appropriate value is to achieve your 20% effect (presumably 0.2f times the container's width, or something along those lines), or to undo that effect when the full fragment should be seen.

My answer contains links to full sample projects, both for the native API Level 11 animators, and using the NineOldAndroids backport of the animator framework.



来源:https://stackoverflow.com/questions/12849931/moving-a-fragment-partially-off-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!