形如手机的设计界面,有seekbar或者button,此次我用了三种形式,但是不知道在onCreated中如何作显示,写了,基本都FC。
class MyAdapter extends BaseAdapter{ Context mContext; //LinearLayout linearLayout = null; LayoutInflater inflater; //TextView tex; final int TYPE_SWITCH = 0; final int TYPE_SEEKBAR = 1; final int TYPE_BUTTON = 2; final int TYPE_CHECKBOX = 3; public MyAdapter(Context context) { mContext = context; inflater = LayoutInflater.from(mContext); //inflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { // TODO Auto-generated method stub return mData.size();} //每个convert view都会调用此方法,获得当前所需要的view样式 @Override public int getItemViewType(int position) { // TODO Auto-generated method stub int p = position%4; if(p == 0) return TYPE_SWITCH; else if(p == 1) return TYPE_SEEKBAR; else if(p == 4) return TYPE_SEEKBAR_RGB; else if(p == 2) return TYPE_BUTTON; else if(p == 3) return TYPE_CHECKBOX; else return TYPE_SEEKBAR; } @Override public int getViewTypeCount() { // TODO Auto-generated method stub return 1; } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return mData.get(arg0); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub viewHolder_switch holder_switch = null; viewHolder_seekbar holder_seekbar = null; viewHolder_button holder_button = null; int type = getItemViewType(position); if (convertView == null) { Log.e("convertView = ", " NULL"); switch(type) { case TYPE_SWITCH: holder_switch = new viewHolder_switch(); convertView = inflater.inflate(R.layout.listview_switch_row, parent, false); // holder_switch.img = (ImageView)convertView.findViewById(R.id.img_sw); holder_switch.title = (TextView)convertView.findViewById(R.id.title_sw); holder_switch.info = (TextView)convertView.findViewById(R.id.info_sw); Log.e("convertView = ", "NULL TYPE_SWITCH"); //Map<String, Object> myItem = getItem(position); holder_switch.img.setBackgroundResource(R.drawable.power_ico_50dp); holder_switch.title.setText(led_opt[position]); holder_switch.info.setText(led_optContext[position]); holder_switch.Switch.setChecked(true); convertView.setTag(holder_switch); break; case TYPE_SEEKBAR: convertView = inflater.inflate(R.layout.listview_seekbar_row, parent, false); holder_seekbar = new viewHolder_seekbar(); holder_seekbar.img = (ImageView)convertView.findViewById(R.id.img_sb); holder_seekbar.title = (TextView)convertView.findViewById(R.id.title_sb); holder_seekbar.info = (TextView)convertView.findViewById(R.id.info_sb); Log.e("convertView = ", "NULL TYPE_SEEKBAR"); holder_seekbar.img.setBackgroundResource(R.drawable.rgb_ico_50dp); holder_seekbar.title.setText(led_opt[position]); holder_seekbar.info.setText(led_optContext[position]); convertView.setTag(holder_seekbar); break; case TYPE_BUTTON: convertView = inflater.inflate(R.layout.listview_button_row, parent, false); holder_button = new viewHolder_button(); holder_button.img = (ImageView)convertView.findViewById(R.id.img_btn); holder_button.title = (TextView)convertView.findViewById(R.id.title_btn); holder_button.info = (TextView)convertView.findViewById(R.id.info_btn); Log.e("convertView = ", "NULL TYPE_BUTTON"); holder_button.img.setBackgroundResource(R.drawable.dream_ico_50dp); holder_button.title.setText(led_opt[position]); holder_button.info.setText(led_optContext[position]); convertView.setTag(holder_button); break; } }else{ //有convertView,按样式,取得不用的布局 switch(type) { case TYPE_SWITCH: holder_switch = (viewHolder_switch) convertView.getTag(); Log.e("convertView !!!!!!= ", "NULL TYPE_SWITCH"); break; case TYPE_SEEKBAR: holder_seekbar = (viewHolder_seekbar) convertView.getTag(); Log.e("convertView !!!!!!= ", "NULL TYPE_SEEKBAR"); break; case TYPE_BUTTON: holder_button = (viewHolder_button) convertView.getTag(); Log.e("convertView !!!!!!= ", "NULL TYPE_BUTTON"); break; } } return convertView; } } class viewHolder_switch{ /** 图片ID */ private ImageView img; /** 标题 */ private TextView title; /** 内容 */ private TextView info; /** switch*/ private Switch Switch; } class viewHolder_seekbar{ /** 图片ID */ private ImageView img; /** 标题 */ private TextView title; /** 内容 */ private TextView info; private SeekBar sb; } class viewHolder_button { private ImageView img; /** 标题 */ private TextView title; /** 内容 */ private TextView info; /** 按钮名称 */ private String btnText; }
MyAdapter listAdapter ; protected void onCreate(Bundle savedInstanceState) { //setTheme(16974123); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); { ListView lv = (ListView) this.findViewById(R.id.listview_home); listAdapter = new MyAdapter(this); lv.setAdapter(listAdapter); } }
现在的问题就是如何使这个list包含三种布局并显示,不知道如何写,参考了不少,很多都是每个item都一致的布局,这个不一样,就不知如何下手了。
getViewTypeCount如果为3,就可以放置3个不同的item。
同时这getView的时候,区分这三种Item,分别加载不同的xml文件即可。
这有个例子
http-://download.csdn-.net/detail/piaohong/5020778