日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android顶部粘至视图具体解释

發(fā)布時(shí)間:2023/12/13 Android 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android顶部粘至视图具体解释 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

不知從某某時(shí)間開始,這樣的效果開始在UI設(shè)計(jì)中流行起來了。讓我們先來看看效果:


大家在支付寶、美團(tuán)等非常多App中都有使用。要實(shí)現(xiàn)這個(gè)效果,我們能夠來分析下思路:

我們肯定要用2個(gè)一樣的布局來顯示我們的粘至布局。一個(gè)是正常的、還有一個(gè)是到頂部不動(dòng)的。正常的那個(gè),隨著scroll一起滾,該滾到哪滾到哪。僅僅是他滾到最上面的時(shí)候,

我們須要用粘至的布局,放到頂部。當(dāng)然。他還在后面繼續(xù)滾,ok。如今我們來看看詳細(xì)怎樣實(shí)現(xiàn):

先看布局,just a demo。用幾張圖片略微做做樣子。

粘至布局:

<?

xml version="1.0" encoding="UTF-8"?

> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="50dp" android:orientation="horizontal" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:textColor="#FFFFFF" android:background="#232323" android:text="我不會(huì)動(dòng)" android:textSize="30dp" /> </LinearLayout>


主布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/parent_layout"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><com.xys.scrolltrick.TrickScrollandroid:id="@+id/scrollView"android:layout_width="fill_parent"android:layout_height="fill_parent" ><FrameLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical" ><ImageViewandroid:id="@+id/iamge"android:layout_width="match_parent"android:layout_height="200dp"android:background="@drawable/ic_launcher"android:scaleType="centerCrop" /><includeandroid:id="@+id/stick"layout="@layout/stick_layout" /><ImageViewandroid:layout_width="match_parent"android:layout_height="200dp"android:background="@drawable/ic_launcher"android:scaleType="centerCrop" /><ImageViewandroid:layout_width="match_parent"android:layout_height="200dp"android:background="@drawable/ic_launcher"android:scaleType="centerCrop" /><ImageViewandroid:layout_width="match_parent"android:layout_height="200dp"android:background="@drawable/ic_launcher"android:scaleType="centerCrop" /></LinearLayout><includeandroid:id="@+id/normal"layout="@layout/stick_layout" /></FrameLayout></com.xys.scrolltrick.TrickScroll></LinearLayout>
加入多個(gè)imageview是為了讓他能滾起來

因?yàn)镾crollView中并沒有提供scroll listener,因此我們僅僅能重寫下,來創(chuàng)建一個(gè)接口:

package com.xys.scrolltrick;import android.content.Context; import android.util.AttributeSet; import android.widget.ScrollView;public class TrickScroll extends ScrollView {public interface onScrollListener {public void onScroll(int scrollY);}private onScrollListener onScrollListener;public TrickScroll(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}public TrickScroll(Context context, AttributeSet attrs) {super(context, attrs);}public TrickScroll(Context context) {super(context);}public void setOnScrollListener(onScrollListener onsScrollListener) {this.onScrollListener = onsScrollListener;}@Overrideprotected void onScrollChanged(int l, int t, int oldl, int oldt) {super.onScrollChanged(l, t, oldl, oldt);if (onScrollListener != null) {onScrollListener.onScroll(t);}} }
我們給他的滑動(dòng)。提供一個(gè)監(jiān)聽接口。

主程序:

package com.xys.scrolltrick;import android.app.Activity; import android.os.Bundle; import android.view.ViewTreeObserver.OnGlobalLayoutListener; import android.widget.LinearLayout;import com.xys.scrolltrick.TrickScroll.onScrollListener;public class MainActivity extends Activity implements onScrollListener {private TrickScroll mScroll;// 正常狀態(tài)下的布局private LinearLayout mLayout1;// 頂部粘至的布局private LinearLayout mLayout2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mScroll = (TrickScroll) findViewById(R.id.scrollView);mLayout1 = (LinearLayout) findViewById(R.id.stick);mLayout2 = (LinearLayout) findViewById(R.id.normal);mScroll.setOnScrollListener(this);// 根布局狀態(tài)下。監(jiān)聽布局改變findViewById(R.id.parent_layout).getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {@Overridepublic void onGlobalLayout() {onScroll(mScroll.getScrollY());}});}@Overridepublic void onScroll(int scrollY) {// 獲取正常布局的位置來又一次設(shè)置粘至布局的位置int layoutTop = Math.max(scrollY, mLayout1.getTop());mLayout2.layout(0, layoutTop, mLayout2.getWidth(),layoutTop + mLayout2.getHeight());} }

當(dāng)中的核心就在onScroll(int scrollY)這種方法中。我們用max函數(shù)來推斷當(dāng)前最大值


這里須要注意2個(gè)方法:

1、getTop():該方法返回該view到容器的top像素

2、getScrollY():該方法返回的是。你的scrollview已經(jīng)滾了多少

-------------------一旦這個(gè)世界有了scroll整個(gè)世界就不一樣了-------------------

知道了這2個(gè)方法后。我們就能夠推斷了,當(dāng)滾的距離小于getTop()的時(shí)候。保持與正常的一樣,大于的時(shí)候,就須要用getScrollY()了,這個(gè)比較難理解,事實(shí)上你能夠把布局想象成一個(gè)紙帶,而手機(jī)屏幕是一個(gè)挖了孔的框,我們?cè)诤竺鎻南峦侠垘?#xff0c;這樣就模擬了scroll滑動(dòng),這樣理解的話,會(huì)比較清楚點(diǎn)


以上。


轉(zhuǎn)載于:https://www.cnblogs.com/blfbuaa/p/7077769.html

總結(jié)

以上是生活随笔為你收集整理的Android顶部粘至视图具体解释的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。