Hi Guys,
Today we are learning about the section header in android.
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.
You can download the source code Section Header ListView.
Cheers Guys!
Today we are learning about the section header in android.
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.
layout/main.xml
layout/list_item_section.xml
layout/list_item_enty.xml
Item.java
package com.sunil.util;
public interface Item {
public boolean isSection();
}
SectionItem.java
package com.sunil.util;
public class SectionItem implements Item{
private final String title;
public SectionItem(String title) {
this.title = title;
}
public String getTitle(){
return title;
}
@Override
public boolean isSection() {
return true;
}
}
EntryItem.java
package com.sunil.util;
public class EntryItem implements Item{
public final String title;
public final String subtitle;
public EntryItem(String title, String subtitle) {
this.title = title;
this.subtitle = subtitle;
}
@Override
public boolean isSection() {
return false;
}
}
EntryAdapter.java
package com.sunil.util;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.sunil.sectionlist.R;
public class EntryAdapter extends ArrayAdapter- {
private Context context;
private ArrayList- items;
private LayoutInflater vi;
public EntryAdapter(Context context,ArrayList- items) {
super(context,0, items);
this.context = context;
this.items = items;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
final Item i = items.get(position);
if (i != null) {
if(i.isSection()){
SectionItem si = (SectionItem)i;
v = vi.inflate(R.layout.list_item_section, null);
v.setOnClickListener(null);
v.setOnLongClickListener(null);
v.setLongClickable(false);
final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
sectionView.setText(si.getTitle());
}else{
EntryItem ei = (EntryItem)i;
v = vi.inflate(R.layout.list_item_entry, null);
final TextView title = (TextView)v.findViewById(R.id.list_item_entry_title);
final TextView subtitle = (TextView)v.findViewById(R.id.list_item_entry_summary);
if (title != null)
title.setText(ei.title);
if(subtitle != null)
subtitle.setText(ei.subtitle);
}
}
return v;
}
}
SectionListExampleActivity.java
package com.sunil.sectionactivity;
import java.util.ArrayList;
import com.sunil.util.EntryAdapter;
import com.sunil.util.EntryItem;
import com.sunil.util.Item;
import com.sunil.util.SectionItem;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
import com.sunil.sectionlist.R;
public class SectionListExampleActivity extends Activity implements OnItemClickListener{
ArrayList- items = new ArrayList
- ();
ListView listview=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listview=(ListView)findViewById(R.id.listView_main);
items.add(new SectionItem("My Friends"));
items.add(new EntryItem("Abhi Tripathi", "Champpu"));
items.add(new EntryItem("Sandeep Pal", "Sandy kaliya"));
items.add(new EntryItem("Amit Verma", "Budhiya"));
items.add(new EntryItem("Awadhesh Diwaker ", "Dadda"));
items.add(new SectionItem("Android Version"));
items.add(new EntryItem("Jelly Bean", "android 4.2"));
items.add(new EntryItem("IceCream Sandwich", "android 4.0"));
items.add(new EntryItem("Honey Comb", "android 3.0"));
items.add(new EntryItem("Ginger Bread ", "android 2.2"));
items.add(new SectionItem("Android Phones"));
items.add(new EntryItem("Samsung", "Gallexy"));
items.add(new EntryItem("Sony Ericson", "Xperia"));
items.add(new EntryItem("Nokiya", "Lumia"));
EntryAdapter adapter = new EntryAdapter(this, items);
listview.setAdapter(adapter);
listview.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView arg0, View arg1, int position, long arg3) {
EntryItem item = (EntryItem)items.get(position);
Toast.makeText(this, "You clicked " + item.title , Toast.LENGTH_SHORT).show();
}
}
Cheers Guys!
Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com
atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :