Hello Droid Guy,
How to Create Section Header In Android.... ??
Here , I am Sharing the running code of Android Section Header.
Hope this will Helps anybody.... :)
Enjoy Coding :)
1. ScreenShots:
Code for Android Section Header :
1. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
2. SectionDemoActivity.java
How to Create Section Header In Android.... ??
Here , I am Sharing the running code of Android Section Header.
Hope this will Helps anybody.... :)
Enjoy Coding :)
1. ScreenShots:
Code for Android Section Header :
1. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mukesh.section.demo"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@android:style/Theme.Light">
<activity android:name=".SectionDemoActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.mukesh.section.demo;
import android.app.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.widget.*;
import java.util.*;
import com.mukesh.widget.*;
public class SectionDemoActivity extends Activity {
AmazingListView lsComposer;
SectionComposerAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_section_demo);
lsComposer = (AmazingListView) findViewById(R.id.lsComposer);
lsComposer.setPinnedHeaderView(LayoutInflater.from(this).inflate(
R.layout.item_composer_header, lsComposer, false));
lsComposer.setAdapter(adapter = new SectionComposerAdapter());
}
class SectionComposerAdapter extends AmazingAdapter {
List<Pair<String, List<Composer>>> all = Data.getAllData();
@Override
public int getCount() {
int res = 0;
for (int i = 0; i < all.size(); i++) {
res += all.get(i).second.size();
}
return res;
}
@Override
public Composer getItem(int position) {
int c = 0;
for (int i = 0; i < all.size(); i++) {
if (position >= c && position < c + all.get(i).second.size()) {
return all.get(i).second.get(position - c);
}
c += all.get(i).second.size();
}
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
protected void onNextPageRequested(int page) {
}
@Override
protected void bindSectionHeader(View view, int position,
boolean displaySectionHeader) {
if (displaySectionHeader) {
view.findViewById(R.id.header).setVisibility(View.VISIBLE);
TextView lSectionTitle = (TextView) view
.findViewById(R.id.header);
lSectionTitle
.setText(getSections()[getSectionForPosition(position)]);
} else {
view.findViewById(R.id.header).setVisibility(View.GONE);
}
}
@Override
public View getAmazingView(int position, View convertView,
ViewGroup parent) {
View res = convertView;
if (res == null)
res = getLayoutInflater().inflate(R.layout.item_composer, null);
TextView lName = (TextView) res.findViewById(R.id.lName);
TextView lYear = (TextView) res.findViewById(R.id.lYear);
Composer composer = getItem(position);
lName.setText(composer.name);
lYear.setText(composer.code);
return res;
}
@Override
public void configurePinnedHeader(View header, int position, int alpha) {
TextView lSectionHeader = (TextView) header;
lSectionHeader
.setText(getSections()[getSectionForPosition(position)]);
lSectionHeader.setBackgroundColor(alpha << 24 | (0x000000));
lSectionHeader.setTextColor(alpha << 24 | (0xffffff));
}
@Override
public int getPositionForSection(int section) {
if (section < 0)
section = 0;
if (section >= all.size())
section = all.size() - 1;
int c = 0;
for (int i = 0; i < all.size(); i++) {
if (section == i) {
return c;
}
c += all.get(i).second.size();
}
return 0;
}
@Override
public int getSectionForPosition(int position) {
int c = 0;
for (int i = 0; i < all.size(); i++) {
if (position >= c && position < c + all.get(i).second.size()) {
return i;
}
c += all.get(i).second.size();
}
return -1;
}
@Override
public String[] getSections() {
String[] res = new String[all.size()];
for (int i = 0; i < all.size(); i++) {
res[i] = all.get(i).first;
}
return res;
}
}
}
3. Data.java
package com.mukesh.section.demo;
import android.os.*;
import android.util.*;
import java.util.*;
public class Data {
public static final String TAG = Data.class.getSimpleName();
public static List<Pair<String, List<Composer>>> getAllData() {
List<Pair<String, List<Composer>>> res = new ArrayList<Pair<String, List<Composer>>>();
for (int i = 0; i < 4; i++) {
res.add(getOneSection(i));
}
return res;
}
public static List<Composer> getFlattenedData() {
List<Composer> res = new ArrayList<Composer>();
for (int i = 0; i < 4; i++) {
res.addAll(getOneSection(i).second);
}
return res;
}
public static Pair<Boolean, List<Composer>> getRows(int page) {
List<Composer> flattenedData = getFlattenedData();
if (page == 1) {
return new Pair<Boolean, List<Composer>>(true,
flattenedData.subList(0, 5));
} else {
SystemClock.sleep(2000); // simulate loading
return new Pair<Boolean, List<Composer>>(
page * 5 < flattenedData.size(), flattenedData.subList(
(page - 1) * 5,
Math.min(page * 5, flattenedData.size())));
}
}
public static Pair<String, List<Composer>> getOneSection(int index) {
String[] titles = { "Assam", "Goa", "Jharkhand", "Maharashtra",
"Uttar Pradesh" };
Composer[][] composerss = {
{ new Composer("Abhayapuri", "3669"),
new Composer("Barama", "3623"),
new Composer("Bihupuria", "3759"),
new Composer("Bijni", "3668"), },
{ new Composer("Canacona", "8346"),
new Composer("Margaon", "8342"),
new Composer("Panji", "832"),
new Composer("Ponda", "8343"), },
{ new Composer("Basia", "6533"),
new Composer("Bokaro", "6542"),
new Composer("Chaibasa", "6582"),
new Composer("Chandil", "6591"), },
{ new Composer("Achalpur", "7223"),
new Composer("Ahmednagar", "241"),
new Composer("Ajara", "2323"),
new Composer("Akola", "724"), },
{ new Composer("Agra", "562"),
new Composer("Akbarpur", "5111"),
new Composer("Aligarh", "571"),
new Composer("Allahabad", "532"), }, };
return new Pair<String, List<Composer>>(titles[index],
Arrays.asList(composerss[index]));
}
}
Download the full running source code: