Android滑動(dòng)組件懸浮固定在頂部效果
本文實(shí)例為大家分享了Android滑動(dòng)組件懸浮固定在頂部效果的具體代碼,供大家參考,具體內(nèi)容如下
要想實(shí)現(xiàn)的效果是如下:
場(chǎng)景:有些時(shí)候是內(nèi)容中間的組件當(dāng)滑動(dòng)至頂部的時(shí)候固定顯示在頂部。
實(shí)現(xiàn)的思路:
1.目標(biāo)組件(button)有兩套,放在頂部和內(nèi)容中間;
2.當(dāng)內(nèi)容中間的組件滑動(dòng)至頂部欄位置時(shí)控制顯示/隱藏頂部和中間的組件(涉及到組件獲取在屏幕的位置知識(shí)點(diǎn));
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();//標(biāo)題欄高度 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); } } }
要點(diǎn)解析:
1.在onWindowFocusChanged()方法中獲取屏幕狀態(tài)欄和標(biāo)題欄的高度(在onCreate()方法中是獲取是0);
2.因?yàn)椴季种械?span style="color: #800000">ScrollView的onScrollChangeListener()方法低版本API不支持——>所以activity實(shí)現(xiàn)了自定義ScrollView中的onScrollChanged()接口方法——>在此方法中實(shí)現(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>
更多關(guān)于滑動(dòng)功能的文章,請(qǐng)點(diǎn)擊專題: 《Android滑動(dòng)功能》
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android通過ImageView設(shè)置手指滑動(dòng)控件縮放
- Android ScrollView嵌套橫向滑動(dòng)控件時(shí)沖突問題
- Android開源堆疊滑動(dòng)控件仿探探效果
- Android自定義View實(shí)現(xiàn)隨手勢(shì)滑動(dòng)控件
- Android仿微信列表滑動(dòng)刪除之可滑動(dòng)控件(一)
- Android實(shí)現(xiàn)IOS相機(jī)滑動(dòng)控件
- Android實(shí)現(xiàn)頂部懸浮效果
- Android懸浮按鈕點(diǎn)擊返回頂部FloatingActionButton
- android 添加隨意拖動(dòng)的桌面懸浮窗口
- Android開發(fā)懸浮按鈕 Floating ActionButton的實(shí)現(xiàn)方法
- Android ScrollView實(shí)現(xiàn)向上滑動(dòng)控件頂部懸浮效果
相關(guān)文章
Android:利用SharedPreferences實(shí)現(xiàn)自動(dòng)登錄
本篇文章主要介紹了Android實(shí)現(xiàn)自動(dòng)登錄,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11Android布局之RelativeLayout相對(duì)布局
RelativeLayout是相對(duì)布局控件:以控件之間相對(duì)位置或相對(duì)父容器位置進(jìn)行排列,下面通過本文給大家介紹Android布局之RelativeLayout相對(duì)布局,涉及到android relativelayout相對(duì)布局相關(guān)知識(shí),對(duì)android relativelayout相對(duì)布局相關(guān)知識(shí),感興趣的朋友一起學(xué)習(xí)吧2015-12-12Android 實(shí)例開發(fā)一個(gè)學(xué)生管理系統(tǒng)流程詳解
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Java實(shí)現(xiàn)一個(gè)學(xué)生管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11Android實(shí)戰(zhàn)教程第九篇之短信高效備份
這篇文章主要為大家詳細(xì)介紹了Android實(shí)戰(zhàn)教程第九篇之短信高效備份,利用xml序列化器備份短信,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫效果(八)
這篇文章主要為大家詳細(xì)介紹了Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫效果的第八篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08