Grouping Implementation of Listview in android

Source: Internet
Author: User
Tags addall

We are not familiar with the Listview grouping, because the contact information in the address book that comes with Android is the ListView grouping. This function has been used in recent projects. Therefore, we hope to be helpful when we have time to update the next blog over the weekend.

In fact, there is no big difference between the grouped ListView and the ListView we usually use, that is, we need to make a judgment in the getView method in the adapter. As long as you understand this, let's talk about it. Let's look at the implementation code below.

First, the main. xml layout:

[Html] <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: background = "# ffffff"
Android: orientation = "vertical">
 
<ListView
Android: id = "@ + id/listView_list"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content">
</ListView>
 
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: background = "# ffffff"
Android: orientation = "vertical">

<ListView
Android: id = "@ + id/listView_list"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content">
</ListView>

</LinearLayout>
Because listview needs to load two different items, so we need to implement two item la S, addexam_list_item.xml:

[Html] <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: orientation = "horizontal"
Android: padding = "5dip">

<ImageView
Android: id = "@ + id/addexam_list_icon"
Android: background = "@ drawable/ic_launcher"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
<TextView
Android: id = "@ + id/addexam_list_item_text"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center"
Android: layout_marginLeft = "10dp"
Android: text = "Test Data"/>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: orientation = "horizontal"
Android: padding = "5dip">

<ImageView
Android: id = "@ + id/addexam_list_icon"
Android: background = "@ drawable/ic_launcher"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
<TextView
Android: id = "@ + id/addexam_list_item_text"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center"
Android: layout_marginLeft = "10dp"
Android: text = "Test Data"/>
</LinearLayout>
Layout addexam_list_item_tag.xml corresponding to group tags

[Html] <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: background = "#666666"
Android: paddingLeft = "10dp"
Android: gravity = "center_vertical"
Android: orientation = "vertical">

<TextView
Android: id = "@ + id/addexam_list_item_text"
Android: layout_width = "wrap_content"
Android: layout_height = "20dip"
Android: textColor = "# ffffff"
Android: text = "financial examination"
Android: gravity = "center_vertical"/>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: background = "#666666"
Android: paddingLeft = "10dp"
Android: gravity = "center_vertical"
Android: orientation = "vertical">

<TextView
Android: id = "@ + id/addexam_list_item_text"
Android: layout_width = "wrap_content"
Android: layout_height = "20dip"
Android: textColor = "# ffffff"
Android: text = "financial examination"
Android: gravity = "center_vertical"/>
</LinearLayout>
The layout file has been implemented. Let's take a look at how we handle it in the program!

[Html] public class TestActivity extends Activity {
/** Called when the activity is first created .*/
Private List <String> list = null;
Private List <String> groupkey = new ArrayList <String> ();
Private List <String> aList = new ArrayList <String> ();
Private List <String> bList = new ArrayList <String> ();
Private ListView listview;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

Listview = (ListView) findViewById (R. id. listView_list );
InitData ();
MyAdapter adapter = new MyAdapter ();
Listview. setAdapter (adapter );

}
Public void initData (){
List = new ArrayList <String> ();

Groupkey. add ("group ");
Groupkey. add ("group B ");

For (int I = 0; I <5; I ++ ){
AList. add ("group A" + I );
}
List. add ("group ");
List. addAll (aList );

For (int I = 0; I <8; I ++ ){
BList. add ("group B" + I );
}
List. add ("group B ");
List. addAll (bList );
}

Private class MyAdapter extends BaseAdapter {
 
@ Override
Public int getCount (){
// TODO Auto-generated method stub
Return list. size ();
}
 
@ Override
Public Object getItem (int position ){
// TODO Auto-generated method stub
Return list. get (position );
}
 
@ Override
Public long getItemId (int position ){
// TODO Auto-generated method stub
Return position;
}
@ Override
Public boolean isEnabled (int position ){
// TODO Auto-generated method stub
If (groupkey. contains (getItem (position ))){
Return false;
}
Return super. isEnabled (position );
}
@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
// TODO Auto-generated method stub
View view = convertView;
If (groupkey. contains (getItem (position ))){
View = LayoutInflater. from (getApplicationContext (). inflate (R. layout. addexam_list_item_tag, null );
} Else {
View = LayoutInflater. from (getApplicationContext (). inflate (R. layout. addexam_list_item, null );
}
TextView text = (TextView) view. findViewById (R. id. addexam_list_item_text );
Text. setText (CharSequence) getItem (position ));
Return view;
}

}
}
Public class TestActivity extends Activity {
/** Called when the activity is first created .*/
Private List <String> list = null;
Private List <String> groupkey = new ArrayList <String> ();
Private List <String> aList = new ArrayList <String> ();
Private List <String> bList = new ArrayList <String> ();
Private ListView listview;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

Listview = (ListView) findViewById (R. id. listView_list );
InitData ();
MyAdapter adapter = new MyAdapter ();
Listview. setAdapter (adapter );

}
Public void initData (){
List = new ArrayList <String> ();

Groupkey. add ("group ");
Groupkey. add ("group B ");

For (int I = 0; I <5; I ++ ){
AList. add ("group A" + I );
}
List. add ("group ");
List. addAll (aList );

For (int I = 0; I <8; I ++ ){
BList. add ("group B" + I );
}
List. add ("group B ");
List. addAll (bList );
}

Private class MyAdapter extends BaseAdapter {

@ Override
Public int getCount (){
// TODO Auto-generated method stub
Return list. size ();
}

@ Override
Public Object getItem (int position ){
// TODO Auto-generated method stub
Return list. get (position );
}

@ Override
Public long getItemId (int position ){
// TODO Auto-generated method stub
Return position;
}
@ Override
Public boolean isEnabled (int position ){
// TODO Auto-generated method stub
If (groupkey. contains (getItem (position ))){
Return false;
}
Return super. isEnabled (position );
}
@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
// TODO Auto-generated method stub
View view = convertView;
If (groupkey. contains (getItem (position ))){
View = LayoutInflater. from (getApplicationContext (). inflate (R. layout. addexam_list_item_tag, null );
} Else {
View = LayoutInflater. from (getApplicationContext (). inflate (R. layout. addexam_list_item, null );
}
TextView text = (TextView) view. findViewById (R. id. addexam_list_item_text );
Text. setText (CharSequence) getItem (position ));
Return view;
}

}
}
The Code seems quite simple, and we usually use lsitview. Let's see if it can be implemented.

Run:






From the wangkuifeng0118 Column
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.