Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

用 ListView 項目點擊展開,實現類似滑動刪除的交互

編輯:關於 Android 程式設計

最近做專案做得有點頭痛。原本想直接搬 ListView 的註解版(Annotation)來用,那是一個可以做出類似 Android 通知欄「滑動刪除」效果的開源實作,據說是某位高手獨力完成的,這裡先向他致敬一下。

說實話,它的原理並不難理解:偵測使用者的滑動距離與速度,再依此判斷該做什麼操作。想看懂思路,直接去玩一下 Google Keep 兩種列表模式下的滑動刪除就很清楚了。

為什麼最後放棄滑動手勢

不幸的是,我在版面配置裡同時用到了 Fragment 和 ViewPager,結果產生了手勢衝突。為此我改寫了 library 裡的手勢偵測方法,最後勉強改出一個能跑的版本——但是!效果實在慘不忍睹,只好作罷。

反覆權衡之後,我決定改用「點擊 ListView 項目展開」的方式來達成我想要的效果。

動手前先補三個觀念

一、getLayoutParams() 與 setLayoutParams()

先用 getLayoutParams() 取得指定元件的 LayoutParams,接著修改它的屬性,最後再寫回去:

LinearLayout.LayoutParams lp = (RelativeLayout.LayoutParams) deletButton.getLayoutParams();
lp.width = btnWidth;
lp.leftMargin = 10;
deletButton.setLayoutParams(lp);

二、getMeasuredHeight() 與 getHeight() 的差別

getMeasuredHeight() 回傳的是原始測量高度,與螢幕無關;getHeight() 回傳的是元件在螢幕上實際顯示的高度。當內容可以被螢幕完整包住時,兩者數值相等;只有當 View 超出螢幕之後,差異才會顯現出來——此時 getMeasuredHeight() 等於 getHeight() 加上螢幕外沒被顯示的那段高度。

三、MeasureSpec 的用法

MeasureSpec 常出現在自訂 View 中,因為我們往往需要覆寫 onMeasure(int widthMeasureSpec, int heightMeasureSpec),由它來決定自訂元件在螢幕上的尺寸。這兩個傳入參數並不是單純的數字,而是把「模式」與「尺寸」打包在一起的複合值,必須分別取出:

int mode = MeasureSpec.getMode(widthMeasureSpec);
int size = MeasureSpec.getSize(widthMeasureSpec);

mode 共有三種:

  • EXACTLY:精確尺寸。當 layout_width/layout_height 寫成具體數值(例如 50dip)或 FILL_PARENT 時,元件大小已經確定。
  • AT_MOST:最大尺寸。當屬性設為 WRAP_CONTENT 時,元件會隨子元件或內容伸縮,只要不超過父元件允許的上限即可,此時 size 就是父元件給出的最大值。
  • UNSPECIFIED:未指定尺寸。這種情況較少見,通常是父元件為 AdapterView 時透過 measure 傳入的。

需要自行設定時,可以呼叫 MeasureSpec.makeMeasureSpec(int size, int mode)。

先看一下 demo 的執行結果,讀者可以自行再擴充:

效果還算可以。圖示畫得有點呆萌,還請別見笑(自己畫的,可憐的程式員一隻)。

展開動畫的實作

先上程式碼:

public class ViewExpandAnimation extends Animation {
    private View mAnimationView = null;
    private LayoutParams mViewLayoutParams = null;
    private int mStart = 0;
    private int mEnd = 0;

    public ViewExpandAnimation(View view) {
        animationSettings(view, 500);
    }

    public ViewExpandAnimation(View view, int duration) {
        animationSettings(view, duration);
    }

    private void animationSettings(View view, int duration) {
        setDuration(duration);
        mAnimationView = view;
        mViewLayoutParams = (LayoutParams) view.getLayoutParams();
        mStart = mViewLayoutParams.bottomMargin;
        mEnd = (mStart == 0 ? (0 - view.getHeight()) : 0);
        view.setVisibility(View.VISIBLE);
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);
        if (interpolatedTime < 1.0f) {
            mViewLayoutParams.bottomMargin = mStart + (int) ((mEnd - mStart) * interpolatedTime);
            mAnimationView.requestLayout();
        } else {
            mViewLayoutParams.bottomMargin = mEnd;
            mAnimationView.requestLayout();
            if (mEnd != 0) {
                mAnimationView.setVisibility(View.GONE);
            }
        }
    }
}

動效本身的程式碼並不多。setDuration(duration) 用來設定動畫長度;接著操作 RelativeLayout.bottomMargin,官方文件對它的說明是「子元件的下邊界,單位為像素」;mAnimationView.requestLayout() 內部會呼叫 layout 流程,讓版面重新配置一次。

Adapter 與項目版面配置

Adapter 的覆寫相對單純,這裡就不貼全部程式碼,只提幾個要注意的地方。首先在 getView(...) 中載入自訂的項目版面:

convertView = myInflater.inflate(R.layout.expand_item, null);

對應的版面配置如下:

<?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="wrap_content"
    android:orientation="vertical"
    android:padding="5dip" >

    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <TextView
            android:id="@+id/tvName"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:gravity="center|left"
            android:text="name"
            android:textColor="#000000"
            android:textSize="16sp" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff" >
        <Button android:id="@+id/btnOpen" ... />
        <Button android:id="@+id/btnView" ... />
        <Button android:id="@+id/btnWarning" ... />
    </RelativeLayout>
</LinearLayout>

我讓第二個 RelativeLayout(footer)在預設狀態下是隱藏的。在 BaseAdapter 中的設定如下:

RelativeLayout footer = (RelativeLayout) convertView.findViewById(R.id.footer);
int widthSpec = MeasureSpec.makeMeasureSpec((int) (mLcdWidth - 10 * mDensity), MeasureSpec.EXACTLY);
footer.measure(widthSpec, 0);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) footer.getLayoutParams();
params.bottomMargin = -footer.getMeasuredHeight();
footer.setVisibility(View.GONE);

最後在 setOnItemClickListener 裡觸發動畫:點擊時展開,再點一次則收合,並且同一時間只允許一個項目展開(我認為使用者不太可能同時去收合兩個項目):

if (mLastTouchTag != null) {
    View temp = arg0.findViewWithTag(mLastTouchTag);
    if (temp != null) {
        View footTemp = temp.findViewById(R.id.footer);
        if (footTemp != null && footTemp.getVisibility() != View.GONE) {
            footTemp.startAnimation(new ViewExpandAnimation(footTemp));
        }
    }
}
mLastTouchTag = (ViewHolder) v.getTag();

View footer = v.findViewById(R.id.footer);
footer.startAnimation(new ViewExpandAnimation(footer));

就先寫到這裡,有問題歡迎留言。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved