2010-04-16 38 views
5

好的,所以我寫了一個ExpandableListView和Subclassed BaseExpandableListAdapter ...一切正常,除非我無法讓子視圖在單擊時獲得焦點。如果我使用軌跡球,一切正常。但如果我嘗試點擊一個孩子,我不會收到任何反饋。ExpandableListView子項目不會在被觸及時得到焦點

我已經嘗試設置android:focusable,android:focusableInTouchMode和android:clickable(我也嘗試過通過代碼設置這個),但我無法獲得任何工作。有任何想法嗎?

這裏是我的適配器代碼:

public class ExpandableAppAdapter extends BaseExpandableListAdapter 
{ 
    private PackageManager m_pkgMgr; 
    private Context m_context; 
    private List<ApplicationInfo> m_groups; 
    private List<List<ComponentName>> m_children; 

    public ExpandableAppAdapter(Context context, List<ApplicationInfo> groups, List<List<ComponentName>> children) 
    { 
     m_context = context; 
     m_pkgMgr = m_context.getPackageManager(); 
     m_groups = groups; 
     m_children = children; 
    } 

    @Override 
    public Object getChild(int groupPos, int childPos) 
    { 
     return m_children.get(groupPos).get(childPos); 
    } 

    @Override 
    public long getChildId(int groupPos, int childPos) 
    { 
     return childPos; 
    } 

    @Override 
    public int getChildrenCount(int groupPos) 
    { 
     return m_children.get(groupPos).size(); 
    } 

    @Override 
    public View getChildView(int groupPos, int childPos, boolean isLastChild, View convertView, ViewGroup parent) 
    { 
     if (convertView == null) 
     { 
      LayoutInflater inflater = LayoutInflater.from(m_context); 
      convertView = inflater.inflate(R.layout.expandable_app_child_row, null); 
     } 

     ComponentName child = (ComponentName)getChild(groupPos, childPos); 
     TextView txtView = (TextView)convertView.findViewById(R.id.item_app_pkg_name_id); 
     if (txtView != null) 
      txtView.setText(child.getPackageName()); 

     txtView = (TextView)convertView.findViewById(R.id.item_app_class_name_id); 
     if (txtView != null) 
      txtView.setText(child.getClassName()); 

     convertView.setFocusableInTouchMode(true); 
     return convertView; 
    } 

    @Override 
    public Object getGroup(int groupPos) 
    { 
     return m_groups.get(groupPos); 
    } 

    @Override 
    public int getGroupCount() 
    { 
     return m_groups.size(); 
    } 

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

    @Override 
    public View getGroupView(int groupPos, boolean isExpanded, View convertView, ViewGroup parent) 
    { 
     if (convertView == null) 
     { 
      LayoutInflater inflater = LayoutInflater.from(m_context); 
      convertView = inflater.inflate(R.layout.expandable_app_group_row, null); 
     } 

     ApplicationInfo group = (ApplicationInfo)getGroup(groupPos); 
     ImageView imgView = (ImageView)convertView.findViewById(R.id.item_selection_icon_id); 
     if (imgView != null) 
     { 
      Drawable img = m_pkgMgr.getApplicationIcon(group); 
      imgView.setImageDrawable(img); 
      imgView.setMaxWidth(20); 
      imgView.setMaxHeight(20); 
     } 

     TextView txtView = (TextView)convertView.findViewById(R.id.item_app_name_id); 
     if (txtView != null) 
      txtView.setText(m_pkgMgr.getApplicationLabel(group)); 

     return convertView; 
    } 
    @Override 
    public boolean hasStableIds() 
    { 
     return false; 
    } 
    @Override 
    public boolean isChildSelectable(int groupPos, int childPos) 
    { 
     return true; 
    } 
} 

提前感謝!

+0

我有類似的問題試圖在一個ExpandedListView中以編程方式管理焦點。爲什麼你的'wrap_content'修復工作無法奏效。 – Mark 2011-01-28 12:21:58

+0

從技術上講,這應該被添加爲評論,而不是作爲答案... – Justin 2011-01-28 12:21:58

+0

我不知道爲什麼它可以工作,但它適用於我 – Justin 2011-01-28 12:21:58

回答

3

好吧,我終於明白了!

它實際上與我的XML文件,而不是我的適配器(我顯示的適配器,以顯示我創建子視圖時調用setFocusableInTouchMode)。

我使用教程(不記得是哪一個)有我的孩子身高設定爲:只要

android:layout_height="?android:attr/listPreferredItemHeight" 

因爲我改變它只是WRAP_CONTENT一切都開始正常工作。

相關問題