Chinaunix首页 | 论坛 | 博客
  • 博客访问: 236309
  • 博文数量: 54
  • 博客积分: 2656
  • 博客等级: 少校
  • 技术积分: 1020
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-19 21:06
文章分类

全部博文(54)

文章存档

2016年(3)

2014年(8)

2013年(4)

2012年(2)

2011年(29)

2010年(8)

我的朋友

分类: Java

2012-04-09 15:18:02

效果图片:
 
 
 
自定义listview控件

点击(此处)折叠或打开

  1. public class SettingListView extends ListView {

  2.     public ArrayList<HashMap<String, List<SettingItem>>> mSettingList = new ArrayList<HashMap<String, List<SettingItem>>>();
  3.     protected LayoutInflater mInflater;

  4.     // protected LinearLayout mContentView;


  5.     public SettingListView(Context context, AttributeSet attrs) {
  6.         super(context, attrs);
  7.         mInflater = LayoutInflater.from(getContext());
  8.         this.setDivider(null);
  9.         // TODO Auto-generated constructor stub

  10.     }

  11.     public SettingListView(Context context) {
  12.         super(context);
  13.         mInflater = LayoutInflater.from(getContext());
  14.         setDivider(null);
  15.     }

  16.     public void clearSettingList() {
  17.         mSettingList.clear();
  18.     }

  19.     public void updateAdapter() {
  20.         this.setAdapter(new SettingListAdapter());
  21.     }

  22.     public class SettingListAdapter extends BaseAdapter {

  23.         @Override
  24.         public int getCount() {
  25.             // TODO Auto-generated method stub

  26.             return mSettingList.size();
  27.         }

  28.         @Override
  29.         public Object getItem(int position) {
  30.             // TODO Auto-generated method stub

  31.             return mSettingList.get(position);
  32.         }

  33.         @Override
  34.         public long getItemId(int position) {
  35.             // TODO Auto-generated method stub

  36.             return position;
  37.         }

  38.         @Override
  39.         public View getView(int position, View view, ViewGroup parent) {
  40.             // TODO Auto-generated method stub

  41.             
  42.             int divider = getContext().getResources().getDimensionPixelSize(
  43.                 R.dimen.detail_list_divider);
  44.             
  45.             String groupTitle = "";
  46.             boolean isShowGroupTitle = false;
  47.             view = mInflater.inflate(R.layout.setting_grouped_item_layout, null);
  48.             LinearLayout mContentView = (LinearLayout) view.findViewById(R.id.group_root);
  49.             TextView groupTitleView = (TextView) view.findViewById(R.id.group_title);
  50.             View viewchild = null;
  51.             HashMap<String, List<SettingItem>> settingItems = mSettingList.get(position);
  52.             Iterator it = settingItems.entrySet().iterator();
  53.             while (it.hasNext()) {
  54.                 java.util.Map.Entry entry = (java.util.Map.Entry) it.next();
  55.                 if (!"".equals(entry.getKey()) && !isShowGroupTitle) {
  56.                     groupTitle = (String) entry.getKey();
  57.                     isShowGroupTitle = true;
  58.                     groupTitleView.setText(groupTitle);
  59.                     groupTitleView.setVisibility(View.VISIBLE);
  60.                 }

  61.                 List<SettingItem> itemList = (List<SettingItem>) entry.getValue();
  62.                 int i = 0;
  63.                 for (SettingItem item : itemList) {
  64.                     switch (item.getType()) {
  65.                     case SettingItem.TEXT:
  66.                         // mWidget = (TextView)view.findViewById(R.id.widget);

  67.                         ViewHolderText viewHolderText = new ViewHolderText();
  68.                         viewchild = mInflater.inflate(
  69.                             R.layout.setting_entry_text, null);
  70.                         viewHolderText.mGroupTitle = (TextView) viewchild.findViewById(R.id.setting_name);

  71.                         if (item.getText() == null)
  72.                             viewHolderText.mGroupTitle.setVisibility(View.GONE);
  73.                         else {
  74.                             viewHolderText.mGroupTitle.setVisibility(View.VISIBLE);
  75.                             viewHolderText.mGroupTitle.setText(item.getText());
  76.                         }
  77. // viewchild.setOnClickListener(item.getClickListener());

  78.                         break;
  79.                     case SettingItem.IMAGE:
  80.                         ViewHolderImage viewHolderImage = new ViewHolderImage();
  81.                         viewchild = mInflater.inflate(
  82.                             R.layout.setting_entry_image, null);
  83.                         viewHolderImage.mGroupTitle = (TextView) viewchild.findViewById(R.id.setting_name);
  84.                         viewHolderImage.mWidget = (ImageView) viewchild.findViewById(R.id.widget);

  85.                         if (item.getText() == null)
  86.                             viewHolderImage.mGroupTitle.setVisibility(View.GONE);
  87.                         else {
  88.                             viewHolderImage.mGroupTitle.setVisibility(View.VISIBLE);
  89.                             viewHolderImage.mGroupTitle.setText(item.getText());
  90.                         }

  91.                         if (item.getImage() > 0) {
  92.                             viewHolderImage.mWidget.setBackgroundResource(item.getImage());
  93.                             viewHolderImage.mWidget.setVisibility(View.VISIBLE);
  94.                         } else {
  95.                             viewHolderImage.mWidget.setVisibility(View.GONE);
  96.                         }
  97. // viewchild.setOnClickListener(item.getClickListener());

  98.                         break;

  99.                     default:
  100.                         break;
  101.                     }
  102.                     viewchild.setTag(item);
  103.                     viewchild.setBackgroundDrawable(SkinFactory.from(getContext()).getSkinStyle().getDrawable(
  104.                         getBackgroup(i, itemList.size(), true)));
  105.                     viewchild.setOnClickListener(item.getClickListener());
  106.                     mContentView.addView(viewchild);
  107.                     
  108.                     LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
  109.                     if (params == null) {
  110.                         params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  111.                     }
  112.                     if (i == itemList.size() - 1 && i != mSettingList.size() - 1)
  113.                         params.bottomMargin = divider;
  114.                     
  115.                     
  116.                     if (itemList.size() > 1 && i < itemList.size() - 1) {
  117.                         ImageView dividerImage = new ImageView(getContext());
  118.                         dividerImage.setBackgroundDrawable(SkinFactory.from(getContext()).getSkinStyle().getDrawable(
  119.                             getDividerStyle(i, itemList.size())));
  120.                         mContentView.addView(dividerImage, params);
  121.                     }
  122.                     i++;
  123.                 }
  124.             }

  125.             return view;
  126.         }
  127.     }

  128.     
  129.     
  130.     public class ViewHolderText {
  131.         public TextView mGroupTitle;
  132.     }

  133.     public class ViewHolderImage {
  134.         public TextView mGroupTitle;
  135.         public ImageView mWidget;
  136.     }

  137.     public class ViewHolderCheckbox {
  138.         public TextView mGroupTitle;
  139.         public CheckBox mCheckBox;
  140.     }

  141.     public class ViewHolderRadio {
  142.         public TextView mGroupTitle;
  143.         public RadioButton mRadio;
  144.     }
  145. }
 
调用方法:

点击(此处)折叠或打开

  1. listView = (SettingListView)findViewById(R.id.list);
  2.         getListItems();
  3.         listView.updateAdapter();

  4.     public void getListItems() {
  5.         HashMap<String, List<SettingItem>> item;
  6.         SettingItem settingItem = null;
  7.         List<SettingItem> listItems = null;
  8.         //----

  9.         item = new HashMap<String, List<SettingItem>>();
  10.         listItems = new ArrayList<SettingItem>();
  11.         settingItem = new SettingItem(1, SettingItem.IMAGE);
  12.         settingItem.setText(getString(R.string.account_setting_privacy));
  13.         settingItem.setImage(R.drawable.setting_more);
  14.         settingItem.setListener(this);
  15.         listItems.add(settingItem);
  16.         
  17.         settingItem = new SettingItem(2, SettingItem.IMAGE);
  18.         settingItem.setText(getString(R.string.account_setting_notification));
  19.         settingItem.setListener(this);
  20.         settingItem.setImage(R.drawable.setting_more);
  21.         listItems.add(settingItem);
  22.         
  23.         settingItem = new SettingItem(3, SettingItem.IMAGE);
  24.         settingItem.setText(getString(R.string.account_setting_save_phote));
  25.         settingItem.setListener(this);
  26.         settingItem.setImage(R.drawable.setting_more);
  27.         listItems.add(settingItem);
  28.         item.put("", listItems);
  29.         
  30.         listView.mSettingList.add(item);
  31.         //----

  32.         item = new HashMap<String, List<SettingItem>>();
  33.         listItems = new ArrayList<SettingItem>();
  34.         settingItem = new SettingItem(4, SettingItem.TEXT);
  35.         settingItem.setText(getString(R.string.account_setting_share));
  36.         settingItem.setListener(this);
  37.         listItems.add(settingItem);
  38.         settingItem = new SettingItem(5, SettingItem.IMAGE);
  39.         settingItem.setText(getString(R.string.account_setting_feedback));
  40.         settingItem.setListener(this);
  41.         settingItem.setImage(R.drawable.setting_more);
  42.         listItems.add(settingItem);
  43.         settingItem = new SettingItem(6, SettingItem.IMAGE);
  44.         settingItem.setText("ccc");
  45.         settingItem.setListener(this);
  46.         settingItem.setImage(R.drawable.setting_more);
  47.         listItems.add(settingItem);
  48.         item.put("", listItems);
  49.         
  50.         listView.mSettingList.add(item);

  51.         
  52.         item = new HashMap<String, List<SettingItem>>();
  53.         listItems = new ArrayList<SettingItem>();
  54.         settingItem = new SettingItem(7, SettingItem.TEXT);
  55.         settingItem.setText(getString(R.string.account_setting_share));
  56.         settingItem.setListener(this);
  57.         listItems.add(settingItem);
  58.         settingItem = new SettingItem(8, SettingItem.IMAGE);
  59.         settingItem.setText(getString(R.string.account_setting_feedback));
  60.         settingItem.setListener(this);
  61.         settingItem.setImage(R.drawable.setting_more);
  62.         listItems.add(settingItem);
  63.         settingItem = new SettingItem(9, SettingItem.IMAGE);
  64.         settingItem.setText("qweqwe");
  65.         settingItem.setListener(this);
  66.         settingItem.setImage(R.drawable.setting_more);
  67.         listItems.add(settingItem);
  68.         item.put("", listItems);
  69.         
  70.         listView.mSettingList.add(item);
  71.     }


阅读(3069) | 评论(1) | 转发(0) |
0

上一篇:svn 地址

下一篇:开源框架AsyncHttpClient使用

给主人留下些什么吧!~~

十七岁的回忆2012-04-10 02:01:57

好高清的界面!