Android 滑動組件懸浮固定在頂部

要想實現(xiàn)的效果是如下:



場景:有些時候是內容中間的組件當滑動至頂部的時候固定顯示在頂部。

實現(xiàn)的思路:
1.目標組件(button)有兩套,放在頂部和內容中間;
2.當內容中間的組件滑動至頂部欄位置時控制顯示/隱藏頂部和中間的組件(涉及到組件獲取在屏幕的位置知識點);

activity代碼:

public class MainActivity extends AppCompatActivity implements ObservableScrollView.ScrollViewListener {
    private ObservableScrollView scrollView;
    private Button topBtn1, topBtn2, middleBtn1, middleBtn2;
    private View topPanel, middlePanel;
    private int topHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initListeners();

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        Rect frame = new Rect();
        getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;//狀態(tài)欄高度

        int titleBarHeight = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();//標題欄高度
        topHeight = titleBarHeight + statusBarHeight;
    }


    private void initViews() {
        scrollView = (ObservableScrollView) findViewById(R.id.scrollView);
        topPanel = findViewById(R.id.topPanel);
        topBtn1 = (Button) topPanel.findViewById(R.id.button1);
        topBtn2 = (Button) topPanel.findViewById(R.id.button2);

        middlePanel = findViewById(R.id.middlePanel);
        middleBtn1 = (Button) middlePanel.findViewById(R.id.button1);
        middleBtn2 = (Button) middlePanel.findViewById(R.id.button2);

    }

    private void initListeners() {
        topBtn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                middleBtn1.setBackgroundColor(Color.WHITE);
                topBtn1.setBackgroundColor(Color.WHITE);
            }
        });

        middleBtn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                middleBtn1.setBackgroundColor(Color.BLUE);
                topBtn1.setBackgroundColor(Color.BLUE);
            }
        });

        scrollView.setScrollViewListener(this);


    }


    @Override
    public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
        int[] location = new int[2];
        middleBtn1.getLocationOnScreen(location);
        int locationY = location[1];
        Log.e("locationY", locationY + "   " + "topHeight的值是:" + topHeight);

        if (locationY <= topHeight && (topPanel.getVisibility() == View.GONE || topPanel.getVisibility() == View.INVISIBLE)) {
            topPanel.setVisibility(View.VISIBLE);
        }

        if (locationY > topHeight && topPanel.getVisibility() == View.VISIBLE) {
            topPanel.setVisibility(View.GONE);
        }

    }
}

要點解析:
1.在onWindowFocusChanged()方法中獲取屏幕狀態(tài)欄和標題欄的高度(在onCreate()方法中是獲取是0);
2.因為布局中的ScrollView的onScrollChangeListener()方法低版本API不支持——>所以activity實現(xiàn)了自定義ScrollView中的onScrollChanged()接口方法——>在此方法中實現(xiàn)組件的顯示/隱藏;

自定義ScrollView的代碼:

public class ObservableScrollView extends ScrollView {

    private ScrollViewListener scrollViewListener = null;

    public ObservableScrollView(Context context) {
        super(context);
    }

    public ObservableScrollView(Context context, AttributeSet attrs,
                                int defStyle) {
        super(context, attrs, defStyle);
    }

    public ObservableScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setScrollViewListener(ScrollViewListener scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }

    @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);
        if (scrollViewListener != null) {
            scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
        }
    }

    public interface ScrollViewListener {

        void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);

    }
}

然后是布局文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.slideholdapp.MainActivity">

    <com.example.administrator.slideholdapp.ObservableScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scrollView">

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="30dp"
                android:text="@string/content" />

            <include android:id="@+id/middlePanel" layout="@layout/middle_item_layout"></include>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:text="@string/content" />


        </LinearLayout>

    </com.example.administrator.slideholdapp.ObservableScrollView>

    <include android:id="@+id/topPanel" layout="@layout/middle_item_layout" android:visibility="gone"/>
</FrameLayout>

下載地址>>
GitHub資源地址>>

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,094評論 25 709
  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 15,399評論 4 61
  • 內容抽屜菜單ListViewWebViewSwitchButton按鈕點贊按鈕進度條TabLayout圖標下拉刷新...
    皇小弟閱讀 47,163評論 22 665
  • 湘北的農村,溝渠是極常見的。然而這些年在外漂泊著,偶爾回一次故鄉(xiāng),那些記憶中的溝渠,卻漸漸模糊了,直至終于掩蓋在一...
    南山采菊閱讀 461評論 0 0
  • 項目中我們會用到頭像或是在視圖展示時,設置“友好”“不尖銳”的圓角,進行美化,當視圖的渲染幀率低于 40 幀每秒,...
    騎行怪狀閱讀 3,109評論 3 21

友情鏈接更多精彩內容