Sectioned Listview

Introduction
Listview are commonly used to display a large set of similar data. But in the section header list we can show the similar data list in the different and separate section of the list view.
Here I am using the adapter class for loading the view of list one by one by using get view method.
So lets start the coding about the section header.
demo

Using this code

activity_main.xml:

Declare only basic listview
<RelativeLayout   xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >

<ListView
  android:id="@+id/listView1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" >
</ListView>

</RelativeLayout>

menuitem_selectiontext.xml


Here to declare listview section header 
<?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="wrap_content"
  android:background="#FF0040" >

<TextView
  android:id="@+id/section_tv"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:textAppearance="?android:attr/textAppearanceLarge"
  android:textSize="20dp"
  android:textColor="#F9F8F4" />

</RelativeLayout>

menuitems_adapter.xml

Here to declare listview sections inside texts and images 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/RelativeLayout1"
  android:layout_width="fill_parent"
  android:layout_height="match_parent"
  android:background="#5882FA"
  android:orientation="vertical" >

<ImageView
  android:id="@+id/listview_imgview"
  android:layout_width="40dp"
  android:layout_height="40dp"
  android:layout_marginRight="5dp"
  android:contentDescription="@string/app_name"
  android:padding="2dp"
  android:visibility="visible" />

<TextView
  android:id="@+id/listview_txtview"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_alignParentRight="true"
  android:layout_centerVertical="true"
  android:layout_toRightOf="@+id/listview_imgview"
  android:gravity="left"
  android:textAppearance="?android:attr/textAppearanceLarge"
  android:textColor="#F9F8F4"
  android:textSize="20dp" />

</RelativeLayout>

MainActivity.java
package com.example.sectionedlistview;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity 
{
 ListView listview;
 Adapter adapter;
 Context context;
 String[] listview_menu_items={"Start","Search  Profiles","Favourites","Messages","Visits","Events","Blogs","My  Profile","Edit Profile","Edit Profile Photo","My Events","User  Guide","Log Out","Terms of services","Prices"};
 @Override
 protected void onCreate(Bundle savedInstanceState) 
 {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.activity_main);

  listview = (ListView) findViewById(R.id.listView1);
  context=this;
  adapter=new Adapter(this);
  adapter.addSeparatorItem("Go to..");
  for (int i = 0; i <= 14; i++)
  {
  if (i==6) 
  {
   adapter.addSeparatorItem("Profile");
  }
  if (i==11) 
  {
   adapter.addSeparatorItem("Support");
  }
  if (i==13) 
  {
   adapter.addSeparatorItem("Subscription");
  }
  adapter.addItem(listview_menu_items[i]);
 }
 listview.setAdapter(adapter);
 listview.setOnItemClickListener(new OnItemClickListener() 
 {
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int          position,long id) 
  {
   // TODO Auto-generated method stub
   onMenuItemClick(parent, view, position, id);
   listview.smoothScrollToPosition(0);
  }
 });
}

//used to declare clickable listview items
private void onMenuItemClick(AdapterView<?> parent, View view,
int position, long id) 
{
 String selectedItem = adapter.mItems.get(position);
 if (selectedItem.compareTo("Start") == 0)
 {
  Toast.makeText(getApplicationContext(), selectedItem,                   Toast.LENGTH_SHORT).show();
 }
 else if (selectedItem.compareTo("Search Profiles") == 0)
 {
  Toast.makeText(getApplicationContext(),selectedItem,                   Toast.LENGTH_SHORT).show();
 }
 else if (selectedItem.compareTo("Favourites") == 0)
 {
  Toast.makeText(getApplicationContext(),selectedItem,                   Toast.LENGTH_SHORT).show();
 }
else if (selectedItem.compareTo("Messages") == 0)
{
  Toast.makeText(getApplicationContext(),selectedItem,                   Toast.LENGTH_SHORT).show();
}
else if (selectedItem.compareTo("Visits") == 0)
{
 Toast.makeText(getApplicationContext(),selectedItem,                  Toast.LENGTH_SHORT).show();
}
else if (selectedItem.compareTo("Events") == 0)
{
 Toast.makeText(getApplicationContext(),selectedItem,                Toast.LENGTH_SHORT).show();
}
else if (selectedItem.compareTo("Blogs") == 0)
{
 Toast.makeText(getApplicationContext(),selectedItem,                Toast.LENGTH_SHORT).show();
}
else if (selectedItem.compareTo("My Profile") == 0)
{
 Toast.makeText(getApplicationContext(),selectedItem,                  Toast.LENGTH_SHORT).show();
}
else if (selectedItem.compareTo("Edit Profile") == 0)
{
 Toast.makeText(getApplicationContext(),selectedItem,                    Toast.LENGTH_SHORT).show();
}
}
}

Adapter.java
package com.example.sectionedlistview;

import java.util.ArrayList;
import java.util.TreeSet;
import android.content.Context;
import android.util.Log;
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 Adapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
public static ArrayList<String> mItems = new ArrayList<String>();
private LayoutInflater mInflater;
int[] listview_images={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
R.drawable.ic_launcher,R.drawable.ic_launcher};
private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
public Adapter(MainActivity profileActivity)
{
// TODO Auto-generated constructor stub
mInflater = (LayoutInflater)profileActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mItems.clear();
mSeparatorsSet.clear();
}

public void addItem(final String item) 
{
mItems.add(item);
notifyDataSetChanged();
Log.d("mItems 1 = ", ""+mItems.size());
}

public void addSeparatorItem(final String item)
{
mItems.add(item);
mSeparatorsSet.add(mItems.size() - 1);
notifyDataSetChanged();
Log.d("mItems 2 = ", ""+mItems.size());
Log.d("mItems 3 = ", ""+mSeparatorsSet.size());
}

@Override
public int getItemViewType(int position) 
{
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}

@Override
public int getViewTypeCount() 
{
return TYPE_MAX_COUNT;
}

@Override
public int getCount() 
{
return mItems.size();
}

@Override
public String getItem(int position) 
{
return mItems.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
ViewHolder holder = null;
int type = getItemViewType(position);
System.out.println("getView " + position + " " + convertView + " type = " + type);
if (convertView == null
{
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.menuitems_adapter, null);
holder.adapter_textview = (TextView)convertView.findViewById(R.id.listview_txtview);
holder.adapter_imageview=(ImageView)convertView.findViewById(R.id.listview_imgview);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.menuitem_selectiontext, null);
holder.adapter_textview = (TextView)convertView.findViewById(R.id.section_tv);
break;
}
convertView.setTag(holder);


else 
{
holder = (ViewHolder)convertView.getTag();
}
if(type==0){
holder.adapter_imageview.setBackgroundResource(listview_images[position]);
holder.adapter_textview.setText(mItems.get(position));
Log.d("TYPE ITEM", mItems.get(position));
Log.d("TYPE", String.valueOf(type));
}else{
holder.adapter_textview.setText(mItems.get(position));
Log.d("TYPE_SEPARATOR", mItems.get(position));
Log.d("TYPE", String.valueOf(type));
}

return convertView;
}
public static class ViewHolder {
public TextView adapter_textview;
public ImageView adapter_imageview;
}
}

                                                     
Previous
Next Post »

2 comments

Write comments
Unknown
AUTHOR
January 31, 2015 at 4:40 PM delete

In sectioned list view
I am facing problem in this code
Can u mail me project in zip file format at richasoni1807@gmail.com



context = this;
adapter=new Adapter(this);
adapter.addSeparatorItem("Go to..");
for (int i = 0; i <= 14; i++) {
if (i == 6) {
adapter.addSeparatorItem("Profile");
}
if (i == 11) {
adapter.addSeparatorItem("Support");
}
if (i == 13) {
adapter.addSeparatorItem("Subscription");
}
adapter.addItem(listview_menu_items[i]);
}
listview.setAdapter(adapter);





// used to declare clickable listview items
private void onMenuItemClick(AdapterView parent, View view,
int position, long id) {

String selectedItem = adapter.mItems.get(position);
if (selectedItem.compareTo("Start") == 0) {

Reply
avatar
Kalidoss
AUTHOR
February 2, 2015 at 10:27 AM delete

Hi richa soni,

Already i have posted full project codes in my Github account

Reply
avatar