activity_main.xml
MainActivity.java
note: must use library for that demo code ok for above logic .<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <com.hb.views.PinnedSectionListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/lll"></com.hb.views.PinnedSectionListView> </RelativeLayout>
MainActivity.java
package com.example.pinnedlistview; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.TextView; import android.widget.Toast; import com.hb.views.PinnedSectionListView; import com.hb.views.PinnedSectionListView.PinnedSectionListAdapter; public class MainActivity extends Activity { PinnedSectionListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (PinnedSectionListView)findViewById(R.id.lll); lv.setAdapter(new MyPinnedSectionListAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, prepareItems())); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(MainActivity.this,"POsition : "+arg2,Toast.LENGTH_SHORT).show(); } }); } private static class MyPinnedSectionListAdapter extends ArrayAdapter<Item> implements PinnedSectionListAdapter { private static final int[] COLORS = new int[] { android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_blue_light, android.R.color.holo_red_light }; public MyPinnedSectionListAdapter(Context context, int resource, int textViewResourceId, List<Item> objects) { super(context, resource, textViewResourceId, objects); } @Override public View getView(int position, View convertView, ViewGroup parent) { TextView view = (TextView) super.getView(position, convertView, parent); view.setTextColor(Color.DKGRAY); if (getItem(position).type == Item.SECTION) { view.setBackgroundColor(parent.getResources().getColor(COLORS[position % COLORS.length])); } return view; } @Override public int getViewTypeCount() { return 2; } @Override public int getItemViewType(int position) { return getItem(position).type; } @Override public boolean isItemViewTypePinned(int viewType) { return viewType == Item.SECTION; } } private static class Item { public static final int ITEM = 0; public static final int SECTION = 1; public final int type; public final String text; public Item(int type, String text) { this.type = type; this.text = text; } @Override public String toString() { return text; } } private static ArrayList<Item> prepareItems() { ArrayList<Item> result = new ArrayList<Item>(); for (int i = 0; i < 30; i++) { result.add(new Item(Item.SECTION, "Section " + i)); for (int j=0; j<4; j++) { result.add(new Item(Item.ITEM, "Item " + j)); } } return result; } }