免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1184 | 回复: 0
打印 上一主题 下一主题

[Android] 实现IOS圆角风格的列表ListView [复制链接]

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2016-03-21 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-05-26 10:53 |只看该作者 |倒序浏览
这段代码目前已经加在我的一个jar包androidkit中,还没发布。
适用于android1.6以上,不依赖其他jar包

使用时不需要继承这里的RoundListAdapter。只需要在你实现了ListAdapter的类中,传入一个RoundParams的对象,并在getView方法返回前调用这里RoundListAdapter类提供的静态方法。
RoundListAdapter.setItemBackground(position, switcherView, mParams,
getCount());
  1. /*
  2. * @(#)RoundListAdapter.java               Project:com.sinaapp.msdxblog.androidkit
  3. * Date:2012-12-6
  4. *
  5. * Copyright (c) 2011 CFuture09, Institute of Software,
  6. * Guangdong Ocean University, Zhanjiang, GuangDong, China.
  7. * All rights reserved.
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. *  you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. *     http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. package com.lurencun.cfuture09.androidkit.widget.roundlist;

  22. import android.view.View;
  23. import android.widget.ListAdapter;

  24. /**
  25. * @author Geek_Soledad (66704238@51uc.com)
  26. */
  27. public abstract class RoundListAdapter implements ListAdapter {
  28.     /**
  29.      * 圆角ListView的参数类。定义了顶部背景,底部背景,中间背景及单独一个时的背景。
  30.      *
  31.      * @author msdx
  32.      *
  33.      */
  34.     public static class RoundParams {
  35.         public int topResid;
  36.         public int middleResid;
  37.         public int bottomResid;
  38.         public int lonelyResid;

  39.         public RoundParams(int topResid, int middleReside, int bottomResid,
  40.                 int lonelyResid) {
  41.             this.topResid = topResid;
  42.             this.middleResid = middleReside;
  43.             this.bottomResid = bottomResid;
  44.             this.lonelyResid = lonelyResid;
  45.         }
  46.     }

  47.     public static void setItemBackground(int position, View item,
  48.             final RoundParams mParams, final int count) {
  49.         if (count == 1) {
  50.             item.setBackgroundResource(mParams.lonelyResid);
  51.         } else if (position > 0 && position < count - 1) {
  52.             item.setBackgroundResource(mParams.middleResid);
  53.         } else if (position == 0) {
  54.             item.setBackgroundResource(mParams.topResid);
  55.         } else {
  56.             item.setBackgroundResource(mParams.bottomResid);
  57.         }
  58.     }
  59. }
复制代码
  1. /*  
  2. * @(#)LocalAdapter.java              Project:RTKSETTINGS  
  3. * Date:2013-1-9  
  4. *  
  5. * Copyright (c) 2013 Geek_Soledad.  
  6. * All rights reserved.  
  7. *  
  8. * Licensed under the Apache License, Version 2.0 (the "License");  
  9. *  you may not use this file except in compliance with the License.  
  10. * You may obtain a copy of the License at  
  11. *  
  12. *     http://www.apache.org/licenses/LICENSE-2.0  
  13. *  
  14. * Unless required by applicable law or agreed to in writing, software  
  15. * distributed under the License is distributed on an "AS IS" BASIS,  
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  17. * See the License for the specific language governing permissions and  
  18. * limitations under the License.  
  19. */
  20. package com.realtek.msdx.rtksettings.view;

  21. import java.util.ArrayList;
  22. import java.util.List;

  23. import android.app.TvManager;
  24. import android.content.Context;
  25. import android.view.View;
  26. import android.view.ViewGroup;
  27. import android.widget.BaseAdapter;

  28. import com.lurencun.cfuture09.androidkit.widget.roundlist.RoundListAdapter;
  29. import com.lurencun.cfuture09.androidkit.widget.roundlist.RoundListAdapter.RoundParams;
  30. import com.realtek.msdx.rtksettings.activity.MainActivity;
  31. import com.realtek.msdx.rtksettings.bean.LocalSettingsBean;

  32. /**
  33. * @author Geek_Soledad (msdx.android@tom.com)
  34. */
  35. public class LocalAdapter extends BaseAdapter {

  36.     private RoundParams mParams;
  37.     private Context mContext;

  38.     public LocalAdapter(Context context, RoundParams params) {
  39.         super();
  40.         mContext = context;
  41.         mParams = params;
  42.     }

  43.     @Override
  44.     public int getCount() {
  45.         return 5;
  46.     }

  47.     @Override
  48.     public Object getItem(int position) {
  49.         return position;
  50.     }

  51.     @Override
  52.     public long getItemId(int position) {
  53.         return position;
  54.     }

  55.     @Override
  56.     public View getView(int position, View convertView, ViewGroup parent) {
  57.         // 在这里创建view,
  58.         //SwitcherTextView view = new SwitcherTextView(mContext);
  59.         // 然后在返回view前进行调用
  60.         RoundListAdapter.setItemBackground(position, view, mParams,
  61.                 getCount());
  62.         return view;
  63.     }
  64. }
复制代码

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP