2011-11-25 18 views
2

我有一個奇怪的問題,在我相當複雜的視圖佈局。 (我會盡量簡化它在我的解釋中)點擊事件ImageButton沒有立即調度

基本上我有一個ListView,其中每個項目包括一個TextViewImageButton。我能夠單擊列表項(在textview上)或按鈕(我將ImageButton設置爲不可聚焦,否則它將不起作用)

現在它似乎工作正常,直到我打開另一個窗口並返回到列表視圖。 從那時起,我可以單擊ImageButton而不發生任何事情(甚至在點擊過程中不會發生背景變化)。但是當我再次點擊TextView時,所有來自ImageButton的點擊事件都會立即分派。

這是爲什麼?

編輯:

列表項:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="0px" 
    android:minHeight="40dp" 
    android:orientation="horizontal" 
    android:paddingLeft="2px" 
    android:paddingRight="2px" 
    > 

    <TextView 
     android:id="@+id/text1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Text" 
     android:textSize="19dp" 
     android:paddingTop="4px" 
     android:paddingBottom="4px" 
     android:layout_gravity="center_vertical"/> 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/open_subtree_layout" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_margin="0px" 
     android:orientation="horizontal" 
     android:padding="0px"> 

     <View 
      android:layout_width="1px" 
      android:layout_height="match_parent" 
      android:background="@drawable/separator_line" /> 

     <com.treeviewer.leveldisplay.DontPressWithParentImageButton 
      android:id="@+id/btn_right" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_gravity="center_vertical" 
      android:background="@drawable/list_selector_background" 
      android:focusable="false" 
      android:focusableInTouchMode="false" 
      android:padding="10dp" 
      android:src="@drawable/arrow_right" /> 
    </LinearLayout> 

</LinearLayout> 

這就是它是如何膨脹:

[...] 
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
mView = inflater.inflate(R.layout.tree_row, null, false); 

TextView textView = (TextView)mView.findViewById(R.id.text1); 
LinearLayout nextNodeButtonContainer = (LinearLayout)mView.findViewById(R.id.open_subtree_layout); 

if(childCount >= 0) { 
    titleBuilder.append(" (" + childCount + ")"); 
    nextNodeButtonContainer.setVisibility(View.VISIBLE); 
    View button = nextNodeButtonContainer.findViewById(R.id.btn_right); 
    button.setFocusable(false); 
    button.setFocusableInTouchMode(false); 
    //button.setClickable(true); 
    button.setOnClickListener(clickListener); 

    button.setTag(tagValue); 
} else { 
    nextNodeButtonContainer.setVisibility(View.GONE); 
} 

textView.setText(titleBuilder); 

讓我知道,如果你需要更多的代碼。

+1

通過在這裏發佈一些代碼來幫助我們... – Optimus

回答

0

好吧,我終於解決了這個問題。

不幸的是,在我的問題我沒有提供必要的信息來解決這個問題,因爲這個問題是什麼地方我沒想到吧:

我有一個ListAdapter在getView方法是這樣的:

public View getView(int position, View convertView, ViewGroup parent) { 
    return mNodes.get(position).getView(mNodeArrowClickListener, position); 
} 

和節點(TreeLevelElement或多個)的方法getView看起來像:

public class TreeLevelElement { 
    private final Context mContext; 
    private View mView = null; 

    //[...] other methods 

    View getView(OnClickListener clickListener, final int tagValue) { 
     if(mView == null) { 
      //[...] produce a new View from XML 
     } 
     return mView; 
    } 
} 

的問題是,我將視圖存儲在我的元素中,所以我猜想與android策略有衝突,可以重新使用舊視圖來獲取新項目。 我不知道到底發生了什麼,但現在我刪除mView並每次創建一個新的,它的工作原理。

我也會改變它重新使用convertView

相關問題