Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide only the Action bar on Scroll not action bar tabs

I am getting an issue at trying to hide the action bar while scrolling down.Then while scrolling up,the action bar have to shown again.

For Eg:

I referred this Tutorial.Here you can see the output and respective codes related to hide and show the action bar.

I am showing the output what I get it till now.

enter image description here

After scrolling down:

enter image description here

The screenshot above shown, it hide the action bar tab also.But it have to hide only the action bar.That's the major issue.

After scrolling up:

enter image description here

The screenshot above shows it displays back the action bar with tabs.

TopRatedFragment.java:

import info.androidhive.tabsswipe.R;
import android.app.ActionBar;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.widget.ScrollView;

public class TopRatedFragment extends Fragment implements ViewTreeObserver.OnScrollChangedListener {

    private float mActionBarHeight;
    private ActionBar actionBar;


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

        View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);

        final TypedArray styledAttributes = getActivity().getTheme().obtainStyledAttributes(
                new int[] { android.R.attr.actionBarSize });
        mActionBarHeight = styledAttributes.getDimension(0, 0);
        styledAttributes.recycle(); 
        actionBar = getActivity().getActionBar();  



        ((ScrollView)rootView.findViewById(R.id.parent)).getViewTreeObserver().addOnScrollChangedListener(this);


        return rootView;
    }


    @Override
    public void onScrollChanged() {

        float y = ((ScrollView)getActivity().findViewById(R.id.parent)).getScrollY();
        if (y >= mActionBarHeight && actionBar.isShowing()) {
            actionBar.hide();
        } else if ( y==0 && !actionBar.isShowing()) {
            actionBar.show();
        }
    }
}

fragment_top_rated.xml:

<?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/parent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
              android:paddingTop="?android:attr/actionBarSize" 
            android:orientation="vertical" >  

            <Button
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Button" />

            <Button
                android:id="@+id/button5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Button" />

               .
               .
    </LinearLayout>

    </ScrollView>

My issue is, While scrolling down it only have to hide the action bar only not action bar tabs.

like image 351
Steve Avatar asked Nov 27 '14 05:11

Steve


1 Answers

I think this library and its sample could help you.

Look at the top-right gif animation:

image.gif

I think the sample file is called "ViewPagerTabScrollViewActivity"

like image 100
android developer Avatar answered Nov 10 '22 15:11

android developer