Robert Sun
Robert Sun

Reputation: 43

Expandable List View items without Group

I'm trying to implement an expandable list view for a navigation drawer menu. I was wondering if there was a way to make it so that only one of the items in the menu could expand? For example, opening the navigation drawer menu would yield

A
B
C
->C1
->C2
->C3
D

So, C would be the only group while A, B, and D would just be singular items that don't have any children. Anyone have any insight into this?

Upvotes: 0

Views: 584

Answers (2)

Robust
Robust

Reputation: 2485

Another way to get your design is using a Layout in navigation drawer menu, inside this layout, put an ExpandListView with one element. And, if you want to do that, just follow 2 steps below:

Step 1: design layout:

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The navigation drawer -->
    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="220dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_gravity="start"
        android:choiceMode="singleChoice">

        <!-- you can put other views, I put a textview here -->

        <TextView
            android:id="@+id/tv_test"
            android:text="New Test"
            android:layout_width="210dp"
            android:layout_height="wrap_content" />

    </LinearLayout>

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

Step 2: java code:

DrawerLayout mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
TextView tv_test = (TextView )findViewById(R.id.tv_test);
tv_test.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        mDrawerLayout.closeDrawer(linearLayout);//don't forget it
        mDrawerLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                //action
            }
        } 
    }
});

Upvotes: 0

Ankii Rawat
Ankii Rawat

Reputation: 2080

You can do that. just map the child to only the "C" which are c1, c2, etc. And replace

   @Override
public int getChildrenCount(int groupPosition) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .size();
}

this in ExpandableListAdapter with

  @Override
public int getChildrenCount(int groupPosition) {
    try {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    } catch (NullPointerException e) {
        e.printStackTrace();
        return 0;
    }
}

this will return 0 child count for A, B, D etc.

Upvotes: 0

Related Questions