选项卡式应用程序中的按钮淡入

时间:2013-12-15 21:17:43

标签: android android-actionbar

我的应用程序中有ActionBar选项卡。

我在第二个和第三个标签中有按钮,我想在用户切换到该标签(片段)时应用淡入效果。

我不确定必须在何处或如何做。我假设不在onCreate方法中。 当我在那里尝试时,我得到一个空指针异常(显然组件尚未实例化。)

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    int selectedNavigationIndex = (savedInstanceState != null ? savedInstanceState.getInt("selectedNavigationIndex") : 0);

    InfoFragment infoFragment = new InfoFragment();
    ProductFragment productFragment = new ProductFragment();
    PesticideFragment pesticideFragment = new PesticideFragment();
    SeedFragment seedFragment = new SeedFragment();

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

    ActionBar.Tab infoTab = actionBar.newTab().setText("INFORMATION");
    ActionBar.Tab productTab = actionBar.newTab().setText("PRODUCT");
    ActionBar.Tab pesticideTab = actionBar.newTab().setText("PESTICIDE");
    ActionBar.Tab seedTab = actionBar.newTab().setText("SEED");

    infoTab.setTabListener(new TabListener(infoFragment));
    productTab.setTabListener(new TabListener(productFragment));
    pesticideTab.setTabListener(new TabListener(pesticideFragment));
    seedTab.setTabListener(new TabListener(seedFragment));

    actionBar.addTab(infoTab, 0, false);
    actionBar.addTab(productTab, 1, false);
    actionBar.addTab(pesticideTab, 2, false);
    actionBar.addTab(seedTab, 3, false);

    if (selectedNavigationIndex != -1)
    {
        actionBar.setSelectedNavigationItem(selectedNavigationIndex);
    }
}

这就是我想要实现的目标。

    Button btn_scanProduct = (Button) findViewById(R.id.ProductTracer);
    animFadein = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in);

    btn_scanProduct.startAnimation(animFadein);

main.xml中          

    <FrameLayout
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">
    </FrameLayout>
</LinearLayout>

InfoFragment.java

public class InfoFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.info, container, false);
    }
}

1 个答案:

答案 0 :(得分:2)

试试这个:

public class InfoFragment extends Fragment {
    ViewGroup rootView = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = (ViewGroup) inflater
            .inflate(R.layout.info, container, false);
        AlphaAnimation fadeIn = new AlphaAnimation(0.0f , 1.0f ) ; 
        Button btn_scanProduct = (Button) findViewById(R.id.ProductTracer);
        btn_scanProduct.startAnimation(fadeIn);
        fadeIn.setDuration(500);
        fadeIn.setFillAfter(true);

        return rootView;
    }
}