2014-01-05 105 views
1

我有一個方法在我需要從活動調用的片段中。從片段調用方法

活動運行正常如果我將此代碼添加到活動中,但我想保持活動清潔並將代碼保留在片段中。

我該怎麼做?

這是如果我理解你的問題,你想從活動調用片段方法Foreg(View v)的片段

public void Foreg(final View v) { 
    ValueAnimator box = ValueAnimator.ofFloat(0, 1); 
    box.setDuration(1500); 
    box.start(); 

    box.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 

     @Override 
     public void onAnimationUpdate(ValueAnimator animation) { 
      // TODO Auto-generated method stub 

      float value = ((Float) (animation.getAnimatedValue())) 
        .floatValue(); 
      v.setRotationX(value * -180); 
      v.setPivotX(v.getWidth()/2); 
      v.setPivotY(v.getHeight()); 
      v.setCameraDistance(10000); 
      v.setClickable(false); 

      /* 
      * float value = ((Float) (animation.getAnimatedValue())) 
      * .floatValue(); v.setRotationY(value * -160); v.setPivotX(00); 
      * v.setPivotY(300); v.setCameraDistance(3500); 
      * v.setClickable(false); 
      */ 

     } 
    }); 

    ValueAnimator box2 = ValueAnimator.ofFloat(1, 0); 
    box2.setDuration(1500); 
    box2.setStartDelay(4000); 
    box2.start(); 

    box2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 

     @Override 
     public void onAnimationUpdate(ValueAnimator animation) { 
      // TODO Auto-generated method stub 

      float value = ((Float) (animation.getAnimatedValue())) 
        .floatValue(); 
      v.setRotationX(value * -180); 
      v.setPivotX(v.getWidth()/2); 
      v.setPivotY(v.getHeight()); 
      v.setCameraDistance(10000); 
      v.setClickable(true); 

      /* 
      * float value = ((Float) (animation.getAnimatedValue())) 
      * .floatValue(); v.setRotationY(value * -160); v.setPivotX(00); 
      * v.setPivotY(300); v.setCameraDistance(3500); 
      * v.setClickable(true); 
      */ 
     } 
    }); 
} 

回答

0

的方法。

試試這個方法:

FragmentManager fm = getSupportFragmentManager();  
YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentById(R.id.your_fragment_id); 
fragment.Foreg(View v); 
+0

只是一個問題,我在哪裏 「R.id.your_fragment_id」 謝謝! –

+1

在您的佈局xml文件中。如果您沒有通過xml添加片段,那麼您必須通過標記來獲取片段。 下面是一個例子: YourFragmentClass fragment =(YourFragmentClass)fm.findFragmentByTag(「yourTag」); –

+0

我用碎片創建了一個ViewPager,我如何得到Id或Tag? –