簡體   English   中英

如何通過滾動屏幕隱藏/顯示LinearLayout?

[英]How to hide/show a LinearLayout by scrolling the screen?

根據feed_fragment.xml,我的片段帶有一個布局,該布局顯示兩個TextViews和下面的Cards(在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>

屏幕是這個

我想在向下滾動屏幕時隱藏包含TextViews的LinearLayout,並在使用動畫向上滾動時再次顯示它。 怎么做?

好吧,我們將為此使用視差效果

首先創建這樣的標題布局文件

<?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);
        }
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM