7

Tôi muốn có thể tạo Ngăn điều hướng có một số mục có thể mở rộng, có thể chọn và một số mục không thể mở rộng. Sự đồng thuận về StackOverflow cho các câu hỏi tương tự như điểm của tôi đối với các giải pháp của ExpandableListView (có thể thậm chí không áp dụng cho ý tưởng của tôi). Đối với hầu hết các phần, những gì mọi người đang yêu cầu là một cách để tách các mục trong ngăn kéo Nav như ứng dụng GMail với nhãn, không phải những gì tôi đang cố gắng làm ...Cách tạo menu thả xuống trong Ngăn điều hướng (trong Android)?

... về cơ bản được vạch ra HERE enter image description here

SIMILARLY HERE (though all, not some are dropdowns) . Và không phải như THIS ANSWER.

Trả lời

1

Sử dụng một ExpandableListView trong một DrawerLayout, như vậy:

<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_layout2" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<FrameLayout 
    android:id="@+id/content_frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/tv_commentary_behind_nav" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal|top" 
     android:text="Frame text" /> 
</FrameLayout> 
<!-- The navigation drawer --> 
    <ExpandableListView 
     android:id="@+id/left_drawer2" 
     android:layout_width="250dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="@color/white" 
     android:choiceMode="multipleChoice" 
     android:dividerHeight="0dp" 
     /> 

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

Sau đó khởi tạo trong mã như vậy:

private DrawerLayout drawer; 
    private ExpandableListView drawerList; 
    private CheckBox checkBox; 
    private ActionBarDrawerToggle actionBarDrawerToggle; 


@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.drawer_layout); 
     drawer = (DrawerLayout) findViewById(R.id.drawer_layout2); 
     drawerList = (ExpandableListView) findViewById(R.id.left_drawer2); 
     drawerList.setAdapter(new NewAdapter(this, groupItem, childItem)); 
    } 
+1

'groupItem' và' childItem' là gì. NewAdapter là gì và nó xử lý 'groupItem' và' childItem' như thế nào để làm cho một số mục có thể mở rộng? Câu trả lời của bạn chưa hoàn thành. –

+0

groupItem là một mảng chứa Strings cho từng tùy chọn Nhóm. "TopView 2" ở trên đồ họa. childItem là một ma trận/băm có chứa một danh sách các mục được thả xuống sau khi chọn từng mục trong nhóm. NewAdapter gán các giá trị của các bộ dữ liệu groupItem và childItem cho các khung nhìn khi nó thổi phồng chủ của mỗi mục. – AlleyOOP

1

Bạn nên có một lớp có tất cả các phương pháp thực hiện của ExpandableListAdapter cũng như ChildItemsInfo và một lớp học GroupItemsInfo, với MainActivity để người nghe nhấp chuột nhóm các mục và con cái của họ

... bây giờ để cụ thể hơn ...

Bạn có thể đặt này trong vòng getGroupView() đó là bên MyExpandableListAdapter lớp

View ind = convertView.findViewById(R.id.group_indicator); 
    View ind2 = convertView.findViewById(R.id.group_indicator2); 
    if (ind != null) 
    { 
     ImageView indicator = (ImageView) ind; 
     if (getChildrenCount(groupPosition) == 0) 
     { 
      indicator.setVisibility(View.INVISIBLE); 
     } 
     else 
     { 
      indicator.setVisibility(View.VISIBLE); 
      int stateSetIndex = (isExpanded ? 1 : 0); 

      /*toggles down button to change upwards when list has expanded*/ 
      if(stateSetIndex == 1){ 
       ind.setVisibility(View.INVISIBLE); 
       ind2.setVisibility(View.VISIBLE); 
       Drawable drawable = indicator.getDrawable(); 
       drawable.setState(GROUP_STATE_SETS[stateSetIndex]); 
      } 
      else if(stateSetIndex == 0){ 
       ind.setVisibility(View.VISIBLE); 
       ind2.setVisibility(View.INVISIBLE); 
       Drawable drawable = indicator.getDrawable(); 
       drawable.setState(GROUP_STATE_SETS[stateSetIndex]); 
      } 
     } 
    } 

... và như đối với quan điểm bố trí, đây là cách group_items tôi. xml dường như là đủ

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<TextView 
    android:id="@+id/group_heading" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingLeft="20dp" 
    android:paddingTop="16dp" 
    android:paddingBottom="16dp" 
    android:textSize="15sp" 
    android:textStyle="bold"/> 

<ImageView 
    android:id="@+id/group_indicator" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@android:drawable/arrow_down_float" 
    android:layout_alignParentRight="true" 
    android:paddingRight="20dp" 
    android:paddingTop="20dp"/> 

<ImageView 
    android:id="@+id/group_indicator2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@android:drawable/arrow_up_float" 
    android:layout_alignParentRight="true" 
    android:visibility="gone" 
    android:paddingRight="20dp" 
    android:paddingTop="20dp"/> 

Không rõ ràng ?, bình luận bất cứ khi nào

Các vấn đề liên quan