Android自訂群組件系列【5】——進階實踐(1)

來源:互聯網
上載者:User

接下來幾篇文章將對任老師的博文《可下拉的PinnedHeaderExpandableListView的實現》分步驟來詳細實現,來學習一下大神的代碼並記錄一下。

原文出處:http://blog.csdn.net/singwhatiwanna/article/details/25546871

先看一下最終效果:

建立一個activity_main.xml檔案

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"><com.example.testexpandablelistview.ui.StickyLayout    android:id="@+id/sticky_layout"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_marginTop="0dp"    android:orientation="vertical">    <LinearLayout         android:id="@+id/header"        android:layout_width="match_parent"        android:layout_height="100dp"        android:gravity="center"        android:background="#78a524"        android:orientation="vertical">    </LinearLayout>    <LinearLayout         android:id="@+id/content"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical">            </LinearLayout></com.example.testexpandablelistview.ui.StickyLayout></RelativeLayout>
上面的StickyLayout類就是我們自訂的LinearLayout,思路其實很簡單,先擷取StickyLayout中的header和content兩個View,代碼如下:

private void initData(){//使用getIdentifier()方法可以方便的獲各應用程式套件下的指定資源ID。//詳細請看:http://blog.sina.com.cn/s/blog_5da93c8f0100zlrx.htmlint headerId = getResources().getIdentifier("header", "id", getContext().getPackageName());int contentId = getResources().getIdentifier("content", "id", getContext().getPackageName());if(headerId != 0 && contentId != 0){mHeader = findViewById(headerId);mContent = findViewById(contentId);mOriginalHeaderHeight = mHeader.getMeasuredHeight();mHeaderHeight = mOriginalHeaderHeight;//是一個距離,表示滑動的時候,手的移動要大於這個距離才開始行動控制項。mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();Log.d(TAG, "mTouchSlop = " + mTouchSlop);}else{throw new NoSuchElementException("Did your view with \"header\" or \"content\" exist?");}}
再處理螢幕的監聽函數

@Overridepublic boolean onInterceptTouchEvent(MotionEvent event) {int intercepted = 0;int x = (int) event.getX();int y = (int) event.getY();switch (event.getAction()) {case MotionEvent.ACTION_DOWN:mLastXIntercept = x;mLastYIntercept = y;mLastX = x;mLastY = y;intercepted = 0;break;case MotionEvent.ACTION_MOVE:int deltaX = x - mLastXIntercept;int deltaY = y - mLastYIntercept;if(mStatus == STATUS_EXPANDED && deltaY <= -mTouchSlop){intercepted = 1;}else if(mGiveUpTouchEventListener != null){if(mGiveUpTouchEventListener.giveUpTouchEvent(event) && deltaY >= mTouchSlop){intercepted = 1;}}break;case MotionEvent.ACTION_UP:{intercepted = 0;mLastXIntercept = mLastYIntercept = 0;break;}default:break;}Log.d(TAG, "intercepted = " + intercepted);//如果為1則返回true,傳遞給當前的onTouchEvent。如果為0則返回false,傳遞給子控制項return intercepted != 0;}
onInterceptTouchEvent是在ViewGroup裡面定義的,用於攔截手勢事件,每個手勢事件都會先調用onInterceptTouchEvent,如果該方法返回true則攔截到事件,當前的onTouchEvent會觸發,如果返回false則傳遞給子控制項。

@Overridepublic boolean onTouchEvent(MotionEvent event) {int x = (int) event.getX();int y = (int) event.getY();Log.d(TAG, "x=" + x + " y=" + y + " mlastY=" + mLastY);switch (event.getAction()) {case MotionEvent.ACTION_DOWN:break;case MotionEvent.ACTION_MOVE:int deltaX = x - mLastX;int deltaY = y - mLastY;Log.d(TAG, "mHeaderHeight=" + mHeaderHeight + "  deltaY=" + deltaY + "  mlastY=" + mLastY);mHeaderHeight +=deltaY;setHeaderHeight(mHeaderHeight);break;case MotionEvent.ACTION_UP:int destHeight = 0;if(mHeaderHeight <= mOriginalHeaderHeight * 0.5){destHeight = 0;mStatus = STATUS_COLLAPSED;}else{destHeight = mOriginalHeaderHeight;mStatus = STATUS_EXPANDED;}//慢慢滑向終點this.smoothSetHeaderHeight(mHeaderHeight, destHeight, 500);break;default:break;}mLastX = x;mLastY = y;return true;}
滑動的時候將事件傳遞給onTouchEvent

滑動事件中setHeaderHeight改變了上面的header部分的高度,當抬起時會判斷是否改變了原始高度的一般,再慢慢滑向終點。

/* * 改變header的高度 */private void setHeaderHeight(int height) {if(height < 0){height = 0;} else if (height > mOriginalHeaderHeight) {height = mOriginalHeaderHeight;}if(mHeaderHeight != height || true){mHeaderHeight = height;mHeader.getLayoutParams().height = mHeaderHeight;mHeader.requestLayout();}}
public void smoothSetHeaderHeight(final int from, final int to, long duration) {final int frameCount = (int) (duration / 1000f * 30) + 1;final float partation = (to - from) / (float) frameCount;new Thread("Thread#smoothSetHeaderHeight") {public void ruan(){for(int i = 0; i < frameCount; i++) {final int height;if(i == frameCount - 1){height = to;}else{height = (int)(from + partation * i);}post(new Runnable() {@Overridepublic void run() {setHeaderHeight(height);}});try {sleep(10);} catch (InterruptedException e) {e.printStackTrace();}}}}.start();}
上面的View.post(Runnable)方法的作用是將Runnable對象添加到UI線程中運行,從而改變header部分的高度。

StickyLayout類的完整代碼如下:

package com.example.testexpandablelistview.ui;import java.util.NoSuchElementException;import android.annotation.TargetApi;import android.content.Context;import android.os.Build;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.ViewConfiguration;import android.widget.LinearLayout;/** * 自訂LinearLayout * @author

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.