2012-04-10 60 views
3

我使用這個例子爲我的項目
https://github.com/Udinic/SmallExamples/tree/master/ExpandAnimationExample
但是當我在任何項目上點擊,我得到了崩潰!
我的代碼:安卓:ExpandAnimation隨着片段

public class AccountContents extends Fragment { 
private AccountItem[] rootContents; 
private ListView ls1; 
private CArrayAdapter adapter; 
private Context context; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 

    final View V = inflater.inflate(R.layout.account_contents, container, 
    false); 
    context = getActivity().getApplicationContext(); 
    rootContents = fourshared.rootContents; 
    adapter = new CArrayAdapter(context, rootContents); 
    ls1 = (ListView) V.findViewById(R.id.AC_listView); 
    ls1.setAdapter(adapter); 
    ls1.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, final View view, 
      int position, long id) { 

      View toolbar = V.findViewById(R.id.toolbar); 
      ExpandAnimation expandAni = new ExpandAnimation(toolbar, 500); 
      toolbar.startAnimation(expandAni); 
     } 
    }); 
    return V; 
    } 
} 

ExpandAnimation.java:

public class ExpandAnimation extends Animation { 
private View mAnimatedView; 
private LayoutParams mViewLayoutParams; 
private int mMarginStart, mMarginEnd; 
private boolean mIsVisibleAfter = false; 
private boolean mWasEndedAlready = false; 

    /** 
* Initialize the animation 
* @param view The layout we want to animate 
* @param duration The duration of the animation, in ms 
*/ 
public ExpandAnimation(View view, int duration) { 

    setDuration(duration); 
    mAnimatedView = view; 
    mViewLayoutParams = (LayoutParams) view.getLayoutParams(); 

    // if the bottom margin is 0, 
    // then after the animation will end it'll be negative, and invisible. 
    mIsVisibleAfter = (mViewLayoutParams.bottomMargin == 0); 

    mMarginStart = mViewLayoutParams.bottomMargin; 
    mMarginEnd = (mMarginStart == 0 ? (0- view.getHeight()) : 0); 

    view.setVisibility(View.VISIBLE); 
} 

@Override 
protected void applyTransformation(float interpolatedTime, Transformation t) { 
    super.applyTransformation(interpolatedTime, t); 

    if (interpolatedTime < 1.0f) { 

     // Calculating the new bottom margin, and setting it 
     mViewLayoutParams.bottomMargin = mMarginStart 
       + (int) ((mMarginEnd - mMarginStart) * interpolatedTime); 

     // Invalidating the layout, making us seeing the changes we made 
     mAnimatedView.requestLayout(); 

    // Making sure we didn't run the ending before (it happens!) 
    } else if (!mWasEndedAlready) { 
     mViewLayoutParams.bottomMargin = mMarginEnd; 
     mAnimatedView.requestLayout(); 

     if (mIsVisibleAfter) { 
      mAnimatedView.setVisibility(View.GONE); 
     } 
     mWasEndedAlready = true; 
    } 
} 
} 

對不起,我的英語:(

回答

2

試試這個代碼

在獲取上下文

的第一個錯誤

context = V.getContext(); 

第二次是在列表中,單擊事件

View toolbar =view.findViewById(R.id.toolbar); 

你更新的代碼

public class AccountContents extends Fragment { 
private AccountItem[] rootContents; 
private ListView ls1; 
private CArrayAdapter adapter; 
private Context context; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 

    final View V = inflater.inflate(R.layout.account_contents, container, 
    false); 
    context = V.getContext(); 
    rootContents = fourshared.rootContents; 
    adapter = new CArrayAdapter(context, rootContents); 
    ls1 = (ListView) V.findViewById(R.id.AC_listView); 
    ls1.setAdapter(adapter); 
    ls1.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, final View view, 
      int position, long id) { 

      View toolbar = view.findViewById(R.id.toolbar); 
      ExpandAnimation expandAni = new ExpandAnimation(toolbar, 500); 
      toolbar.startAnimation(expandAni); 
     } 
    }); 
    return V; 
    } 
}