2017-10-01 4 views
1

특정 단편 전환 동작에 도달하려고합니다. 오른쪽은에서 시작 지점으로 다시 같은 방향으로 사라집니다 .. 이 내 XML 애니메이션입니다오른쪽에서 왼쪽으로 단편 전환이 나타나고 왼쪽에서 오른쪽으로 사라집니다 (시작 지점으로 돌아 가기)

right_to_left.xml :

<set xmlns:android="http://schemas.android.com/apk/res/android" > 
<translate 
    android:duration="1000" 
    android:fromXDelta="100%" 
    android:fromYDelta="0%" 
    android:toXDelta="0%" 
    android:toYDelta="0%" /> 

left_to_right.xml

조각이 나타나면

: 16,

<set xmlns:android="http://schemas.android.com/apk/res/android"> 
<translate 
    android:duration="1000" 
    android:fromXDelta="0%" 
    android:fromYDelta="0%" 
    android:toXDelta="-100%" 
    android:toYDelta="0%" /> 

여기에 내가 단편 애니메이션을 달성하기 위해 노력했습니다 여러 가지 방법이다
productHolder.fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Bundle bundle = new Bundle(); 
       bundle.putSerializable(Utils.productDetailsKey, productPojo); 
       ProductDetailsFragment productDetailsFragment = new ProductDetailsFragment(); 
       productDetailsFragment.setArguments(bundle); 
       FragmentManager fragmentManager = activity.getSupportFragmentManager(); 
       FragmentTransaction transaction = fragmentManager.beginTransaction(); 
       transaction.setCustomAnimations(R.anim.product_animation_enter, R.anim.product_animation_enter); 
       transaction.add(R.id.newContainer, productDetailsFragment); 
       transaction.commit(); 
       MainActivity.transparent.setBackgroundColor(ContextCompat.getColor(context, R.color.blackTransparent)); 


      } 
     }); 

조각이 방법 하나를 사라

:

fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
       transaction.setCustomAnimations(R.anim.product_animation_exit, R.anim.product_animation_exit); 
       transaction.remove(ProductDetailsFragment.this); 
       transaction.commitAllowingStateLoss(); 
       MainActivity.transparent.setBackgroundColor(0x00000000); 


      } 
     }); 

조각이 방법이 사라지면 : image 가 사전에 감사합니다 사람들의 불행하게도 아무도 내가 예상 한대로 작동하지

fab.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.product_animation_exit); 
        animation.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime)); 
        animation.setAnimationListener(new Animation.AnimationListener() { 
         @Override 
         public void onAnimationStart(Animation animation) { 

         } 

         @Override 
         public void onAnimationEnd(Animation animation) { 
          try { 
           FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
           transaction.remove(ProductDetailsFragment.this); 
           transaction.commitAllowingStateLoss(); 
           MainActivity.transparent.setBackgroundColor(0x00000000); 

          } catch (Exception e) { 
          } 

         } 

         @Override 
         public void onAnimationRepeat(Animation animation) { 

         } 
        }); 

        getView().startAnimation(animation); 
       } 
      }); 

, 이 이미지는 내가 원하는 것을 더 설명 할 수있다.

답변

0

먼저 문제의 첫 부분, 즉 조각 사이를 전환하는 작업 솔루션을 만든 다음 두 번째 부분 인 애니메이션을 추가하는 것이 좋습니다.

1. 전환 조각 당신의 XML 레이아웃 파일에서

당신이 조각에 속하는 모든 것을 가지고 가고 Framelayout를 추가합니다. 코드에서

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

은 그렇게처럼 FrameLayout에 대한 참조를 가져 :

int containerId = R.id.fragment_container; 

는 그런 다음 두 가지 방법, 앞뒤로 두 사이의 전환을위한 첫번째 조각을 추가하기위한 하나 하나가 있습니다.

은 추가 방법은 다음과 같이 간다 :만큼

void replaceFrag(int containerId, Fragment newFragment){ 
     FragmentManager fragmentManager = activity.getSupportFragmentManager(); 
     FragmentTransaction transaction = fragmentManager.beginTransaction(); 
     transaction.replace(containerId, newFragment); 
     transaction.commit(); 
} 

을 당신이 바로 그 단편을 통과, 당신이 중 하나를 교체하기 위해이 방법을 사용할 수 있습니다

void addFrag(int containerId, Fragment firstFragment){ 
     FragmentManager fragmentManager = activity.getSupportFragmentManager(); 
     FragmentTransaction transaction = fragmentManager.beginTransaction(); 
     transaction.add(containerId, firstFragment); 
     transaction.commit(); 
} 

는 방법은 다음과 같이 간다 교체 당신의 파편들은 다른 파편과 함께 있습니다. 편집 replaceFrag도 컨테이너에 처음으로 조각을 추가하는 경우에도 사용할 수 있습니다.정말 쉬운 부분 편집

2. 애니메이션

끝. 슬라이딩 애니메이션은 Android에 내장되어 있으므로 자신의 XML을 만들 필요가 없습니다 (여기에서 제공하는 정확한 ID를 사용하십시오).

transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right); 

편집이 내장 된 애니메이션은 50 %에서 시작하기 때문에, 그러나, 그들은 보이지 않는 : 당신이 추가하거나 조각을를 교체하기 전에 당신이해야 할 모든 코드 한 줄을 추가하는 것입니다 제 의견으로는 너무 좋았습니다. 간단히 XML을 사용할 수 있습니다.

transaction.setCustomAnimations (R.anim.left_to_right, R.anim.right_to_left);

편집 완료