如何使用 CoordinatorLayout 和 LinearLayout 隐藏和显示 App Bar,而不是 recyclerview

How to hide and show App Bar with CoordinatorLayout and LinearLayout, NOT recyclerview

我正在尝试使用 Google 的新功能来实现更自然的滚动和可隐藏的工具栏,以创建一个应用程序,例如 Google Play Store 应用程序,带有一个在滚动时隐藏的应用程序栏,以及滚动时不隐藏的标签栏。

但奇怪的是,我能找到的所有解释和指南都只有所有执行此操作的选项卡的回收站视图,所以

你能有相同类型的应用栏隐藏和显示,通过滚动触发,但对于可滚动的线性布局元素吗?

简短版本:如何在选项卡中滚动浏览简单的线性布局时平滑隐藏协调器布局和应用程序栏

谢谢!

activity_part_three.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways" />
    <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabTextColor="@android:color/white"
            app:tabSelectedTextColor="@android:color/white"
            app:tabIndicatorColor="@android:color/white"
            app:tabIndicatorHeight="6dp"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<android.support.design.widget.FloatingActionButton
        android:id="@+id/fabButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_favorite_outline_white_24dp"
        android:onClick="fabAction"
        app:borderWidth="0dp"
        app:layout_behavior="pl.michalz.hideonscrollexample.ScrollingFABBehavior"/>
</android.support.design.widget.CoordinatorLayout>

PartThreeActivity.java

public class PartThreeActivity extends AppCompatActivity {

    public Toolbar mToolbar;
    public AppBarLayout appBarLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.AppThemeBlue);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_part_three);

        initToolbar();
        initViewPagerAndTabs();
    }

    private void initToolbar() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        setTitle(getString(R.string.app_name));
        mToolbar.setTitleTextColor(getResources().getColor(android.R.color.white));

        appBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);
    }

    private void initViewPagerAndTabs() {
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
        PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
        pagerAdapter.addFragment(PartThreeFragment.createInstance(20), getString(R.string.tab_1));
        pagerAdapter.addFragment(PartThreeFragment.createInstance(4), getString(R.string.tab_2));
        pagerAdapter.addFragment(HomeFragment.newInstance(), "HOME");
        viewPager.setAdapter(pagerAdapter);
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        tabLayout.setupWithViewPager(viewPager);


    }

    public void fabAction(View view) {
        System.out.println("allo");
        appBarLayout.setExpanded(false);
    }

    static class PagerAdapter extends FragmentPagerAdapter {

        private final List<Fragment> fragmentList = new ArrayList<>();
        private final List<String> fragmentTitleList = new ArrayList<>();

        public PagerAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        public void addFragment(Fragment fragment, String title) {
            fragmentList.add(fragment);
            fragmentTitleList.add(title);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
        @Override
        public CharSequence getPageTitle(int position) {
            return fragmentTitleList.get(position);
        }
    }
}

fragment_home.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="pl.michalz.hideonscrollexample.HomeFragment">

    <!-- TODO: Update blank fragment layout -->
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView android:layout_width="match_parent" android:layout_height="match_parent"
                android:text="@string/hello_blank_fragment" />

        </LinearLayout>
    </ScrollView>

</FrameLayout>

HomeFragment.java

public class HomeFragment extends Fragment {


public HomeFragment() {
    // Required empty public constructor
}

public static HomeFragment newInstance() {
    HomeFragment homeFragment = new HomeFragment();
    Bundle bundle = new Bundle();
    homeFragment.setArguments(bundle);
    return homeFragment;
}



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

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
}

}

PartThreeFragment.java

public class PartThreeFragment extends Fragment {

    public final static String ITEMS_COUNT_KEY = "PartThreeFragment$ItemsCount";

    public static PartThreeFragment createInstance(int itemsCount) {
        PartThreeFragment partThreeFragment = new PartThreeFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(ITEMS_COUNT_KEY, itemsCount);
        partThreeFragment.setArguments(bundle);
        return partThreeFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        RecyclerView recyclerView = (RecyclerView) inflater.inflate(
                R.layout.fragment_part_three, container, false);
        setupRecyclerView(recyclerView);
        return recyclerView;
    }

    private void setupRecyclerView(RecyclerView recyclerView) {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        RecyclerAdapter recyclerAdapter = new RecyclerAdapter(createItemList());
        recyclerView.setAdapter(recyclerAdapter);
    }

    private List<String> createItemList() {
        List<String> itemList = new ArrayList<>();
        Bundle bundle = getArguments();
        if(bundle!=null) {
            int itemsCount = bundle.getInt(ITEMS_COUNT_KEY);
            for (int i = 0; i < itemsCount; i++) {
                itemList.add("Item " + i);
            }
        }
        return itemList;
    }
}

fragment_part_three.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
                                            android:id="@+id/recyclerView"
                                            android:layout_width="match_parent"
                                            android:layout_height="match_parent"/>

ScrollView 不会与 CoordinatorLayout 关联。使用嵌套滚动视图。

<<android.support.v4.widget.NestedScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView android:layout_width="match_parent" android:layout_height="match_parent"
            android:text="@string/hello_blank_fragment" />

    </LinearLayout>
</<android.support.v4.widget.NestedScrollView>

如果你只在api>21时使用Listview就可以模拟出RecyclerView一样的隐藏效果。

试试 ListView.setNestedScrollingEnabled(true);