2012-06-06 44 views
2

我有一個默認的ListView,我已經添加了我的自定義視圖的列表項,但這些視圖中的按鈕大多數時間不可點擊。當按鈕收到點擊事件時,我輸出一個Log.v,但我必須點擊按鈕幾十次纔會註冊點擊。按鈕裏面的Android ListView不響應點擊

與他有關的另一個問題是,當我按下按鈕時,我想要一個動畫發生,揭示從它下面滑出的菜單。目前,我已經嘗試了幾種不同的方法,比如爲視圖製作自定義類,而不是使用佈局充填器爲視圖獲取relativeLayout對象,但沒有任何工作正常。我甚至嘗試過使用listview.getAdapter().notifyDataSetChanged();,但是隻有當我想要動畫時才具有用於擴展視圖的彈出窗口。

我到處搜索,似乎唯一可能的解決方案是重寫我自己的自定義列表視圖或使用scrollview使用linearlayout。後者似乎更容易,但我不認爲它幾乎與列表視圖一樣優化。

任何建議,將不勝感激,如果你需要看到一些代碼,請讓我知道...

謝謝!

UPDATE:

getView()包含此:

Holder hold; 
    convertView = friends.get(position); 
    hold = new Holder(); 
    hold.pos = position; 
    convertView.setTag(hold); 
    return convertView; 

基本上我傳遞一個ArrayList<RelativeLayout>,此刻,到了適配器,這樣我不必每次都創建一個新的觀點和使動畫將保持動畫,當我向下滾動時...

在此活動OnCreate()內我設置ArrayList<RelativeLayout>與此下一個代碼,但這只是暫時的,因爲我打算使用ano療法方法後,像一個異步任務或東西,所以,這個觀點包含了一些數據...

RelativeLayout temp; 
    for(int i=0; i<30; i++){ 
     temp = (RelativeLayout) inflater.inflate(R.layout.list_item, null); 

     final LinearLayout extraListItemInfo = (LinearLayout) temp.findViewById(R.id.extraListItemInfo); 

     Button infoBtn = (Button) temp.findViewById(R.id.infoBtn); 
     infoBtn.setOnClickListener(new OnClickListener(){ 
      @Override 
      public void onClick(View v) { 
       Log.v("ListItem", "Button has been clicked... "); 
       extraListItemInfo .setVisibility(View.VISIBLE); 

       ExpandAnimation closeAnim = new ExpandAnimation(extraListItemInfo , animHeight); 
       closeAnim.setDuration(750); 
       closeAnim.setFillAfter(true); 

       if(extraListItemInfo .getTag() == null || !extraListItemInfo .getTag().equals("expanded")){ 
        extraListItemInfo .getLayoutParams().height = 0; 
        friendInfoList.startAnimation(closeAnim.expand()); 
        extraListItemInfo .setTag("expanded"); 
       }else if(extraListItemInfo .getTag().equals("expanded")){ 
        extraListItemInfo .startAnimation(closeAnim.collapse()); 
        extraListItemInfo .setTag("closed"); 
       } 
       //((BaseAdapter) listview.getAdapter()).notifyDataSetChanged(); i tried it here once but then left it 
    //as the only action inside the listview's onitemclick() 
      } 
     }); 

     listItems.add(temp); 
    } 

這是我使用的列表項:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/darkgrey" 
    android:paddingBottom="5dp" > 

    <LinearLayout 
     android:id="@+id/extraListItemInfo " 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/listItemInfo" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="-10dp" 
     android:background="@color/grey" 
     android:orientation="vertical" 
     android:visibility="gone" > 


     <RelativeLayout 
      android:id="@+id/RelativeLayout04" 
      android:layout_width="match_parent" 
      android:layout_height="@dimen/activity_list_height" 
      android:layout_marginTop="5dp" > 

      <ImageView 
       android:id="@+id/ImageView04" 
       android:layout_width="wrap_content" 
       android:layout_height="25dp" 
       android:layout_margin="5dp" 
       android:src="@drawable/logo_d" /> 

      <TextView 
       android:id="@+id/TextView04" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerVertical="true" 
       android:layout_toRightOf="@+id/ImageView04" 
       android:text="TextView" 
       android:textColor="@color/black" 
       android:textSize="17dp" /> 
     </RelativeLayout> 

     <RelativeLayout 
      android:id="@+id/RelativeLayout03" 
      android:layout_width="match_parent" 
      android:layout_height="@dimen/activity_list_height" > 

      <ImageView 
       android:id="@+id/ImageView03" 
       android:layout_width="wrap_content" 
       android:layout_height="25dp" 
       android:layout_margin="5dp" 
       android:src="@drawable/logo_d" /> 

      <TextView 
       android:id="@+id/TextView03" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerVertical="true" 
       android:layout_toRightOf="@+id/ImageView03" 
       android:text="TextView" 
       android:textColor="@color/black" 
       android:textSize="17dp" /> 
     </RelativeLayout> 

     <RelativeLayout 
      android:id="@+id/RelativeLayout02" 
      android:layout_width="match_parent" 
      android:layout_height="@dimen/activity_list_height" > 

      <ImageView 
       android:id="@+id/ImageView02" 
       android:layout_width="wrap_content" 
       android:layout_height="25dp" 
       android:layout_margin="5dp" 
       android:src="@drawable/logo_d" /> 

      <TextView 
       android:id="@+id/TextView02" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerVertical="true" 
       android:layout_toRightOf="@+id/ImageView02" 
       android:text="TextView" 
       android:textColor="@color/black" 
       android:textSize="17dp" /> 
     </RelativeLayout> 

     <RelativeLayout 
      android:id="@+id/relativeLayout1" 
      android:layout_width="match_parent" 
      android:layout_height="@dimen/activity_list_height" > 

      <ImageView 
       android:id="@+id/imageView1" 
       android:layout_width="wrap_content" 
       android:layout_height="25dp" 
       android:layout_margin="5dp" 
       android:src="@drawable/logo_d" /> 

      <TextView 
       android:id="@+id/textView1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerVertical="true" 
       android:layout_toRightOf="@id/imageView1" 
       android:text="TextView" 
       android:textColor="@color/black" 
       android:textSize="17dp" /> 
     </RelativeLayout> 

     <RelativeLayout 
      android:id="@+id/RelativeLayout01" 
      android:layout_width="match_parent" 
      android:layout_height="@dimen/activity_list_height"> 

      <ImageView 
       android:id="@+id/ImageView01" 
       android:layout_width="wrap_content" 
       android:layout_height="25dp" 
       android:layout_margin="5dp" 
       android:src="@drawable/logo_d" /> 

      <TextView 
       android:id="@+id/TextView01" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerVertical="true" 
       android:layout_toRightOf="@+id/ImageView01" 
       android:text="TextView" 
       android:textColor="@color/black" 
       android:textSize="17dp" /> 
     </RelativeLayout> 
    </LinearLayout> 
     <RelativeLayout 
     android:id="@+id/listItemInfo" 
     android:layout_width="wrap_content" 
     android:layout_height="95dp" 
     android:background="@drawable/friend_cell_background2x" 
     android:clickable="true" > 


     <RelativeLayout 
      android:id="@+id/leftLayout" 
      android:layout_width="90dp" 
      android:layout_height="match_parent" > 
      <ImageView 
       android:id="@+id/imgCompany" 
       android:layout_width="60dp" 
       android:layout_height="50dp" 
       android:layout_centerHorizontal="true" 
       android:layout_marginLeft="10dp" 
       android:layout_marginTop="10dp" 
       android:scaleType="centerInside" 
       android:src="@drawable/user2x" /> 
      <ImageView 
       android:id="@+id/imageView2" 
       android:layout_width="40dp" 
       android:layout_height="40dp" 
       android:layout_alignParentBottom="true" 
       android:layout_centerHorizontal="true" 
       android:scaleType="fitXY" 
       android:src="@drawable/online_indicator2s" /> 

     </RelativeLayout> 


     <LinearLayout 
      android:id="@+id/linearLayout1" 
      android:layout_width="wrap_content" 
      android:layout_height="50dp" 
      android:layout_marginTop="5dp" 
      android:layout_toRightOf="@+id/leftLayout" 
      android:background="@android:color/transparent" 
      android:gravity="left|center" 
      android:orientation="vertical" 
      android:paddingLeft="5dp" > 


      <TextView 
       android:id="@+id/lblCompanyName" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Contact Name" 
       android:textColor="@color/white" 
       android:textSize="18dp" 
       android:textStyle="bold" > 

      </TextView> 


      <TextView 
       android:id="@+id/lblReawrdDesc" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Last Played Offer" 
       android:textColor="@color/white" 
       android:textSize="17dp" > 

      </TextView> 
     </LinearLayout> 

     <ImageView 
      android:id="@+id/imageView4" 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentTop="true" 
      android:layout_marginRight="5dp" 
      android:src="@drawable/facebook_btn2x" /> 
     <Button 
      android:id="@+id/infoBtn" 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:layout_alignParentBottom="true" 
      android:layout_alignRight="@+id/imageView4" 
      android:background="@drawable/info_btn2x" 
      android:clickable="true" /> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_marginBottom="13dp" 
      android:layout_toLeftOf="@+id/infoBtn" 
      android:text="Follows 30+" 
      android:textColor="@color/white" 
      android:textSize="11dp" /> 

     <Button 
      android:id="@+id/button1" 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="75dp" 
      android:layout_height="20dp" 
      android:layout_alignParentBottom="true" 
      android:layout_marginBottom="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_toLeftOf="@+id/textView2" 
      android:background="@drawable/fan_btn2x" 
      android:text="Fans 30+" 
      android:textColor="@color/white" 
      android:textSize="11dp" /> 


     <ImageView 
      android:id="@+id/imageView5" 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:layout_alignParentTop="true" 
      android:layout_toLeftOf="@+id/imageView4" 
      android:src="@drawable/google_btn2x" /> 

    </RelativeLayout> 

</RelativeLayout> 

對不起任何佈局的問題,可能使事情變得難以閱讀...但我希望這有助於你們理解我的問題...謝謝

更新2:

所有這些答案都在一些WA很有幫助y,但我認爲我必須首先解決的主要問題是爲什麼按鈕不會收到點擊事件,直到我第一次從該listItem滾動,然後再次回到它,然後再次單擊按鈕...如果有人可以幫助找到該解決方案我認爲這一切會更容易解決...... 謝謝...

截圖的要求,但要記住,這個鏡頭是拍攝的三星Galaxy Tab 10.1,並因我在這個更大的屏幕上使用相同的佈局,它看起來與我通常測試的手機有很大的不同(摩托羅拉droid x,它根本無法取得屏幕截圖...)

enter image description here

另一個更新: 我設法獲得點擊和動畫通過擴展ArrayAdapter,而不是基礎轉接器的工作很好。可悲的是,我仍然遇到問題,因爲只有列表的下半部分是可點擊的。列表的上半部分仍然像以前一樣表現非常糟糕的點擊事件......關於這次發生的事情的任何想法?謝謝...

+0

請包括相關的代碼,以告訴我們爲什麼點擊監聽器不聽。 – Sam

+0

適配器的getView()是什麼代碼?另外,如果你在膨脹行,發佈xml。 –

+0

在你的xml文件中使用android:focasable = false我可能會工作... – DynamicMind

回答

3

嗯,這是不是一個真正的答案,但這個重寫了幾次後,我設法使其功能正是我想要的方式來解決它。

這一次,我將所有的數據從每個視圖分離和有每個項目是一個自定義的類繼承RelativeLayout,也實現了自己的OnClickListener其具體infoBtn

我的適配器現在僅僅簡單地擴展ArrayAdapter <>和覆蓋此getView()方法:

public View getView(int position, View convertView, ViewGroup parent) { 

    if(convertView != null && (convertView instanceof FriendListItem2)) 
     ((FriendListItem2) convertView).setFriendInfo(friends.get(position)); 
    else 
     convertView = new FriendListItem2(getContext(), friends.get(position)); 

    return convertView; 
} 

最後,在該頁面的主要活動我簡單地設置ListView與我的數據傳遞給適配器。

這比我之前清潔得多,我希望它沒有多次重寫代碼才能正確使用它。希望有人能從中受益,儘管我還不知道爲什麼我以前遇到問題。

感謝以前的所有建議。

+0

恭喜(如果它有效,這是一個答案)! Upvote回來並關閉問題。 – Sam

0

添加android:onClick="onClick"到您的按鈕XML,這將使它調用onClick()方法

+0

點擊事件是去正確的地方,它只是他們arent被調用,直到我滾動從該listItem,滾動回來,然後再次按下按鈕... – Intrivix

2

這是一個複雜的佈局創造的每一行...

無論如何,當您使用您正在創建不同範圍的new關鍵字。簡而言之,您的onClickListener不會而不是請參閱您期望看到的30個不同的extraListItemInfo引用。

嘗試這樣:

@Override 
public void onClick(View v) { 
    LinearLayout extraListItemInfo = v.getParent(); 
    ... 
+0

我認爲這將幫助我,但問題不是我對事件做了什麼,它首先獲得了事件......請參閱更新2以獲得澄清。此外,我試過這個,結果和以前一樣,除了我同意這是更好的方式。 – Intrivix

+0

你可以發佈一個屏幕截圖嗎?你**是**,我錯過了你的問題的根源。我理解你的問題(現在),但是我對佈局有點困惑。 – Sam

+0

有沒有其他想法?我應該放棄我擁有的東西並嘗試重新開始? – Intrivix

0

我有同樣的問題。嘗試從相對佈局中獲取「android:clicable =」true「」。當你這樣做時,活動期望你做setOnClickListener到那個RelativeLayout。它對我很有用

1

請嘗試在放置子視圖的頂層佈局中使用此屬性。

android:descendantFocusability="blocksDescendants" 

它有點棘手,但我建議,如果上面的行不行,請嘗試去除之間的切換可聚焦=真和可聚焦=「假」,從按鈕。它應該工作。

乾杯!