2012-01-24 43 views
1

我在有兩個組的Activity中有ExpandableListView。每個組都有一個自定義的View。我可以點擊每個組中的按鈕,它可以工作。如果我點擊一個組的自定義視圖中的文本字段,則會顯示軟鍵盤。但是一旦我解僱它,任何一組都不會註冊點擊。鍵盤解散後,ExpanableListView失去焦點。一旦對話被解散,我怎樣才能將焦點重新放在列表視圖上?如果我摺疊並擴展組,它將被重置。我嘗試了各種聽衆無濟於事。ExpandableListView中的自定義視圖不會獲得點擊事件

我的課程和佈局稍微複雜一點,但我把它剝離到最低限度,這是我的代碼。

package com.test; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.*; 
import android.widget.*; 

public class TestOnClickActivity extends Activity 
{ 
    private static final String cLogTag = "TestOnClick"; 

    private ExpadableAdapter iExpandableListAdapter; 
    private ExpandableListView iExpandableList; 
    private View[] iSearchViews; 


    private class ExpadableAdapter extends BaseExpandableListAdapter 
    { 
     private String[] iGroups = 
     { 
      "Search By Device", 
      "Search By Date", 
     }; 

     @Override 
     public Object getChild(int theGroupPosition, int theChildPosition) 
     { 
      return "NA"; 
     } 

     @Override 
     public long getChildId(int theGroupPosition, int theChildPosition) 
     { 
      return theGroupPosition; 
     } 

     @Override 
     public int getChildrenCount(int theGroupPosition) 
     { 
      return 1; 
     } 

     @Override 
     public View getChildView(int theGroupPosition, 
           int theChildPosition, 
           boolean isLastChild, 
           View theConvertView, 
           ViewGroup theParent) 
     { 
      return iSearchViews[theGroupPosition]; 
     } 

     @Override 
     public Object getGroup(int theGroupPosition) 
     { 
      return iGroups[theGroupPosition]; 
     } 

     @Override 
     public int getGroupCount() 
     { 
      return iGroups.length; 
     } 

     @Override 
     public long getGroupId(int theGroupPosition) 
     { 
      return theGroupPosition; 
     } 

     @Override 
     public View getGroupView(int theGroupPosition, 
           boolean theIsExpanded, 
           View theConvertView, 
           ViewGroup theParent) 
     { 
      if (theConvertView == null) { 
       Context theContext = TestOnClickActivity.this; 
       TextView theTV = new TextView(theContext, 
               null, 
               android.R.attr.textAppearanceMedium); 
       theTV.setText(iGroups[theGroupPosition]); 
       return theTV; 

      } else { 
       return theConvertView; 
      } 
     } 

     @Override 
     public boolean hasStableIds() 
     { 
      return true; 
     } 

     @Override 
     public boolean isChildSelectable(int theGroupPosition, 
             int theChildPosition) 
     { 
      return true; 
     } 
    } 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle theSavedState) 
    { 
     super.onCreate(theSavedState); 
     setContentView(R.layout.main); 
     iExpandableListAdapter = new ExpadableAdapter(); 
     iExpandableList = 
       (ExpandableListView) findViewById(R.id.searchOptionsListView); 
     iExpandableList.setAdapter(iExpandableListAdapter); 
     iExpandableList.setGroupIndicator(null); 
     createSearchViews(); 
    } 

    private void createSearchViews() 
    { 
     iSearchViews = new View[2]; 
     LinearLayout theRowView; 

     // Create the Search By Device View 
     LayoutInflater theInflator = (LayoutInflater) getSystemService(
             Context.LAYOUT_INFLATER_SERVICE); 
     theRowView = new LinearLayout(this); 
     theInflator.inflate(R.layout.search1, theRowView, true); 
     iSearchViews[0] = theRowView; 

     // Create the Search By Date View 
     theRowView = new LinearLayout(this); 
     theInflator.inflate(R.layout.search2, theRowView, true); 
     iSearchViews[1] = theRowView; 
    } 
} 

佈局文件main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ExpandableListView 
     android:id="@+id/searchOptionsListView" 
     android:divider="@android:color/transparent"   
     android:childDivider="#00000000" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ExpandableListView> 

</LinearLayout> 

search1.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/searchByDeviceIdLinearLayout" 
    android:paddingLeft="36dp" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="5dp"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="4dp" 
     android:text="@string/deviceId"/> 

    <EditText 
     android:id="@+id/deviceIdEditText" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:inputType="number" > 
    </EditText> 

    <ImageButton 
     android:id="@+id/searchButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:contentDescription="@string/search" 
     android:src="@drawable/ic_btn_search" /> 

</LinearLayout> 

和search2.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/searchByDateLinearLayout" 
    android:paddingLeft="36dp"  
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="5dp"> 

    <ImageButton 
     android:id="@+id/searchButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:contentDescription="@string/search" 
     android:src="@drawable/ic_btn_search" /> 

</LinearLayout> 

回答

0

在我看來,損失的問題焦點是另一個問題的結果。
您的適配器構造不正確。請注意,對於屏幕上顯示的每個項目,它將傳入getChildView和getGroupView方法。 這就是爲什麼每次點擊它時組的名稱都會改變的原因。

如果我在這些方法中添加一些日誌,我可以看到:

01-24 11:18:02.575: D/TEST(443): > createSearchViews 
01-24 11:18:02.715: D/TEST(443): > getGroupView : position=0 
01-24 11:18:02.715: D/TEST(443): > getGroupView : position=1 
01-24 11:18:02.755: D/TEST(443): > getGroupView : position=0 
01-24 11:18:02.755: D/TEST(443): > getGroupView : position=1 
01-24 11:18:02.845: D/TEST(443): > getGroupView : position=0 
01-24 11:18:02.845: D/TEST(443): > getGroupView : position=1 
01-24 11:18:02.885: I/ActivityManager(59): Displayed activity com.test/.TestOnClickActivity: 455 ms (total 455 ms) 
01-24 11:18:02.905: D/TEST(443): > getGroupView : position=0 
01-24 11:18:02.905: D/TEST(443): > getGroupView : position=1 
01-24 11:18:08.246: D/TEST(443): > getGroupView : position=0 
01-24 11:18:08.246: D/TEST(443): > getGroupView : position=1 
01-24 11:18:08.246: D/TEST(443): > getChildView : position=1 
01-24 11:18:08.256: D/TEST(443): > getGroupView : position=0 
01-24 11:18:08.256: D/TEST(443): > getGroupView : position=1 
01-24 11:18:08.256: D/TEST(443): > getChildView : position=1 

在這個例子中,我只點擊了集團之一。
你需要重新考慮你的適配器,這個工作

對於我的代碼爲例,我有這樣的:

/** 
* {@inheritDoc} 
*/ 
@Override 
public View getGroupView(int p_iSecteurPosition, boolean p_bIsExpanded, View p_oConvertView, ViewGroup p_oParent) { 
    if (p_oConvertView==null){ 
     LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     p_oConvertView = infalInflater.inflate(R.layout.maquette_recherche_avancee_secteur_item_layout, null); 
    } 

    String sLabel = (String) getGroup(p_iSecteurPosition); 
    TextView oVoieLabel = (TextView) p_oConvertView.findViewById(R.id.maquette_secteur_label); 
    oVoieLabel.setText(sLabel); 

    return p_oConvertView; 
} 


/** 
* {@inheritDoc} 
*/ 
@Override 
public View getChildView(int p_iSecteurPosition, int p_iVoiePosition, boolean p_bIsLastChild, View p_oConvertView, ViewGroup p_oParent) { 
    if (p_oConvertView==null){ 
     LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     p_oConvertView = infalInflater.inflate(R.layout.maquette_recherche_avancee_voie_item_layout, null); 
    } 

    String sLabel = (String) getChild(p_iSecteurPosition, p_iVoiePosition); 
    TextView oVoieLabel = (TextView) p_oConvertView.findViewById(R.id.maquette_voie_label); 
    oVoieLabel.setText(sLabel); 

    CheckBox oCheck= (CheckBox) p_oConvertView.findViewById(R.id.maquette_voie_selection); 
    oCheck.setOnCheckedChangeListener(this); 

    p_oConvertView.setTag(FBO_KEY, p_iSecteurPosition+SEPARATOR+p_iVoiePosition); 

    return p_oConvertView; 
} 

一個簡單的教程在這裏:
http://androgue.blogspot.com/2011/08/android-expandablelistview-tutorial.html

希望我能幫助 François

+0

我的團隊的名字每次點擊它都不會改變。你的代碼和我的代碼之間的區別似乎是我在onCreate中創建了一次行視圖,而你的代碼每次getGroupView和getChildView都被調用時,它們都會設置它們。我會嘗試一下,看看它是否有所作爲。 – Milind

相關問題