如何通过滚动屏幕隐藏/显示LinearLayout?

时间:2017-08-08 19:04:35

标签: android android-layout

根据feed_fragment.xml

,我有一个带有布局的Fragment,它显示了两个TextView和下面的ListView中的卡片
<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="com.maik.projphotocrowd.YourPhotosFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/all_photos"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="All photos"
                android:textSize="16sp"
                android:layout_margin="8dp"/>

            <TextView
                android:id="@+id/just_your_photos"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="My photos"
                android:textSize="16sp"
                android:layout_margin="8dp" />
        </LinearLayout>

        <ListView
            android:id="@+id/listView_firebase_card"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

</FrameLayout>

屏幕为this

我想在向下滚动屏幕时隐藏包含TextViews的LinearLayout,并在使用动画向上滚动时再次显示它。怎么做?

1 个答案:

答案 0 :(得分:0)

我们将为此

使用视差效果

首先创建一个像这样的标题布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Space
        android:id="@+id/stickyViewPlaceholder"
        android:layout_width="match_parent"
        android:layout_height="50dp" />

</LinearLayout>

第二个让我们处理列表滚动

listView = (ListView) findViewById(R.id.listView);
textContainer = (LinearLayout) findViewById(R.id.textContainer);

// Inflate list header layout 
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View listHeader = inflater.inflate(R.layout.list_header, null);
stickyViewSpacer = listHeader.findViewById(R.id.stickyViewPlaceholder);

// Add list view header 
listView.addHeaderView(listHeader);

//Handle list View scroll events 
listView.setOnScrollListener(new AbsListView.OnScrollListener() {

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        // Check if the first item is already reached to top/
        if (listView.getFirstVisiblePosition() == 0) {
            View firstChild = listView.getChildAt(0);
            int topY = 0;
            if (firstChild != null) {
                topY = firstChild.getTop();
            }
            textContainer.setY(topY * 0.5f);
        }
    }
});