編輯:關於 Android 開發

上一篇我們談完了 ListView 版本的 PullToZoomListViewEx,這次要介紹的是 PullZoomView 在 ScrollView 上的另一種實作:PullToZoomScrollViewEx。專案下載網址同樣是 https://github.com/Frank-Zhu/PullZoomView 。
與 ListView 版本最大的差異在於:ScrollView 版本是在 Java 程式碼中動態裝載各個子 View,而不是全部寫死在版面檔裡。
private void loadViewForPullToZoomScrollView(PullToZoomScrollViewEx scrollView) { View headView = LayoutInflater.from(this).inflate(R.layout.head_view, null); View zoomView = LayoutInflater.from(this).inflate(R.layout.head_zoom_view, null); View contentView = LayoutInflater.from(this).inflate(R.layout.content_view, null); scrollView.setHeaderView(headView); scrollView.setZoomView(zoomView); scrollView.setScrollContentView(contentView); }
(1)各種效果都能透過 boolean 開關控制。PullZoomView 的頭部與縮放行為可以隨心開啟或關閉,關鍵方法如下:
setParallax(boolean b):傳入 true 才有視差效果,false 則沒有。
setHideHeader(boolean b):true 會隱藏自訂的 head view,false 則顯示。
setZoomEnabled(boolean b):true 支援縮放,false 則關閉縮放。
而系統的預設值為:
setParallax(true);setHideHeader(false);setZoomEnabled(true);
(2)子 View 的取得方式不同。在 PullZoomView 內部,若要抓取巢狀的子 View,必須透過 getPullRootView().findViewById(R.id.xxxx) 這種寫法,而不能直接用 findViewById(),否則會找不到元件。
1 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" 2 xmlns:tools="https://schemas.android.com/tools" 3 xmlns:custom="https://schemas.android.com/apk/res-auto" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" > 6 7 <com.ecloud.pulltozoomview.PullToZoomScrollViewEx 8 android:id="@+id/scroll_view" 9 android:layout_width="match_parent" 10 android:layout_height="match_parent" /> 11 12 </RelativeLayout>
1 package com.zzw.testpullzoomview_scrollview; 2 3 import com.ecloud.pulltozoomview.PullToZoomScrollViewEx; 4 import android.app.Activity; 5 import android.os.Bundle; 6 import android.util.DisplayMetrics; 7 import android.util.Log; 8 import android.view.LayoutInflater; 9 import android.view.Menu; 10 import android.view.MenuItem; 11 import android.view.View; 12 import android.widget.LinearLayout; 13 14 public class MainActivity extends Activity { 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 20 // 注意初始化順序,不要弄亂,否則拋出運行時空指針 21 PullToZoomScrollViewEx scrollView = (PullToZoomScrollViewEx) findViewById(R.id.scroll_view); 22 loadViewForPullToZoomScrollView(scrollView); 23 24 scrollView.getPullRootView().findViewById(R.id.tv_test1).setOnClickListener(new View.OnClickListener() { 25 @Override 26 public void onClick(View v) { 27 Log.d("PullToZoomScrollViewEx1", "onClick"); 28 } 29 }); 30 31 scrollView.getPullRootView().findViewById(R.id.tv_test2).setOnClickListener(new View.OnClickListener() { 32 @Override 33 public void onClick(View v) { 34 Log.e("PullToZoomScrollViewEx2", "onClick"); 35 } 36 }); 37 38 scrollView.getPullRootView().findViewById(R.id.tv_test3).setOnClickListener(new View.OnClickListener() { 39 @Override 40 public void onClick(View v) { 41 Log.d("PullToZoomScrollViewEx3", "onClick"); 42 } 43 }); 44 45 setPullToZoomViewLayoutParams(scrollView); 46 } 47 48 private void loadViewForPullToZoomScrollView(PullToZoomScrollViewEx scrollView) { 49 View headView = LayoutInflater.from(this).inflate(R.layout.head_view, null); 50 View zoomView = LayoutInflater.from(this).inflate(R.layout.head_zoom_view, null); 51 View contentView = LayoutInflater.from(this).inflate(R.layout.content_view, null); 52 scrollView.setHeaderView(headView); 53 scrollView.setZoomView(zoomView); 54 scrollView.setScrollContentView(contentView); 55 } 56 57 // 設置頭部的View的寬高。 58 private void setPullToZoomViewLayoutParams(PullToZoomScrollViewEx scrollView) { 59 DisplayMetrics localDisplayMetrics = new DisplayMetrics(); 60 getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics); 61 int mScreenHeight = localDisplayMetrics.heightPixels; 62 int mScreenWidth = localDisplayMetrics.widthPixels; 63 LinearLayout.LayoutParams localObject = new LinearLayout.LayoutParams(mScreenWidth, 64 (int) (9.0F * (mScreenWidth / 16.0F))); 65 scrollView.setHeaderLayoutParams(localObject); 66 } 67 }
1 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" 2 android:id="@+id/layout_view" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:layout_gravity="bottom" 6 android:gravity="bottom"> 7 8 <ImageView 9 android:id="@+id/iv_user_head" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:layout_centerInParent="true" 13 android:src="@drawable/ic_launcher" /> 14 15 <TextView 16 android:id="@+id/tv_user_name" 17 android:textSize="12sp" 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:layout_below="@id/iv_user_head" 21 android:layout_centerHorizontal="true" 22 android:text="新浪微博" 23 android:textColor="#ffffff" /> 24 25 <LinearLayout 26 android:id="@+id/ll_action_button" 27 android:layout_width="match_parent" 28 android:layout_height="wrap_content" 29 android:background="#66000000" 30 android:layout_alignParentBottom="true" 31 android:padding="10dip"> 32 33 <TextView 34 android:id="@+id/tv_register" 35 android:layout_width="match_parent" 36 android:layout_height="wrap_content" 37 android:text="注冊" 38 android:layout_weight="1" 39 android:textSize="12sp" 40 android:gravity="center" 41 android:layout_gravity="center" 42 android:textColor="#ffffff" /> 43 44 <TextView 45 android:id="@+id/tv_login" 46 android:layout_width="match_parent" 47 android:layout_height="wrap_content" 48 android:text="登錄" 49 android:layout_weight="1" 50 android:textSize="12sp" 51 android:gravity="center" 52 android:layout_gravity="center" 53 android:textColor="#ffffff" /> 54 </LinearLayout> 55 </RelativeLayout>
這裡就是放一張可供縮放拉伸的圖片。
1 <?xml version="1.0" encoding="utf-8"?> 2 <ImageView xmlns:android="https://schemas.android.com/apk/res/android" 3 android:id="@+id/imageView" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:layout_gravity="center_horizontal" 7 android:scaleType="centerCrop" 8 android:src="@drawable/a" />
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="#ffffff" 6 android:orientation="vertical" > 7 8 <TextView 9 android:id="@+id/tv_test1" 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:gravity="center_vertical" 13 android:padding="20dp" 14 android:text="test1" 15 android:textSize="20sp" /> 16 17 <TextView 18 android:id="@+id/tv_test2" 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 android:gravity="center_vertical" 22 android:padding="20dp" 23 android:text="test2" 24 android:textSize="20sp" /> 25 26 <TextView 27 android:id="@+id/tv_test3" 28 android:layout_width="match_parent" 29 android:layout_height="wrap_content" 30 android:gravity="center_vertical" 31 android:padding="20dp" 32 android:text="test3" 33 android:textSize="20sp" /> 34 35 <TextView 36 android:layout_width="match_parent" 37 android:layout_height="wrap_content" 38 android:gravity="center_vertical" 39 android:padding="20dp" 40 android:text="test4" 41 android:textSize="20sp" /> 42 43 <TextView 44 android:layout_width="match_parent" 45 android:layout_height="wrap_content" 46 android:gravity="center_vertical" 47 android:padding="20dp" 48 android:text="test5" 49 android:textSize="20sp" /> 50 51 <TextView 52 android:layout_width="match_parent" 53 android:layout_height="wrap_content" 54 android:background="#eeeeee" /> 55 56 <TextView 57 android:layout_width="match_parent" 58 android:layout_height="wrap_content" 59 android:gravity="center_vertical" 60 android:padding="20dp" 61 android:text="test1" 62 android:textSize="20sp" /> 63 64 <TextView 65 android:layout_width="match_parent" 66 android:layout_height="wrap_content" 67 android:gravity="center_vertical" 68 android:padding="20dp" 69 android:text="test2" 70 android:textSize="20sp" /> 71 72 <TextView 73 android:layout_width="match_parent" 74 android:layout_height="wrap_content" 75 android:gravity="center_vertical" 76 android:padding="20dp" 77 android:text="test3" 78 android:textSize="20sp" /> 79 80 <TextView 81 android:layout_width="match_parent" 82 android:layout_height="wrap_content" 83 android:gravity="center_vertical" 84 android:padding="20dp" 85 android:text="test4" 86 android:textSize="20sp" /> 87 88 <TextView 89 android:layout_width="match_parent" 90 android:layout_height="wrap_content" 91 android:gravity="center_vertical" 92 android:padding="20dp" 93 android:text="test5" 94 android:textSize="20sp" /> 95 96 </LinearLayout>
實際開發時,如果你打算用 ScrollView 包住自己專案裡的內容,那麼 content_view.xml 就是承載其他 View 的「父」版面,真正要發揮的地方都在這裡展開。
Intent 隱式調用:輕鬆打開網頁與撥號盤
Intent 隱式調用:輕鬆打開網頁與撥號盤在上一節中,我們一起學習了 Intent 的顯式調用。這一節則要探討另一種更靈活的方式——隱式調用。掌握隱式 Intent,就
自訂 Toast 樣式:讓提示訊息更好看也不重複彈出
自訂 Toast 樣式:讓提示訊息更好看也不重複彈出 系統預設的 Toast 樣子實在朴素,而且連續觸發時容易疊在一起。想換上自訂版面,其實只要四個步驟: 建立一個 T
Android 網路開發:實作一個網頁原始碼查看器(HttpURLConnection + Handler)
Android 網路開發:實作一個網頁原始碼查看器(HttpURLConnection + Handler) 本文示範如何打造一個簡易的網頁原始碼查看器:使用者輸入網址後
白天/夜間模式流暢切換:一套用屬性替換做到的三行接入方案
這個庫想解決的問題很單純:讓 App 在日間與夜間主題之間切換時,畫面上的背景色、文字色能即時重繪,而不是重啟 Activity 才生效。核心思路是把顏色抽成自訂屬性(a