2016-02-28 50 views
0

我想用每个片段中的一些动画来做SwipeViews。例如,我想改变按钮的阿尔法从0到1在PagerAdapter的片段中使用动画

我写了动画

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
android:shareInterpolator="false"> 
<alpha 
    android:duration="2000" 
    android:fromAlpha="0.0" 
    android:interpolator="@android:anim/linear_interpolator" 
    android:toAlpha="1.0"/> 
</set> 

我必须跟fragmets这样

public class FragmentOne extends Fragment { 
private Button bw; 
private Animation anim; 
private Context CON; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    CON=container.getContext(); 
    View vw = inflater.inflate(R.layout.fragemnt1,container,false); 
    bw=(Button)vw.findViewById(R.id.button); 
    anim= AnimationUtils.loadAnimation(CON,R.anim.animacia); 
    bw.startAnimation(anim); 
    return inflater.inflate(R.layout.fragemnt1,container,false); 
} 
} 

与布局工作PagerAdapter

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#F20C36" > 


<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="New Button" 
    android:id="@+id/button" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="60dp" 
    android:visibility="invisible"/> 


</RelativeLayout> 

但这些动画不起作用。什么都没发生。有人能帮助我吗?

编辑:我PagerAdapter实施

public class PagerAdapter extends FragmentPagerAdapter { 

Context CON; 
public PagerAdapter(FragmentManager fm,Context con) { 
    super(fm); 
    CON=con; 
    // TODO Auto-generated constructor stub 
} 

@Override 
public Fragment getItem(int arg0) { 
    // TODO Auto-generated method stub 
    switch (arg0) { 
     case 0: 

      return new FragmentOne(); 
     case 1: 
      return new FragmentTwo(); 
     case 2: 
      return new FragmentThree(); 

     default: 
      break; 
    } 
    return null; 
} 

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return 3; 
} 


} 

回答

0

你的按钮是不可见的。你把可见性标签设置为可见吗?

此片段可能会在片段显示给用户之前启动onCreateView方法,因为PagerAdapter可以随时准备好显示,具体取决于您的应用程序。所以,你可能需要一个setOnTabSelectedListener

如果您使用您的PagerAdapter的TabLayout,你可以做这样的事情:

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 

    tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager) { 
     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 
      super.onTabSelected(tab); 
      //start animation here 
     } 
    }); 
+0

喜欢的东西OnTabSelectedListener大概是,我一直在寻找。但是我该如何使用PagerAdapter来实现它? –

+0

这取决于你如何实现你的PagerAdapter,也许你可以分享更多的代码,而不仅仅是片段。我编辑了一个如何完成TabLayout的示例的答案 – user5994441

+0

我添加了我的PagerAdapter的实现 –