2014-01-14 163 views
13

我怎麼能這樣做?導航抽屜內Android:2個或多個ExpandableListView導航抽屜裏面

enter image description here

兩個可擴展列表視圖。我嘗試將它添加到我的XML中,但沒有運氣。 我要的是隻有一個滾動視圖,但我不」知道怎麼做吧..

這是我的抽屜式導航欄佈局:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:background="@color/Bianco" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:orientation="vertical" 
     android:layout_marginLeft="16dp" 
     android:layout_marginTop="8dp" 
     android:layout_marginBottom="8dp" 
     android:layout_marginRight="16dp" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="@string/tvHomeActions" 
      android:id="@+id/textView" /> 

     <ExpandableListView 
      android:id="@+id/elvHome" 
      android:layout_width="match_parent" 
      android:layout_marginTop="4dp" 
      android:layout_height="300dp" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="@string/tvHomeNavigations" 
      android:layout_marginTop="16dp" 
      android:id="@+id/textView2" /> 

     <ExpandableListView 
      android:id="@+id/elvNavigateTo" 
      android:layout_width="match_parent" 
      android:layout_height="200dp" 
      android:layout_marginTop="4dp" /> 

    </LinearLayout> 

</ScrollView> 

編輯: 我想在Gmail應用中創建類似抽屜的內容

+2

我認爲3是一個計數器,而不是項目內的項目數,對於你的問題我也認爲沒有2個不同的可擴展ListViews。認爲標題1和標題2作爲分隔符,我不必編碼如何添加它們。 – user666

+1

好吧,但是如果你看LOG_TAG發佈的屏幕截圖,「item 3」有一個「3」,它會被關閉。我認爲,如果「項目3」可以打開,就像「項目4」有3個孩子。 無論如何,現在我刪除了所有,我只添加一個可解析的列表視圖。 我認爲,所有的關鍵是在可擴展列表視圖內的組或headerviews .. Ps。對不起,我的英文全部;) –

+0

不,盒內3表示在按下該按鈕時引起的片段中有三個項目要查看(例如按下收件箱時有4條未讀消息) – AlleyOOP

回答

16

enter image description here

最後我有啦! 這是我創建的用於獲得帶有節標題的ExpandableListView的代碼。 現在,我可以輕鬆地爲標題,組和兒童創建三個xml自定義佈局。

它適用於我,但我接受任何代碼改進來優化內存使用情況,速度等。

// --------------------------------------------------------------------------------------------- 
// NAVIGATION DRAWER SIDE FRAGMENT 
// --------------------------------------------------------------------------------------------- 

private ExpandableListView mDrawerListView; 
private List<Elemento> mainActions = new ArrayList<>(); 
private HashMap<Integer, List<String>> childActions = new HashMap<>(); 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    View v = inflater.inflate(R.layout.frg_navigation_drawer, container, false); 

    assert v != null; 

    mDrawerListView = (ExpandableListView) v.findViewById(R.id.elvHome); 
    mDrawerListView.setGroupIndicator(null); 

    // add first title 
    mainActions.add(new TitoloGruppo("Good guys"));      // 0 
    mainActions.add(new Azione("Admiral Ackbar", "Dagobah System")); // 1 
    mainActions.add(new Azione("Han Solo", "Millenium Falcon"));  // 2 
    mainActions.add(new Azione("Yoda", "Dagobah System"));    // 3 
    // add second title 
    mainActions.add(new TitoloGruppo("Bad guys"));      // 4 
    mainActions.add(new Azione("Emperor", "Death star 2"));    // 5 
    mainActions.add(new Azione("Jabba", "Tatooine"));     // 6 
    mainActions.add(new Azione("Grand Moff Tarkin", "Death star 1")); // 7 

    // Adding child quotes to Ackbar 
    List<String> mainSubFive = new ArrayList<>(); 
    mainSubFive.add("It's a trap!"); 

    // Adding child quotes to Yoda 
    List<String> mainSubThree = new ArrayList<>(); 
    mainSubThree.add("Do or do not; there is no try."); 
    mainSubThree.add("There is … another … Sky … walker.…"); 
    mainSubThree.add("When 900 years old you reach, look as good you will not ehh."); 

    childActions.put(0, new ArrayList<String>()); 
    childActions.put(1, mainSubFive); 
    childActions.put(2, new ArrayList<String>()); 
    childActions.put(3, mainSubThree); 
    childActions.put(4, new ArrayList<String>()); 
    childActions.put(5, new ArrayList<String>()); 
    childActions.put(6, new ArrayList<String>()); 

    mDrawerListView.setAdapter(new ExpandableAdapter(getActivity(), mainActions, childActions)); 
    mDrawerListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { 
     @Override 
     public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { 
      List<String> list = childActions.get(groupPosition); 
      if(list.size() > 0) 
       return false; 
      else 
       Toast.makeText(getActivity(), ""+ ((Azione) mainActions.get(groupPosition)).getSubtitle(), Toast.LENGTH_LONG).show(); 
      return false; 
     } 
    }); 

    mDrawerListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
     @Override 
     public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 
      List<String> list = childActions.get(groupPosition); 

      Toast.makeText(getActivity(), "" + list.get(childPosition).toString(), Toast.LENGTH_LONG).show(); 
      return false; 
     } 
    }); 
    return v; 
} 


// --------------------------------------------------------------------------------------------- 
// INTERNAL CLASS 
// --------------------------------------------------------------------------------------------- 

protected class ExpandableAdapter extends BaseExpandableListAdapter { 

    private Context context; 
    private List<Elemento> mainElements; 
    private HashMap<Integer, List<String>> childElements; 
    private LayoutInflater vi; 

    public ExpandableAdapter(Context context, List<Elemento> mainElements, HashMap<Integer, List<String>> childElements) { 
     this.context = context; 
     this.mainElements = mainElements; 
     this.childElements = childElements; 
     vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

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

    @Override 
    public int getChildrenCount(int groupPosition) { 
     if(this.childElements.get(groupPosition) == null) 
      return 0; 
     return this.childElements.get(groupPosition).size(); 
    } 

    @Override 
    public Object getGroup(int groupPosition) { 
     return this.mainElements.get(groupPosition); 
    } 

    @Override 
    public Object getChild(int groupPosition, int childPosition) { 
     return this.childElements.get(groupPosition).get(childPosition); 
    } 

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

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

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

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
     View v = convertView; 

     final Elemento i = mainElements.get(groupPosition); 
     if (i != null) { 
      if(i.isGroupSection()){ 
       final TitoloGruppo si = (TitoloGruppo)i; 
       v = vi.inflate(android.R.layout.simple_list_item_1, null); 
       v.setOnClickListener(null); 
       v.setOnLongClickListener(null); 
       v.setLongClickable(false); 
       final TextView sectionView = (TextView) v.findViewById(android.R.id.text1); 
       sectionView.setTextColor(Color.parseColor("#FFC800")); 
       sectionView.setText(si.getTitle()); 
      }else if(i.isAction()){ 
       Azione ei = (Azione)i; 
       v = vi.inflate(android.R.layout.simple_list_item_2, null); 
       final TextView title = (TextView)v.findViewById(android.R.id.text1); 
       final TextView subtitle = (TextView)v.findViewById(android.R.id.text2); 

       if (title != null) 
        title.setText(ei.title); 
       if(subtitle != null) 
        subtitle.setText("count: " + getChildrenCount(groupPosition)); 
      } 
     } 
     return v; 
    } 

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

     final String childText = (String) getChild(groupPosition, childPosition); 

     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) this.context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(android.R.layout.simple_list_item_1, null); 
     } 

     TextView txtListChild = (TextView) convertView.findViewById(android.R.id.text1); 
     txtListChild.setText(childText); 
     return convertView; 
    } 

    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 
} 

    public class TitoloGruppo implements Elemento { 

    private final String titolo; 

    public TitoloGruppo(String titolo) { 
     this.titolo = titolo; 
    } 

    public String getTitle(){ 
     return titolo; 
    } 

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

    @Override 
    public boolean isAction() { 
     return false; 
    } 
} 

protected interface Elemento { 
    public boolean isGroupSection(); 
    public boolean isAction(); 
} 

protected class Azione implements Elemento { 
    public final String title; 
    public final String subtitle; 

    public Azione(String title, String subtitle) { 
     this.title = title; 
     this.subtitle = subtitle; 
    } 

    public String getTitle() { 
     return this.title; 
    } 

    public String getSubtitle() { 
     return this.subtitle; 
    } 

    @Override 
    public boolean isGroupSection() { 
     return false; 
    } 

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

Ps。謝謝大家

+2

+1分享! –

+0

你能分享這個完整的源代碼還是內部活動? – NovusMobile

+0

嗨,活動代碼,是默認的Android Studio空白項目的代碼 –

8

供參考您在qsn中顯示的屏幕截圖也有Pinnned或分段列表視圖。

ExpandableListView在導航抽屜:

enter image description here

使用此代碼爲DrawerLayoutTest在ExpandableListView導航抽屜裏。

更新:這正是你尋找的東西,給它在嘗試這種michenux navigation-drawerGit

邏輯:

1>使用ExpandableListView + michenux導航抽屜式抽屜設計,可擴展列表視圖和計數的中的「3」項使用jgilfelt的android-viewbadger庫。

2>你必須在列表視圖中使用getview(..)來禁用啓用可擴展列表視圖的下拉圖標,它沒有子項(檢查數組或列表列表爲空/空)和使看不見的獾(下拉計數圖標/獾)多數民衆贊成在它或簡單地根據每個項目值改變列表視圖項目佈局例如:對於列表行包含可擴展的孩子裝載不同的佈局與視圖獾!

現金Michenaud,Jgilfelt

+0

謝謝你的回答,我不知道我認爲我的屏幕截圖與固定列表視圖有關。 我認爲這是一個擴大的列表視圖(「TopView 4」作爲內部的「3」項的計數)和頂部的列表視圖。這就是我想要的。 我檢查了你的鏈接,但那不是我正在尋找的.. 再次謝謝;) –

+0

我想在Gmail應用程序中創建類似抽屜的東西 –

+0

@crc_error更新了答案給它一個嘗試,讓我知道。 –