Thắng Bùi Mạnh Thắng Bùi Mạnh - 4 years ago 133
Android Question

How to add animation for showing and hiding actionbar in android?

'public class GrammarFragment extends Fragment {

public GrammarFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_grammar, container, false);
ObservableWebView webView = (ObservableWebView) v.findViewById(R.id.grammar_webview);
//webView.loadData("ABC", "text/html", "UTF-8");
webView.loadUrl("file:///android_asset/grammar/danhtu.html");
webView.setScrollViewCallbacks(new ObservableScrollViewCallbacks() {
@Override
public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) {
}
@Override
public void onDownMotionEvent() {
}
@Override
public void onUpOrCancelMotionEvent(ScrollState scrollState) {
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (scrollState == ScrollState.UP) {
if (actionBar != null) actionBar.hide();
} else if (scrollState == ScrollState.DOWN) {
if (actionBar != null) actionBar.show();
}
}
});
return v;
}


}
`When action bar is being hide or showed, it disappears or appears on the screen almost instantly without smooth animation sliding down or up. Is there any way I can configure it to show or hide smoothly?

Answer Source

Modify your code like this

Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
            if (scrollState == ScrollState.UP) {
                if (toolbar != null) {
                    toolbar.animate().translationY(-toolbar.getBottom()).
                            setInterpolator(new AccelerateInterpolator()).start();
                }
            } else if (scrollState == ScrollState.DOWN) {
                if (toolbar != null)
                    toolbar.animate().translationY(0).
                            setInterpolator(new DecelerateInterpolator()).start();
            }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download