Hide only the Action bar on Scroll not action bar tabs

不问归期 提交于 2019-12-12 08:44:26

问题


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.

After scrolling down:

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:

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.


回答1:


I think this library and its sample could help you.

Look at the top-right gif animation:

I think the sample file is called "ViewPagerTabScrollViewActivity"



来源:https://stackoverflow.com/questions/27163374/hide-only-the-action-bar-on-scroll-not-action-bar-tabs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!