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?

Upvotes: 0

Views: 2277

Answers (1)

Ajay Shrestha
Ajay Shrestha

Reputation: 2455

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();
            }

Upvotes: 2

Related Questions