国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android中使用ScrollView指定view的頂部懸停效果

2019-12-12 03:09:23
字體:
來源:轉載
供稿:網友

因項目中的需要實現ScrollView頂部的懸停,也不是太難便自己實現功能,話不多說,先上效果圖

紅色text一到頂上便會懸浮在上面,不會跟隨scrollview的滑動而上滑。

原理:

原理其實很簡單就是對view的gone和visible,寫兩個相同的要置頂的view,一個設置為gone,一個為visible,當可見的view超出屏幕范圍的時候,將不可以的view設置為visible,不可見的view 與scrollview要同級,這樣滑動的時候不會影響到view的位置。

直接上代碼

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"        android:layout_width="match_parent"        android:layout_height="match_parent">  <com.lanmai.ObservableScrollView    xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/scrollview"    android:layout_width="match_parent"    android:layout_height="match_parent"    >    <RelativeLayout      android:layout_width="match_parent"      android:layout_height="match_parent"      android:orientation="vertical">      <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical">          <!-- 中間就是填充的view就不寫了-->          <!--指定要置頂的view-->        <TextView          android:id="@+id/specific_text_view"          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:background="@android:color/holo_red_dark"          android:gravity="center"          android:text="text"          android:textSize="40sp"/>        <TextView          android:layout_width="match_parent"          android:layout_height="200dp"          android:background="@android:color/darker_gray"          android:gravity="center"          android:text="text"          android:textSize="40sp"/>      </LinearLayout>    </RelativeLayout>  </com.lanmai.ObservableScrollView>  <!--指定要置頂的相同的view visibility設置為gone -->  <TextView    android:id="@+id/specific_text_view_gone"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="@android:color/holo_red_dark"    android:gravity="center"    android:text="text"    android:textSize="40sp"    android:visibility="gone"/></RelativeLayout>

接下來要重寫scrollview,為什么要重寫ScrollView,scrollview的滑動監聽事件setOnScrollChangeListener 這個方法是在6.0以上才能用的。為了考慮低版本的的需求,要重寫ScrollView把接口開放出來。

重寫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(ScrollView scrollView, int x, int y, int oldx, int oldy);  }}

我把重寫的ScrollView命名為ObservableScrollView,重寫三個構造方法,都是換湯不換藥的作法,這里就不贅述。 最重要的是重寫onScrollChanged這個方法,如何把滑動監聽事件開放出去呢,其實也就是寫一個監聽回調,參數和onScrollChanged里面的的參數一樣就可以了,當然主要不是用到這些參數,只是為了判斷ScrollView的滑動事件,參數對于這個功并不是很重要。那這樣,一個簡單的自定義就寫好了scrollview

如何去用?

用法也是挺簡單的,直接上代碼

@Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_scroll_view);    mTextView = ((TextView) findViewById(R.id.specific_text_view));    mScrollView = ((ObservableScrollView) findViewById(R.id.scrollview));    mVisibleTextView = ((TextView) findViewById(R.id.specific_text_view_gone));    mTextView.setOnClickListener(this);    mScrollView.setScrollViewListener(this);  }

這里onCreate方法里面的,也簡單,拿到view 并且設置監聽事件,當然,這里多實現了一個點擊view置頂的功能,監聽設置好以后,實現相應的接,接下來就是重頭戲了

 @Override  public void onScrollChanged(ScrollView scrollView, int x, int y, int oldx, int oldy) {    int[] location = new int[2];    mTextView.getLocationOnScreen(location);    int xPosition = location[0];    int yPosition = location[1];    Log.d("ScrollViewActivity", "yPosition:" + yPosition);    int statusBarHeight = getStatusBarHeight();    Log.d("ScrollViewActivity", "statusBarHeight:" + statusBarHeight);    if (yPosition <= statusBarHeight) {      mVisibleTextView.setVisibility(View.VISIBLE);    } else {      mVisibleTextView.setVisibility(View.GONE);    }  }

onScrollChanged這個方法就是自己寫的監聽回調,里面的參數就是Scrollview滑動的時候回調出來的,里面的參數并不用去關心

int[] location = new int[2];    mTextView.getLocationOnScreen(location);    int xPosition = location[0];    int yPosition = location[1];   /* mTextView就是要懸浮的view,getLocationOnScreen(location)這個方法就是拿到view在屏幕中的位置 ,傳入一個數組,最后得到的yPosition就是view在屏幕中的高度,這里面調用了native層的實現方式,所以數組能直接附上值*/    // 值得注意的是,拿到的這個高度還包括狀態欄的高度。只要減掉就可以了,狀態欄的高度獲取獲取附上代碼:public int getStatusBarHeight() {  int result = 0;  int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");  if (resourceId > 0) {    result = getResources().getDimensionPixelSize(resourceId);  }  return result;}    int statusBarHeight = getStatusBarHeight();    Log.d("ScrollViewActivity", "statusBarHeight:" + statusBarHeight);      通過獲取到的狀態欄高度,如果小于狀態欄的高度就表示已經滑出屏幕了,將要置頂的view設置為visibvle否則設置為gone     if (yPosition <= statusBarHeight) {      mVisibleTextView.setVisibility(View.VISIBLE);    } else {      mVisibleTextView.setVisibility(View.GONE);    }

這樣scrollview的懸浮置頂的功能就實現了,這里我也給出點擊view置頂的代碼

@Override  public void onClick(View v) {    int[] location = new int[2];    v.getLocationOnScreen(location);    int x = location[0];    int y = location[1];    mScrollView.scrollBy(0, location[1] - getStatusBarHeight());  }

    當然要緩慢的滑動過程用smoothScrollBy替代就可以了

結論:

實現這種效果,找對了思路就可以很容易的寫出來了,這是一種比較簡單的實現方式了,源碼我就不貼出來了,基本已經都在了。

以上所述是小編給大家介紹的Android中使用ScrollView指定view的懸停效果,希望對大家有所幫助。。。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东莞市| 谷城县| 虞城县| 漠河县| 昭苏县| 斗六市| 潮州市| 邳州市| 南华县| 天祝| 红安县| 阿图什市| 辉南县| 仁怀市| 拜泉县| 方城县| 富宁县| 陆河县| 兴化市| 腾冲县| 庆元县| 沧州市| 西平县| 承德市| 乌恰县| 乌什县| 宿松县| 文成县| 沐川县| 苍梧县| 万年县| 梨树县| 绍兴县| 龙游县| 巴楚县| 无锡市| 兰西县| 龙口市| 灵宝市| 夏邑县| 邹城市|