2013-08-11 14 views
10

私はかなり奇妙な問題を抱えています。私のロジックは簡単で、私のアプリはメインアプリケーションのナビゲーションにNavigationDrawerを使用しています。利用可能なオプションが複雑であるため、ExpandableListVievを使用してナビゲーション項目をネストします。ExpandableListViewとNavigationDrawer

問題がありますか?シンプルすぎます:アイテムのリストがドロワーにロードされます。しかし、彼らは子供たちを示すために拡大しない。

質問拡大しない理由はありますか?このために別のUIウィジェットを使用する必要がありますか?いくつかの注意事項

:私は別の活動に同じコードを追加しましたが、代わりに引き出し上ExpandableListVievをホスティングしているのは、私はそれが唯一の要素として、単純なLinearLayoutに追加持っていて、それがうまくに動作します。活動のための

マイレイアウト:

<android.support.v4.widget.DrawerLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/drawer_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context=".MainActivity"> 
    <FrameLayout 
      android:id="@+id/content_frame" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

<ExpandableListView 
     android:id="@+id/left_drawer" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="@color/secondary_color_holo"/> 

</android.support.v4.widget.DrawerLayout> 

アクティビティコード:

private DrawerLayout mDrawerLayout; 
private ExpandableListView mDrawerList; 
private ExpandableDrawerAdapter mDrawerAdapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
    mDrawerList = (ExpandableListView) findViewById(R.id.left_drawer); 
    // set up the drawer's list view with items and click listener 
    mDrawerAdapter = new ExpandableDrawerAdapter(this); 
    mDrawerList.setAdapter(mDrawerAdapter); 
    mDrawerList.setOnGroupClickListener(new DrawerGroupClickListener()); 
    mDrawerList.setOnChildClickListener(new DrawerChildClickListener()); 
} 

そして最後に、非常に単純なアダプター:

public class ExpandableDrawerAdapter extends BaseExpandableListAdapter { 

private static final int CAMPAIGNS = 0; 
private static final int CONVERSIONS = 1; 
private static final int MYACCOUNT = 2; 

private LayoutInflater mInflater; 
private String[] mainNavItems, campaignNavItems, myAccountNavItems; 

public ExpandableDrawerAdapter (Activity context) { 
    this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    this.mainNavItems = context.getResources().getStringArray(R.array.main_nav_items); 
    this.campaignNavItems = context.getResources().getStringArray(R.array.campaigns_nav_items); 
    this.myAccountNavItems = context.getResources().getStringArray(R.array.my_account_nav_items); 
} 

public Object getChild(int groupPosition, int childPosition) { 

    switch (groupPosition) { 
     case CAMPAIGNS: 
      return campaignNavItems[childPosition]; 
     case MYACCOUNT: 
      return myAccountNavItems[childPosition]; 
     default: 
      return ""; 
    } 
} 

public long getChildId(int groupPosition, int childPosition) { 
    return childPosition; 
} 

public View getChildView(final int groupPosition, final int childPosition, 
         boolean isLastChild, View convertView, ViewGroup parent) { 

    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.drawer_child_list_item, null); 
    } 

    TextView childText = (TextView) convertView.findViewById(R.id.drawer_child_list_item_text); 
    childText.setText((String) getChild(groupPosition, childPosition)); 

    return convertView; 
} 

public int getChildrenCount(int groupPosition) { 
    switch (groupPosition) { 
     case CAMPAIGNS: 
      Constants.logMessage("Children for group position: " + groupPosition + " are: " + campaignNavItems.length); 
      return campaignNavItems.length; 
     case CONVERSIONS: 
      Constants.logMessage("Children for group position: " + groupPosition + " are: " + 0); 
      return 0; 
     case MYACCOUNT: 
      Constants.logMessage("Children for group position: " + groupPosition + " are: " + myAccountNavItems.length); 
      return myAccountNavItems.length; 
     default: 
      return 0; 
    } 
} 

public Object getGroup(int groupPosition) { 
    return mainNavItems[groupPosition]; 
} 

public int getGroupCount() { 
    return mainNavItems.length; 
} 

public long getGroupId(int groupPosition) { 
    return groupPosition; 
} 

public View getGroupView(int groupPosition, boolean isExpanded, 
         View convertView, ViewGroup parent) { 

    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.drawer_list_item, 
       null); 
    } 

    TextView groupText = (TextView) convertView.findViewById(R.id.drawer_list_item_text); 
    groupText.setText((String) getGroup(groupPosition)); 
    return convertView; 
} 

public boolean hasStableIds() { 
    return true; 
} 

public boolean isChildSelectable(int groupPosition, int childPosition) { 
    return true; 
} 
} 
+1

明白な疑問は、それらの配列に子供のための値がある場合です。私は、あなたが 'ExpandableListView'で' OnGroupClickListener'を設定しているのを見て、そのリスナーの 'onGroupClick()'コールバックから 'false'を返すことを願っています。そうしないと、崩壊/ 'true'(既にイベントを処理したので、他のアクションは必要ありません)。 – Luksprog

+0

ええ、ドキュメントでは、trueを返すとデフォルトの折りたたみと展開が妨げられることはあまりにも明白になりません。私は引き出しの外で同じロジックを適用したことも奇妙で、うまくいきました。私は当時は自宅にいませんが、私は両方のリスナーに真実を返すので、私の問題の正しい解決策のように聞こえるでしょう。もしあなたが望むなら、それを答えとして掲示して、家に帰って偽を返すようにテストしたら、あなたの答えを受け入れることができます。 –

+0

各アイテムは展開/縮小していますか?または、拡張可能なアイテムや、クリック可能なアイテムのまま残っているアイテムがありますか? – AlleyOOP

答えて

16

私が作る、私のコメントで言ったようにに設定したDrawerGroupClickListenerからfalseを返すようにしてください10。リスナーのonGroupClick()コールバックからtrueを返すと、イベントがconsidered handledであるため、折りたたみ/展開アクションがブロックされます。

+1

うん、うまくいきました。ありがとう! –