編輯:關於 Android 程式設計
上一篇談的是 PullZoomView 在 ListView 上的用法,這篇接著說明它在 ScrollView 上的對應版本:PullToZoomScrollViewEx。
專案下載:https://github.com/Frank-Zhu/PullZoomView
與 ListView 版本最大的差異在於載入方式:ScrollView 版是在 Java 程式碼中動態掛載各個 View,而不是透過 XML 屬性指定。
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);
}
所有頭部與縮放效果都可以自由啟停,方式就是切換以下方法的參數:
setParallax(true); // true 有視差效果,false 無 setHideHeader(false); // true 隱藏自訂 head view,false 顯示 setZoomEnabled(true); // true 支援縮放,false 不支援
以上三行即為預設值。
PullZoomView 內部把子 View 包了一層,所以不能直接用 findViewById(),必須寫成 getPullRootView().findViewById(R.id.xxx),否則會拿到 null。
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
xmlns:custom="https://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.ecloud.pulltozoomview.PullToZoomScrollViewEx
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
package com.zzw.testpullzoomview_scrollview;
import com.ecloud.pulltozoomview.PullToZoomScrollViewEx;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 注意初始化順序,顛倒會拋出空指標
PullToZoomScrollViewEx scrollView = (PullToZoomScrollViewEx) findViewById(R.id.scroll_view);
loadViewForPullToZoomScrollView(scrollView);
// 子 View 必須透過 getPullRootView() 取得
scrollView.getPullRootView().findViewById(R.id.tv_test).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("PullToZoomScrollViewEx", "onClick");
}
});
setPullToZoomViewLayoutParams(scrollView);
}
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);
}
// 設定頭部 View 的寬高
private void setPullToZoomViewLayoutParams(PullToZoomScrollViewEx scrollView) {
DisplayMetrics localDisplayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics);
int mScreenWidth = localDisplayMetrics.widthPixels;
LinearLayout.LayoutParams localObject =
new LinearLayout.LayoutParams(mScreenWidth, (int) (1.5f * mScreenWidth));
scrollView.setHeaderLayoutParams(localObject);
}
}
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/layout_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:gravity="bottom">
<ImageView
android:id="@+id/iv_user_head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/tv_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/iv_user_head"
android:layout_centerHorizontal="true"
android:text="新浪微博"
android:textColor="#ffffff"
android:textSize="16sp" />
<LinearLayout
android:id="@+id/ll_action_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#33ffffff"
android:padding="5dip">
<TextView
android:id="@+id/tv_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="註冊"
android:textColor="#ffffff"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="登入"
android:textColor="#ffffff"
android:textSize="16sp" />
</LinearLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:scaleType="centerCrop"
android:src="@drawable/a" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="10dp"
android:text="test"
android:textSize="16sp" />
<!-- 重複多個 TextView 以撐出可捲動的高度 -->
</LinearLayout>
如果決定用 ScrollView 包住專案裡既有的子 View,那麼 content_view.xml 就是那些 View 的「父」容器,真正的業務布局要在這裡展開。理解 headView(正常顯示、只會位移)與 zoomView(會動態縮放、墊在底層)這兩者的分工,這個元件其實就不難用了。
自訂 View 樣式外掛化:TypedArray 與 declare-styleable 實戰
寫自訂 View 時,若希望它像系統控制項一樣可以透過 XML 屬性調整外觀,就必須認識 TypedArray。舉例來說,想讓自訂的展開/收合 TextView 能配置展
ExpandableListView 完美嵌套 GridView:三個坑與解法
做專案的時候想在 ExpandableListView 中嵌套一個 GridView,實作過程中踩了不少坑,於是寫篇記錄,順便幫幫和我一樣的新手。 我一直覺得,再多的文字
Android Drawable mutations:為何改一個按鈕透明度會影響另一個?
前言Android 為節省記憶體,可能讓多個控制項共用同一個 Drawable 資源。若只修改其中一個控制項的 alpha,另一個也可能跟著變化。本文說明 Drawabl
Android 事件傳遞機制解析:手把手實作仿下拉刷新
概述 在 Android 中,事件主要包含點按、長按、拖曳、滑動等,其中點按又分單擊與雙擊,還包含單指與多指操作。不論是哪一種,所有事件都可拆解為三個基礎動作:按下(AC