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

首頁 > 系統 > Android > 正文

android仿知乎標題欄隨ScrollView滾動變色

2019-10-21 21:49:27
字體:
來源:轉載
供稿:網友

本文實例為大家分享了android標題欄隨ScrollView滾動變色的具體代碼,供大家參考,具體內容如下

參考:Android之scrollview滑動使標題欄漸變背景色的實例代碼

效果圖:

android,仿知乎,標題欄,ScrollView,變色

核心類:ObservableScrollView

package com.jukopro.titlebarcolor;import android.content.Context;import android.util.AttributeSet;import android.widget.ScrollView;/** * 帶滾動監聽的scrollview */public class ObservableScrollView extends ScrollView {  public interface ScrollViewListener {    void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);  }  private ScrollViewListener scrollViewListener = null;  public ObservableScrollView(Context context) {    super(context);  }  public ObservableScrollView(Context context, AttributeSet attrs) {    super(context, attrs);  }  public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) {    super(context, attrs, defStyle);  }  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);    }  }}

MyListview

解決ScrollView嵌套Listview的滑動沖突:

public class MyListview extends ListView {  public MyListview(Context context) {    super(context);  }  public MyListview(Context context, AttributeSet attrs) {    super(context, attrs);  }  public MyListview(Context context, AttributeSet attrs, int defStyle) {    super(context, attrs, defStyle);  }  @Override  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);    super.onMeasure(widthMeasureSpec, expandSpec);  }} 

MainActivity

package com.jukopro.titlebarcolor;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.view.ViewTreeObserver;import android.view.ViewTreeObserver.OnGlobalLayoutListener;import android.widget.ArrayAdapter;import android.widget.ImageView;import android.widget.ListView;import android.widget.TextView;import com.jukopro.titlebarcolor.ObservableScrollView.ScrollViewListener;public class MainActivity extends Activity implements ScrollViewListener {  private ObservableScrollView scrollView;  private ListView listView;  private ImageView imageView;  private TextView textView;  private int imageHeight;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    scrollView = (ObservableScrollView) findViewById(R.id.scrollview);    listView = (ListView) findViewById(R.id.listview);    imageView = (ImageView) findViewById(R.id.imageview);    textView = (TextView) findViewById(R.id.textview);    initListeners();    initData();  }  private void initListeners() {    // 獲取頂部圖片高度后,設置滾動監聽    ViewTreeObserver vto = imageView.getViewTreeObserver();    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {      @Override      public void onGlobalLayout() {        imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);        imageHeight = imageView.getHeight();        scrollView.setScrollViewListener(MainActivity.this);      }    });  }  private void initData() {    ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,       android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.data));    listView.setAdapter(adapter);  }  /**   * ScrollView滾動監聽   *   * @param scrollView:滾動控件   * @param x:x軸坐標   * @param y:y軸坐標   * @param oldx:上一個x軸坐標   * @param oldy:上一個y軸坐標   */  @Override  public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {    if (y <= 0) {      textView.setBackgroundColor(Color.argb((int) 0, 227, 29, 26));//AGB由相關工具獲得,或者美工提供    } else if (y > 0 && y <= imageHeight) {      float scale = (float) y / imageHeight;      float alpha = (255 * scale);      // 只是layout背景透明(仿知乎滑動效果)      textView.setBackgroundColor(Color.argb((int) alpha, 227, 29, 26));    } else {      textView.setBackgroundColor(Color.argb((int) 255, 227, 29, 26));    }  }}

布局:

<RelativeLayout 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">  <com.jukopro.titlebarcolor.ObservableScrollView    android:id="@+id/scrollview"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:scrollbars="none">    <LinearLayout      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:orientation="vertical">      <ImageView        android:id="@+id/imageview"        android:layout_width="match_parent"        android:layout_height="200dp"        android:background="@drawable/ic_launcher" />      <com.jukopro.titlebarcolor.MyListview        android:id="@+id/listview"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    </LinearLayout>  </com.jukopro.titlebarcolor.ObservableScrollView>  <TextView    android:id="@+id/textview"    android:layout_width="match_parent"    android:layout_height="48dp"    android:background="#00000000"    android:gravity="center"    android:text="我是標題"    android:textColor="@android:color/white"    android:textSize="18sp" /></RelativeLayout>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汉阴县| 论坛| 十堰市| 临汾市| 久治县| 周至县| 五指山市| 莱芜市| 习水县| 阜康市| 蚌埠市| 望谟县| 武宣县| 阳山县| 古丈县| 通许县| 肇源县| 葵青区| 宜兰市| 金堂县| 额济纳旗| 黑水县| 鸡西市| 临邑县| 德钦县| 漾濞| 营口市| 明溪县| 乌兰察布市| 平阳县| 金湖县| 女性| 潞西市| 宁南县| 嘉鱼县| 霍州市| 葵青区| 广平县| 南京市| 宁武县| 大连市|