2015-01-26 98 views
0

我正在一個應用程序有一個書籍的樹形結構,我在側面導航菜單中使用Expandable列表視圖顯示書名,單擊書名時顯示章節在子列表中。但是,在我的情況下,我需要以不同的方式,當用戶單擊書名時,應打開ChaptersFragment並顯示章節列表,當用戶單擊展開圖標時,章節應顯示在Expandable List中。ExpandableListView與自定義控件在android

mDrawerListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { 

     @Override 
     public boolean onGroupClick(ExpandableListView parent, View v, 
            int groupPosition, long id) { 

      return false; 
     } 
    }); 

    // Listview Group expanded listener 
    mDrawerListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { 

     @Override 
     public void onGroupExpand(int groupPosition) { 

     } 
    }); 

    // Listview Group collasped listener 
    mDrawerListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() { 

     @Override 
     public void onGroupCollapse(int groupPosition) { 


     } 
    }); 

    // Listview on child click listener 
    mDrawerListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 

     @Override 
     public boolean onChildClick(ExpandableListView parent, View v, 
            int groupPosition, int childPosition, long id) { 
      // TODO Auto-generated method stub 

      return false; 
     } 
    }); 

回答

0

您需要回調活動,說明哪些項目被點擊。那麼活動需要得到的信息來填充新的片段,並使用FragmentManager交換新片段

在NavigationDrawerFragment:

private NavigationDrawerListener mCallback; 

@Override 
public void onAttach(Activity activity) { 
    super.OnAttach(activity); 

    try { 
     mCallback = (NavigationDrawerListener) activity; 
    } catch (ClassCastException e) { 
     throw new ClassCastException("..."); 
    } 
    ... 
} 
... 
    @Override 
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { 
     mCallback.onNavigationItemClicked(groupPosition); 
    } 
... 
public static interface NavigationDrawerListerner { 
    public void onNavigationItemClicked(int groupPosition) 
} 
... 

在活動

... 
@Override 
public void onNavigationItemClicked(int groupPosition) { 
    Bundle args = getFragmentInformation(); //somehow 
    Fragment newFragment = YourFragment.newInstance(args); 
    getFragmentManager().beginTransaction(). 
      .replace(R.id.your_container), newFragment) //whatever ViewGroup contains the fragment 
      .commit(); 
} 
... 
+0

感謝@ seiterm,但我需要兩個行動來執行..我的意思是,溫用戶點擊指標,它應該擴大名單..我們如何實現這一點 – Nama 2015-01-27 05:55:39

+0

擴大名單是默認行爲。當聽者不在場時,我認爲這是有效的。你是否嘗試過不使用該事件(返回false)? – seiterm 2015-01-28 12:48:43