向上滚动时隐藏顶部布局,向下滚动时显示顶部布局(例如collapsingToolbarLayout)

时间:2018-12-20 11:31:02

标签: android android-tablayout

我遵循了这个答案https://stackoverflow.com/a/36796343,并在屏幕中间实现了布局。现在,我想在向上滚动时隐藏顶部布局,而在整个屏幕中隐藏底部标签布局。同样,在向下滚动时,页面应恢复到原始位置。 https://drive.google.com/open?id=0B_sVfcILd-_8UzlXUlBLekFDcWV2eTVJamxYaGkxVXFTVHow

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
app:layout_scrollFlags="scroll|enterAlways"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:id="@+id/top_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <include layout="@layout/display_agents_row"/>

    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/bottom_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.TabLayout
            android:id="@+id/pager_header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_gravity="center"
            android:layout_marginTop="0dp"
            android:background="@color/black"
            android:minHeight="60dp"
            app:tabGravity="fill"
            app:tabIndicatorColor="@color/red"
            app:tabMode="fixed"
            app:tabSelectedTextColor="@color/white"
            app:tabTextAppearance="@style/TextAppearance.AppCompat.Small.Inverse" />

        <android.support.v4.view.ViewPager
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/pager_header"/>
    </RelativeLayout>
</LinearLayout>


</android.support.design.widget.CoordinatorLayout>

我的片段

public class DisplayPartnerFragment extends Fragment {
Agent agent;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.display_partner, container, false);

    agent=new Gson().fromJson(getArguments().getString("agent"),Agent.class);

    if (agent!=null){
        setData();
        ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(""+agent.getName());
    }

    // Locate the viewpager in activity_main.xml
    ViewPager viewPager = (ViewPager) v.findViewById(R.id.pager);

    // Set the ViewPagerAdapter into ViewPager
    ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
    adapter.addFrag(new SelectServiceFragment(), "About");
    adapter.addFrag(new SelectServiceFragment(), "Overview");

    viewPager.setAdapter(adapter);

    TabLayout mTabLayout = (TabLayout) v.findViewById(R.id.pager_header);
    mTabLayout.setupWithViewPager(viewPager);


    return v;
}

class ViewPagerAdapter extends FragmentStatePagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

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

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFrag(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

private void setData() {

}

}

1 个答案:

答案 0 :(得分:1)

将根LinearLayout替换为CoordinatorLayout,并将app:layout_scrollFlags="scroll|enterAlways"添加到 top_layout

编辑:还将app:layout_behavior="@string/appbar_scrolling_view_behavior"添加到ViewPager