2012-06-05 58 views
0

我有一個畫廊。每張幻燈片裏面都有一個按鈕。如何禁用按下圖庫幻燈片時按下「按下」按鈕?

我有一個自定義佈局,我在爲幻燈片充氣。

附加點擊監聽器按鈕,它工作得很好,畫廊還正常滾動。

但是,當我按下按鈕的外廊,按鈕也被壓並變成藍色。這不會觸發按鈕的onClickListener。但它使按鈕看起來像被按下,我不想那樣。

試過畫廊的所有偵聽器設置爲空的實現,但沒有效果...

在此先感謝。

編輯:附加一個空的點擊收聽到自定義佈局,現在按鈕不會變成藍色了,但畫廊不滾動,因爲事件被消耗。將會看到可以做些什麼...

回答

0

找到了解決方法。這太可怕了,但它很有用。

首先,畫廊的代碼負責我的問題的一部分是這樣的:

public boolean onDown(MotionEvent e) { 

    // Kill any existing fling/scroll 
    mFlingRunnable.stop(false); 

    // Get the item's view that was touched 
    mDownTouchPosition = pointToPosition((int) e.getX(), (int) e.getY()); 

    if (mDownTouchPosition >= 0) { 
     mDownTouchView = getChildAt(mDownTouchPosition - mFirstPosition); 
     mDownTouchView.setPressed(true); 
    } 

    // Reset the multiple-scroll tracking state 
    mIsFirstScroll = true; 

    // Must return true to get matching events for this down event. 
    return true; 
} 

更準確地說這條線:

mDownTouchView.setPressed(true); 

這裏按我的幻燈片的版式和默認LinearLayout的setPressed行爲會將其分派給所有兒童,以便按下所有兒童。

首先我試圖使畫廊的一個子類,並覆蓋onDown。如果我只是返回了虛假而沒有其他的東西,它就可以工作,但幻燈片在接觸時會跳到下一張幻燈片中。那是因爲這一行:

mFlingRunnable.stop(false); 

哪一個沒執行。由於該變量是私有的,並且與Gallery類中的所有其他變量相關,所以我沒有找到從子類中使用它的方法。我試圖複製所有的圖庫代碼,但它也沒有工作,因爲它使用了很多隻有包訪問的東西......等等。

所以我創建了LinearLayout的子類,它覆蓋onSetPressed:

public class LinearLayoutOnSetPressedDoNothing extends LinearLayout { 

public LinearLayoutOnSetPressedDoNothing(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

@Override 
public void setPressed(boolean pressed) { 
} 
} 

和使用,在我的佈局,而不是的LinearLayout:

<?xml version="1.0" encoding="utf-8"?> 
<com.test.LinearLayoutOnPressDoNothing 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
> 

    <!-- content --> 
</com.test.LinearLayoutOnPressDoNothing> 

和好,這個工程。