How to create Sliding Menu in FragmentActivity – Android Tutorial

 

I recently developed an android application Superjokes which is.. if I say so myself.. the best jokes app with a rich selection of Jokes of all kinds. In one of the activities, I had to create a sliding menu using the support v4 library. Sliding menu is a simple menu which is generally invisible when the activity first becomes visible. It becomes visible only on a button press. You can see the example screenshot of what I created in Superjokes application.. go ahead and download it if you would like to see how it works..

INVISIBLE state:-

device-2015-02-25-150041

 

VISIBLE state:-

device-2015-02-25-150102

 

As you can see, this is different from normal sliding menu as , it does not overlap the activity but it moves the activity to this side. This is a simple tutorial of how I created the Sliding menu.

First we need to create our own implementation of a linear layout which will mainly be responsible for animation and creation of the sliding menu. Create a class called MainLayout and extend LinearLayout.

MainLayout.java:-

[java]

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Interpolator;
import android.widget.LinearLayout;
import android.widget.Scroller;

public class MainLayout extends LinearLayout {

private static final int SLIDING_DURATION = 500;
private static final int QUERY_INTERVAL = 16;
int mainLayoutWidth;
private View menu;
private View content;
private static int menuRightMargin = 15;

private enum MenuState {
HIDING, HIDDEN, SHOWING, SHOWN,
};

private int contentXOffset;
private MenuState currentMenuState = MenuState.HIDDEN;
private Scroller menuScroller = new Scroller(this.getContext(),
new EaseInInterpolator());
private Runnable menuRunnable = new MenuRunnable();
private Handler menuHandler = new Handler();
int prevX = 0;
boolean isDragging = false;
int lastDiffX = 0;

public MainLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}

public MainLayout(Context context) {
super(context);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mainLayoutWidth = MeasureSpec.getSize(widthMeasureSpec);
menuRightMargin = mainLayoutWidth * 25 / 100;
}

@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();

menu = this.getChildAt(0);
content = this.getChildAt(1);
content.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
return MainLayout.this.onContentTouch(v, event);
}
});
menu.setVisibility(View.GONE);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
if (changed) {
LayoutParams contentLayoutParams = (LayoutParams) content
.getLayoutParams();
contentLayoutParams.height = this.getHeight();
contentLayoutParams.width = this.getWidth();
LayoutParams menuLayoutParams = (LayoutParams) menu
.getLayoutParams();
menuLayoutParams.height = this.getHeight();
menuLayoutParams.width = this.getWidth() – menuRightMargin;
}
menu.layout(left, top, right – menuRightMargin, bottom);
content.layout(left + contentXOffset, top, right + contentXOffset,
bottom);

}

public void toggleMenu() {

if (currentMenuState == MenuState.HIDING || currentMenuState == MenuState.SHOWING)
return;

switch (currentMenuState) {

case HIDDEN:
currentMenuState = MenuState.SHOWING;
menu.setVisibility(View.VISIBLE);
menuScroller.startScroll(0, 0, menu.getLayoutParams().width, 0, SLIDING_DURATION);
break;
case SHOWN:
currentMenuState = MenuState.HIDING;
menuScroller.startScroll(contentXOffset, 0, -contentXOffset, 0, SLIDING_DURATION);
break;
default:
break;
}
menuHandler.postDelayed(menuRunnable, QUERY_INTERVAL);
this.invalidate();
}

protected class MenuRunnable implements Runnable {
@Override
public void run() {
boolean isScrolling = menuScroller.computeScrollOffset();
adjustContentPosition(isScrolling);
}
}

private void adjustContentPosition(boolean isScrolling) {
int scrollerXOffset = menuScroller.getCurrX();
content.offsetLeftAndRight(scrollerXOffset – contentXOffset);
contentXOffset = scrollerXOffset;
this.invalidate();
if (isScrolling)
menuHandler.postDelayed(menuRunnable, QUERY_INTERVAL);
else
this.onMenuSlidingComplete();
}

private void onMenuSlidingComplete() {
switch (currentMenuState) {
case SHOWING:
currentMenuState = MenuState.SHOWN;
break;
case HIDING:
currentMenuState = MenuState.HIDDEN;
menu.setVisibility(View.GONE);
break;
default:
return;
}
}

protected class EaseInInterpolator implements Interpolator {
@Override
public float getInterpolation(float t) {
return (float) Math.pow(t – 1, 5) + 1;
}

}

public boolean isMenuShown() {
return currentMenuState == MenuState.SHOWN;
}

public boolean onContentTouch(View v, MotionEvent event) {
if (currentMenuState == MenuState.HIDING || currentMenuState == MenuState.SHOWING)
return false;
int curX = (int) event.getRawX();
int diffX = 0;

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
prevX = curX;
return true;

case MotionEvent.ACTION_MOVE:
if (!isDragging) {
isDragging = true;
menu.setVisibility(View.VISIBLE);
}
diffX = curX – prevX;
if (contentXOffset + diffX <= 0) {
diffX = -contentXOffset;
} else if (contentXOffset + diffX > mainLayoutWidth – menuRightMargin) {
diffX = mainLayoutWidth – menuRightMargin – contentXOffset;
}

content.offsetLeftAndRight(diffX);
contentXOffset += diffX;
this.invalidate();

prevX = curX;
lastDiffX = diffX;
return true;

case MotionEvent.ACTION_UP:
Log.d(“MainLayout.java onContentTouch()”, “Up lastDiffX ” + lastDiffX);

if (lastDiffX > 0) {
currentMenuState = MenuState.SHOWING;
menuScroller.startScroll(contentXOffset, 0,
menu.getLayoutParams().width – contentXOffset, 0, SLIDING_DURATION);
} else if (lastDiffX < 0) {
currentMenuState = MenuState.HIDING;
menuScroller.startScroll(contentXOffset, 0, -contentXOffset, 0, SLIDING_DURATION);
}

menuHandler.postDelayed(menuRunnable, QUERY_INTERVAL);
this.invalidate();
isDragging = false;
prevX = 0;
lastDiffX = 0;
return true;

default:
break;
}

return false;
}
}

[/java]

 

Inside your activity class (Lets call it DisplayActivity), we need to refer to a layout xml. We will be adding our MainLayout created above in the xml file (activity_display.xml)

activity_display.xml :-

 

[xml]<YOUR_PACKAGE_NAME.SlideMenu.MainLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”wrap_content”
android:layout_height=”match_parent” >
<LinearLayout

xmlns:android=”http://schemas.android.com/apk/res/android”

android:layout_width=”match_parent”

android:layout_height=”match_parent”

android:orientation=”vertical” >

<ListView

android:id=”@+id/menu_listview”

android:layout_width=”wrap_content”

android:layout_height=”match_parent”

style=”@style/styleMenuDrawerList”

android:drawSelectorOnTop=”true” >

</ListView>

</LinearLayout>

<RelativeLayout

xmlns:android=”http://schemas.android.com/apk/res/android”

xmlns:tools=”http://schemas.android.com/tools”

xmlns:ads=”http://schemas.android.com/apk/res-auto”

android:id=”@+id/container”

android:layout_width=”match_parent”

android:layout_height=”match_parent”

android:background=”@drawable/background_display”

tools:context=”com.taureano.superjokes.DisplayActivity”

tools:ignore=”MergeRootFrame” >

<!– This acts as the Action Bar –>

<LinearLayout

android:id=”@+id/displayLinearBar”

android:layout_width=”match_parent”

android:layout_height=”wrap_content”

android:background=”@drawable/display_bar”

android:gravity=”start”

android:orientation=”horizontal”

android:weightSum=”1.0″ >

<View

android:id=”@+id/iView_Lib_Icon”

android:onClick=”toggleMenu”

android:layout_width=”0dip”

android:layout_height=”54dip”

android:layout_marginRight=”6dip”

android:layout_marginTop=”6dip”

android:layout_weight=”0.2″

android:scaleType=”fitXY” />

<View

android:layout_weight=”0.25″

android:layout_width=”0dip”

android:layout_height=”0dip”/>

<Button

android:id=”@+id/button_bookmark”

android:layout_width=”0dip”

android:layout_height=”wrap_content”

android:layout_weight=”0.1″

android:background=”@drawable/bookmark_it”

/>

<View

android:layout_weight=”0.25″

android:layout_width=”0dip”

android:layout_height=”0dip”/>

<Button

android:id=”@+id/iView_SJ_BackIcon”

android:layout_width=”0dip”

android:layout_height=”wrap_content”

android:layout_weight=”0.2″

android:layout_marginLeft=”6dip”

android:background=”@drawable/back_icon”

android:contentDescription=”@string/back_icon”

android:scaleType=”fitXY”

android:onClick=”homeClick” />

</LinearLayout>

<TextView

android:id=”@+id/joke_head”

android:scaleType=”fitXY”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:background=”@drawable/joke_head”

android:gravity =”center”

android:textSize=”22sp”

android:typeface=”serif”

android:paddingTop=”10dp”

android:paddingLeft=”48dp”

android:paddingRight=”48dp”

android:scrollHorizontally=”true”

android:contentDescription=”@string/desc_JokeHeader”

android:layout_below=”@id/displayLinearBar”

android:maxLines = “5”

android:scrollbars = “vertical”

/>

<ScrollView

android:layout_width=”match_parent”

android:layout_height=”wrap_content”

android:layout_below=”@id/joke_head”

android:layout_above=”@+id/displayBottomBar”

android:background=”@drawable/display_joke”

android:paddingTop=”14dp”

android:layout_alignLeft=”@id/joke_head”

android:paddingBottom=”14dp”

android:id=”@+id/scrollView”

>

<RelativeLayout

android:layout_width=”match_parent”

android:layout_height=”wrap_content”

android:paddingBottom=”@dimen/activity_vertical_margin”

android:paddingLeft=”@dimen/activity_horizontal_margin”

android:paddingRight=”@dimen/activity_horizontal_margin”

android:paddingTop=”@dimen/activity_vertical_margin”

>

<TextView

android:id=”@+id/textViewInImage”

android:layout_width=”match_parent”

android:layout_height=”match_parent”

android:textSize=”16sp”

android:typeface=”serif”

android:paddingTop=”16dp”

android:paddingLeft=”48dp”

android:paddingRight=”48dp”

android:paddingBottom=”40dp”

android:contentDescription=”@string/jokebackground”

android:gravity=”left” />

</RelativeLayout>

</ScrollView>

<com.google.android.gms.ads.AdView

android:id=”@+id/adMob”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:layout_above = “@id/displayBottomBar”

ads:adSize=”SMART_BANNER”

ads:adUnitId=”xxxxxx” />

<LinearLayout

android:id=”@+id/displayBottomBar”

android:layout_width=”match_parent”

android:layout_height=”64dp”

android:layout_alignParentBottom=”true”

android:background=”@drawable/bottom_display_bar”

android:orientation=”horizontal”

android:weightSum=”1.0″ >

<Button

android:id=”@+id/button_delete_joke”

android:layout_width=”0dip”

android:layout_height=”wrap_content”

android:layout_weight=”0.15″

android:layout_margin=”6dip”

android:background=”@drawable/delete_joke” />

<View

android:layout_weight=”0.1″

android:layout_width=”0dip”

android:layout_height=”0dip”/>

<Button

android:id=”@+id/button_prev”

android:layout_width=”0dip”

android:layout_height=”wrap_content”

android:layout_weight=”0.1″

android:layout_margin=”6dip”

android:background=”@drawable/prev_button” />

<View

android:layout_weight=”0.05″

android:layout_width=”0dip”

android:layout_height=”0dip”/>

<Button

android:id=”@+id/button_random”

android:layout_width=”0dip”

android:layout_height=”wrap_content”

android:layout_weight=”0.2″

android:layout_margin=”6dip”

android:background=”@drawable/random_button” />

<View

android:layout_weight=”0.05″

android:layout_width=”0dip”

android:layout_height=”0dip”/>

<Button

android:id=”@+id/button_next”

android:layout_width=”0dip”

android:layout_height=”wrap_content”

android:layout_weight=”0.1″

android:layout_margin=”6dip”

android:background=”@drawable/next_button” />

<View

android:layout_weight=”0.1″

android:layout_width=”0dip”

android:layout_height=”0dip”/>

<Button

android:id=”@+id/button_share_it”

android:layout_width=”0dip”

android:layout_height=”wrap_content”

android:layout_weight=”0.15″

android:layout_margin=”6dip”

android:background=”@drawable/share_it” />

</LinearLayout>

</RelativeLayout>

<FrameLayout

android:id=”@+id/activity_main_content_fragment”

android:layout_width=”match_parent”

android:layout_height=”match_parent” >

</FrameLayout>
[/xml]

 

Replace YOUR_PACKAGE_NAME with your package name. In the first screenshot you can see that the actionbar that I used is implemented by me only and I didn’t use the systems default actionbar. If you choose to do so, in the above code, you can see how the actionbar is created.The button that will toggle the sliding menu is named iView_Lib_Icon

Unfortunetly the images used in the app are copywritten so I am not sharing them here so if errors pop up wherever I have used @drawables, replace them with yours if you are infact creating the exact same thing as my activity.

Now in your styles.xml file (in res->values folder), add a new style.

 

[xml]
<style name=”styleMenuDrawerList” parent=”android:Widget.Holo.Light.ListView”>

<itemname=”android:background”>@drawable/background_dash_drawer</item>

<itemname=”android:choiceMode”>singleChoice</item>

<itemname=”android:dividerHeight”>4dip</item>

<itemname=”android:paddingLeft”>4dip</item>

<itemname=”android:paddingRight”>4dip</item>

</style>
[/xml]

 

You can change the drawable above (background_dash_drawer) to whatever you need as a background to your drawer. In my case it was a simple wood texture that you can find freely if you do google image search.

Now, Inside your DisplayActivity, extend FragmentActivity (I am using support fragment library but you can use the other non support library if you are not supporting the older android apis).

 

[java]

public class DisplayActivity extends FragmentActivity{

public static final String TAG = DisplayActivity.class.getSimpleName();

MainLayout mLayout;

private ListView lvMenu; //for list view menu

@Override

protectedvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

mLayout = (MainLayout) this.getLayoutInflater().inflate(R.layout.activity_display, null);

setContentView(mLayout);

View libIcon = findViewById(R.id.iView_Lib_Icon);

libIcon.setBackgroundResource(GET_THUMB);  //Add your own image for the toggle button

libIcon.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// Show/hide the menu

toggleMenu(v);

}

});

lvMenu = (ListView) findViewById(R.id.menu_listview);

ArrayList<NavDrawerItem> navDrawerBookmarked = new ArrayList<NavDrawerItem>();

ArrayList<NavDrawerItem> navDrawerLength = new ArrayList<NavDrawerItem>();

navDrawerBookmarked.add(new NavDrawerItem(“Bookmarked”, R.drawable.bookmark_icon, true, Integer.toString(BookmarkedCount)));

navDrawerLength.add(new NavDrawerItem(“Short”, R.drawable.icon_small, true, Integer.toString(ShortCount)));

navDrawerLength.add(new NavDrawerItem(“Medium”, R.drawable.icon_medium, true, Integer.toString(MediumCount)));

navDrawerLength.add(new NavDrawerItem(“Long”, R.drawable.icon_long, true, Integer.toString(LongCount)));

NavDrawerListAdapter navAdapterBookMarked = new NavDrawerListAdapter(getApplicationContext(), navDrawerBookmarked);

NavDrawerListAdapter navLengthAdapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerLength);

SeparatedListAdapter adapter = new SeparatedListAdapter(this);

adapter.addSection(“BookMarked”, navAdapterBookMarked);

adapter.addSection(“Length”, navLengthAdapter);

lvMenu.setAdapter(adapter);
}

public void toggleMenu(View v) {
mLayout.toggleMenu();
}

}[/java]

 

The variables BookmarkedCount, ShortCount, LongCount, MediumCount are integers that I calculate within my code. You can replace those with any integer for testing purposes.

Since each row of your sliding menu listview contains an image, text and a number, we will need to create a custom adapter. We will call this adapter NavigationDrawerListAdapter . First we will create a class that defines the structure of our row of listview. We call this class NavDrawerItem.

NavDrawerItem.java :-

 

[java]

public class NavDrawerItem{

private String title;

private int icon;

private String count = “0”;

// boolean to set visiblity of the counter

private boolean isCounterVisible = false;

public NavDrawerItem(){}

public NavDrawerItem(String title, int icon){

this.title = title;

this.icon = icon;

}

public NavDrawerItem(String title, int icon, boolean isCounterVisible, String count){

this.title = title;

this.icon = icon;

this.isCounterVisible = isCounterVisible;

this.count = count;

}

public String getTitle(){

return this.title;

}

public int getIcon(){

return this.icon;

}

public String getCount(){

return this.count;

}

public boolean getCounterVisibility(){

return this.isCounterVisible;

}

public void setTitle(String title){

this.title = title;

}

public void setIcon(int icon){

this.icon = icon;

}

public void setCount(String count){

this.count = count;

}

public void setCounterVisibility(boolean isCounterVisible){

this.isCounterVisible = isCounterVisible;

}

}

[/java]

 

Now we create a java class NavigationDrawerListAdapter and make it extend BaseAdaper. This is the adapter for each row of the sliding menu. For that first create a layout xml file and name it list_complex.xml. Copy and paste below code in xml

 

[xml]

<?xml version=”1.0″ encoding=”utf-8″?>

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”

android:layout_width=”match_parent”

android:layout_height=”48dp”

android:background=”@drawable/list_selector”>

<ImageView

android:id=”@+id/list_complex_icon”

android:layout_width=”25dp”

android:layout_height=”wrap_content”

android:layout_alignParentLeft=”true”

android:layout_marginLeft=”12dp”

android:layout_marginRight=”12dp”

android:contentDescription=”icon”

android:layout_centerVertical=”true” />

<TextView

android:id=”@+id/list_complex_title”

android:layout_width=”wrap_content”

android:layout_height=”match_parent”

android:layout_toRightOf=”@id/list_complex_icon”

android:minHeight = “15dp”

android:textSize=”20sp”

android:textColor=”#fff”

android:layout_centerVertical=”true”

android:paddingRight=”40dp”/>

<TextView android:id=”@+id/list_complex_number”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:layout_alignParentRight=”true”

android:layout_centerVertical=”true”

android:layout_marginRight=”8dp”

android:textColor=”#fff”/>

</RelativeLayout>

[/xml]

 

The above layout file will be used as a layout for our list adapter. For the drawable list_selector created above, following is the code:-

list_selector.xml :-

[xml]

<?xml version=”1.0″ encoding=”utf-8″?>

<selector xmlns:android=”http://schemas.android.com/apk/res/android” >

<item android:drawable=”@drawable/list_item_bg_normal” android:state_activated=”false”/>

<item android:drawable=”@drawable/list_item_bg_pressed” android:state_pressed=”true”/>

<item android:drawable=”@drawable/list_item_bg_pressed” android:state_activated=”true”/>

</selector>

[/xml]

 

list_item_bg_normal.xml :-

 

[xml]

<?xml version=”1.0″ encoding=”utf-8″?>

<shape xmlns:android=”http://schemas.android.com/apk/res/android”

android:shape=”rectangle”>

<gradient

android:startColor=”@color/red”

android:endColor=”@color/maroon”

android:angle=”270″ />

</shape>

[/xml]

 

list_item_bg_pressed.xml :-

[xml]

<?xml version=”1.0″ encoding=”utf-8″?>

<shape xmlns:android=”http://schemas.android.com/apk/res/android”

android:shape=”rectangle”>

<gradient

android:startColor=”@color/gray”

android:endColor=”@color/black”

android:angle=”270″ />

</shape>

[/xml]

Now copy and paste below code in your NavigationDrawerListAdapter file.

NavigationDrawerListAdapter.java :-

 

[java]

import java.util.ArrayList;

import android.app.Activity;

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.TextView;

public class NavDrawerListAdapter extends BaseAdapter {

private Context context;

private ArrayList<NavDrawerItem> navDrawerItems;

public NavDrawerListAdapter(Context context, ArrayList<NavDrawerItem> navDrawerItems){

this.context = context;

this.navDrawerItems = navDrawerItems;

}

@Override

public int getCount() {

return navDrawerItems.size();

}

@Override

public Object getItem(int position) {

return navDrawerItems.get(position);

}

@Override

public long getItemId(int position) {

return position;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

if (convertView == null) {

LayoutInflater mInflater = (LayoutInflater)

context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

convertView = mInflater.inflate(R.layout.list_complex, null);

}

ImageView imgIcon = (ImageView) convertView.findViewById(R.id.list_complex_icon);

TextView txtTitle = (TextView) convertView.findViewById(R.id.list_complex_title);

TextView txtCount = (TextView) convertView.findViewById(R.id.list_complex_number);

imgIcon.setImageResource(navDrawerItems.get(position).getIcon());

txtTitle.setText(navDrawerItems.get(position).getTitle());

// displaying count

// check whether it set visible or not

if(navDrawerItems.get(position).getCounterVisibility()){

txtCount.setText(navDrawerItems.get(position).getCount());

}else{

// hide the counter view

txtCount.setVisibility(View.GONE);

}

return convertView;

}

}

[/java]

Once we have all the data we need to fill in the sliding menu, we just need to make the adapter that will show the menu with sections (like Bookmarked, Length etc that is in the screenshot). To create sections we need to implement another adapter called SeparatedListAdapter. To create this, first create a layout xml file and name it list_header.xml

list_header.xml :-

 

[xml]

<?xml version=”1.0″ encoding=”utf-8″?>

<TextView  xmlns:android=”http://schemas.android.com/apk/res/android”

android:id=”@+id/list_header_title”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:paddingTop=”2dip”

android:paddingBottom=”2dip”

android:paddingLeft=”5dip”

android:textColor=”#fff”

android:textSize=”20sp”

/>

[/xml]

 

Now create a class and name it SeparatedListAdapter and make it extend BaseAdapter. Copy and paste the below code:

SeparatedListAdapter.java :-

 

[java]

import java.util.LinkedHashMap;
import java.util.Map;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;

public class SeparatedListAdapter extends BaseAdapter {

public final Map<String,Adapter> sections = new LinkedHashMap<String,Adapter>();
public final ArrayAdapter<String> headers;
public final static int TYPE_SECTION_HEADER = 0;

public SeparatedListAdapter(Context context) {
headers = new ArrayAdapter<String>(context, R.layout.list_header);
}

public void addSection(String section, Adapter adapter) {
this.headers.add(section);
this.sections.put(section, adapter);
}

public Object getItem(int position) {
for(Object section : this.sections.keySet()) {
Adapter adapter = sections.get(section);
int size = adapter.getCount() + 1;

// check if position inside this section
if(position == 0) return section;
if(position < size) return adapter.getItem(position – 1);

// otherwise jump into next section
position -= size;
}
return null;
}

public int getCount() {
// total together all sections, plus one for each section header
int total = 0;
for(Adapter adapter : this.sections.values())
total += adapter.getCount() + 1;
return total;
}

public int getViewTypeCount() {
// assume that headers count as one, then total all sections
int total = 1;
for(Adapter adapter : this.sections.values())
total += adapter.getViewTypeCount();
return total;
}

public int getItemViewType(int position) {
int type = 1;
for(Object section : this.sections.keySet()) {
Adapter adapter = sections.get(section);
int size = adapter.getCount() + 1;

// check if position inside this section
if(position == 0) return TYPE_SECTION_HEADER;
if(position < size) return type + adapter.getItemViewType(position – 1);

// otherwise jump into next section
position -= size;
type += adapter.getViewTypeCount();
}
return -1;
}

public boolean areAllItemsSelectable() {
return false;
}

public boolean isEnabled(int position) {
return (getItemViewType(position) != TYPE_SECTION_HEADER);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
int sectionnum = 0;
for(Object section : this.sections.keySet()) {
Adapter adapter = sections.get(section);
int size = adapter.getCount() + 1;

// check if position inside this section
if(position == 0) return headers.getView(sectionnum, convertView, parent);
if(position < size) return adapter.getView(position – 1, convertView, parent);

// otherwise jump into next section
position -= size;
sectionnum++;
}
return null;
}

@Override
public long getItemId(int position) {
return position;
}

}

[/java]

Your code should compile well now! In case of any issues.. please add to the comments and I will try to answer them..

Leave a Comment