Android sets different la s for the items in ListView

Source: Internet
Author: User

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: * sets different la s for ListView items. * For example, in this example, the first Item of ListView shows an image, and all other items display text. ** to achieve this, you need to override the * 1 getView in BaseAdapter. TypeCount () and getItemViewType (int position) methods. * 1.1 specify a total of several different items in getViewTypeCount * return 2 here. * 1.2 In getItemViewType (int position), different types are returned based on different positions. * 2 in the getView () method, set the layout of items for different types * 2.1. the code for the Type in which the current position is obtained is * currentType = getItemViewType (position ); * 2.2 set layout for items based on different types ** reference: * 1 http://blog.csdn.net/yueyue369/article/details/6115552 * 2 http://blog.sina.com.cn/s/blog_5da 93c8f0100wx4v.html * Thank you very much */public class MainActivity extends Activity {private ListView mListView; private HashMap <String, Object> mHashMap; private ArrayList <HashMap <String, Object> mArrayList; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); init ();} private void init () {mArrayList = new ArrayList <HashMap <St Ring, Object> (); 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 <String, object> (); m HashMap. put ("content", "This is --->" + I); mArrayList. add (mHashMap) ;}} private class ItemClickListenerImpl implements OnItemClickListener {@ Overridepublic 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 <? Extends Map <String,?> 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 <? Extends Map <String,?> Data, int resource, String [] from, int [] to) {this. mArrayList = data; this. resource = resource; mLayoutInflater = (LayoutInflater) context. getSystemService (context. LAYOUT_INFLATER_SERVICE);} @ Overridepublic int getCount () {if (mArrayList = null) {return 0;} else {return (mArrayList. size () + 1) ;}@overridepublic Object getItem (int position) {if (mArrayList = null) {return null;} else {if (position> 0) {return mAr RayList. get (position-1);} else {return mArrayList. get (position + 1) ;}}@ Overridepublic long getItemId (int position) {return position ;} //////////////////////////////////////// /// // @ Overridepublic int getViewTypeCount () {return TYPE_COUNT ;}@ Overridepublic int getItemViewType (int position) {if (position = 0) {return FIRST_TYPE ;}else {return OTHERS_TYPE ;}} ///////////////////////////////////// /////////////// @ Overridepublic View getView (int position, View convertView, ViewGroup parent) {View firstItemView = null; view othersItemView = null; // obtain the current position's Type 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 () {@ Overridepublic void onClick (View view) {System. out. println ("===== click first item ======") ;}}); firstItemViewHolder = new FirstItemViewHolder (); firstItemViewHolder. imageView = (ImageView) firstItemView. findViewById (R. id. imageView); firstItemView. setTag (firstIte MViewHolder);} 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) othe RsItemView. 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 private class FirstItemViewHolder {ImageView imageView ;} // ViewHolder private class OthersViewHolder {TextView textView;} of other items except the first Item ;}}


 

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.