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描述: ? * 为ListView的Item设置不同的布局. ? * 例如在该例子中ListView的第一个Item显示一张 ? * 图片,其余的Item都显示文字. ? *? ? * 为了达到此目的需要重写BaseAdapter中的 ? * 1 getViewTypeCount()和getItemViewType(int position)方法. ? *?? 1.1在getViewTypeCount中指定一共有几种不同的item ? *????? 在此返回2即可. ? *?? 1.2在getItemViewType(int position)中需要依据position的不同 ? *????? 返回不同的Type. ? * 2 在getView()方法中针对不同的Type为Item设置布局 ? *?? 2.1得到当前位置(position)时的Type即代码: ? *?????? currentType= getItemViewType(position); ? *?? 2.2依据Type的不同为Item设置布局 ? *? ? * 参考资料: ? * 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<String, Object> mHashMap;? ???? private ArrayList<HashMap<String, Object>> mArrayList;? ???? @Override ????? protected void onCreate(Bundle savedInstanceState) {? ???????? super .onCreate(savedInstanceState);? ???????? setContentView(R.layout.main);? ???????? init();? ???? }? ???? private void init(){? ???????? mArrayList= new ArrayList<HashMap<String,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>();? ???????????? 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);? ?? ????????? }? ???? }? } |
?
?
ListViewAdapter如下:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | 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);? ???? }? ?????? ????? @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 ;? ???????? //获取到当前位置所对应的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() {? ???????????????????? @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;? ???? }? ?????? ????? ????? //第一个Item的ViewHolder? ???? private class FirstItemViewHolder{? ???????? ImageView imageView;? ???? }? ?????? ????? //除第一个Item以外其余Item的ViewHolder? ???? private class OthersViewHolder{? ???????? TextView textView;? ???? }? }? |