Android sets different la s for the items in ListView

Source: Internet
Author: User

Android sets different la s for the items in ListView

MainActivity is as follows:

Package cc. testlistview;

Import java. util. ArrayList;

Import java. util. HashMap;

Import android. OS. Bundle;

Import android. view. View;

Import android. widget. AdapterView;

Import android. widget. AdapterView. OnItemClickListener;

Import android. widget. ListView;

Import android. app. Activity;

/**

* Demo description:

* Set different la s for the items in ListView.

* For example, in this example, the first Item of ListView displays

* The image and other items are displayed in text.

*

* To achieve this, you need to rewrite

* 1 getViewTypeCount () and getItemViewType (int position) methods.

* 1.1 specify several different items in getViewTypeCount.

* 2 is returned here.

* 1.2 In getItemViewType (int position), the position must be different.

* Different types are returned.

* 2 set the layout for items of different types in the getView () method

* 2.1 The Type code when the current position is obtained:

* CurrentType = getItemViewType (position );

* 2.2 set layout for items based on different types

*

* References:

* 1 http://blog.csdn.net/yueyue369/article/details/6115552

* 2 http://blog.sina.com.cn/s/blog_5da93c8f0100wx4v.html

* Thank you very much

*/

Public class MainActivity extends Activity {

Private ListView mListView;

Private HashMap

Private ArrayList

@ Override

Protected void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

Init ();

}

Private void init (){

MArrayList = new ArrayList

MListView = (ListView) findViewById (R. id. listview );

AddDataForListView ();

MListView. setAdapter

(New ListViewAdapter (MainActivity. this, mArrayList, R. layout. othersitem, new String [] {"content"}, new int [] {R. id. textView }));

MListView. setOnItemClickListener (new ItemClickListenerImpl ());

}

Private void addDataForListView (){

For (int I = 0; I <30; I ++ ){

MHashMap = new HashMap

MHashMap. put ("content", "This is --->" + I );

MArrayList. add (mHashMap );

}

}

Private class ItemClickListenerImpl implements OnItemClickListener {

@ Override

Public void onItemClick (AdapterView parent, View view, int position, long arg ){

System. out. println ("OnItemClickListener position =" + position );

}

}

}

The ListViewAdapter is as follows:

Package cc. testlistview;

Import java. util. List;

Import java. util. Map;

Import android. content. Context;

Import android. view. LayoutInflater;

Import android. view. View;

Import android. view. View. OnClickListener;

Import android. view. ViewGroup;

Import android. widget. BaseAdapter;

Import android. widget. ImageView;

Import android. widget. TextView;

Public class ListViewAdapter extends BaseAdapter {

Private List> mArrayList;

Private int resource;

Private LayoutInflater mLayoutInflater;

Private final int TYPE_COUNT = 2;

Private final int FIRST_TYPE = 0;

Private final int OTHERS_TYPE = 1;

Private int currentType;

Public ListViewAdapter (Context context, List> data, int resource, String [] from, int [] ){

This. mArrayList = data;

This. resource = resource;

MLayoutInflater = (LayoutInflater) context. getSystemService (context. LAYOUT_INFLATER_SERVICE );

}

@ Override

Public int getCount (){

If (mArrayList = null ){

Return 0;

} Else {

Return (mArrayList. size () + 1 );

}

}

@ Override

Public Object getItem (int position ){

If (mArrayList = null ){

Return null;

} Else {

If (position> 0 ){

Return mArrayList. get (position-1 );

} Else {

Return mArrayList. get (position + 1 );

}

}

}

@ Override

Public long getItemId (int position ){

Return position;

}

//////////////////////////////////////// ///////////////

@ Override

Public int getViewTypeCount (){

Return TYPE_COUNT;

}

@ Override

Public int getItemViewType (int position ){

If (position = 0 ){

Return FIRST_TYPE;

} Else {

Return OTHERS_TYPE;

}

}

//////////////////////////////////////// ///////////////

@ Override

Public View getView (int position, View convertView, ViewGroup parent ){

View firstItemView = null;

View othersItemView = null;

// Obtain the Type corresponding to the current position

CurrentType = getItemViewType (position );

System. out. println ("type =" + currentType );

If (currentType = FIRST_TYPE ){

FirstItemView = convertView;

FirstItemViewHolder firstItemViewHolder = null;

If (firstItemView = null ){

System. out. println ("firstItemView = null ");

FirstItemView = mLayoutInflater. inflate (R. layout. firstitem, null );

FirstItemView. setOnClickListener (new OnClickListener (){

@ Override

Public void onClick (View view ){

System. out. println ("===== click first item ===== ");

}

});

FirstItemViewHolder = new FirstItemViewHolder ();

FirstItemViewHolder. imageView = (ImageView) firstItemView. findViewById (R. id. imageView );

FirstItemView. setTag (firstItemViewHolder );

} Else {

System. out. println ("firstItemView! = Null ");

System. out. println ("111 getClass =" + firstItemView. getTag (). getClass (). toString ());

FirstItemViewHolder = (FirstItemViewHolder) firstItemView. getTag ();

}

If (firstItemViewHolder. imageView! = Null ){

FirstItemViewHolder. imageView. setImageResource (R. drawable. ic_launcher );

}

ConvertView = firstItemView;

} Else {

OthersItemView = convertView;

OthersViewHolder othersViewHolder = null;

If (othersItemView = null ){

System. out. println ("othersItemView = null ");

OthersItemView = mLayoutInflater. inflate (R. layout. othersitem, null );

OthersViewHolder = new OthersViewHolder ();

OthersViewHolder. textView = (TextView) othersItemView. findViewById (R. id. textView );

OthersItemView. setTag (othersViewHolder );

} Else {

System. out. println ("othersItemView! = Null ");

System. out. println ("222 getClass =" + othersItemView. getTag (). getClass (). toString ());

OthersViewHolder = (OthersViewHolder) othersItemView. getTag ();

}

If (mArrayList! = Null ){

If (othersViewHolder. textView! = Null ){

OthersViewHolder. textView. setText (String) (mArrayList. get (position-1). get ("content ")));

}

}

ConvertView = othersItemView;

}

Return convertView;

}

// ViewHolder of the first Item

Private class FirstItemViewHolder {

ImageView imageView;

}

// ViewHolder of other items except the first Item

Private class OthersViewHolder {

TextView textView;

}

}

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.