这段代码目前已经加在我的一个jar包androidkit中,还没发布。
适用于android1.6以上,不依赖其他jar包
使用时不需要继承这里的RoundListAdapter。只需要在你实现了ListAdapter的类中,传入一个RoundParams的对象,并在getView方法返回前调用这里RoundListAdapter类提供的静态方法。
RoundListAdapter.setItemBackground(position, switcherView, mParams,
适用于android1.6以上,不依赖其他jar包
使用时不需要继承这里的RoundListAdapter。只需要在你实现了ListAdapter的类中,传入一个RoundParams的对象,并在getView方法返回前调用这里RoundListAdapter类提供的静态方法。
RoundListAdapter.setItemBackground(position, switcherView, mParams,
getCount());
01 | /* |
02 |
* @(#)RoundListAdapter.java Project:com.sinaapp.msdxblog.androidkit |
03 |
* Date:2012-12-6 |
04 |
* |
05 |
* Copyright (c) 2011 CFuture09, Institute of Software, |
06 |
* Guangdong Ocean University, Zhanjiang, GuangDong, China. |
07 |
* All rights reserved. |
08 |
* |
09 |
* 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 |
23 | import android.view.View; |
24 | import android.widget.ListAdapter; |
25 |
26 | /** |
27 |
* @author Geek_Soledad (66704238@51uc.com) |
28 |
*/ |
29 | public abstract class RoundListAdapter implements ListAdapter { |
30 |
/** |
31 |
* 圆角ListView的参数类。定义了顶部背景,底部背景,中间背景及单独一个时的背景。 |
32 |
* |
33 |
* @author msdx |
34 |
* |
35 |
*/ |
36 |
public static class RoundParams { |
37 |
public int topResid; |
38 |
public int middleResid; |
39 |
public int bottomResid; |
40 |
public int lonelyResid; |
41 |
42 |
public RoundParams( int topResid, int middleReside, int bottomResid, |
43 |
int lonelyResid) { |
44 |
this .topResid = topResid; |
45 |
this .middleResid = middleReside; |
46 |
this .bottomResid = bottomResid; |
47 |
this .lonelyResid = lonelyResid; |
48 |
} |
49 |
} |
50 |
51 |
public static void setItemBackground( int position, View item, |
52 |
final RoundParams mParams, final int count) { |
53 |
if (count == 1 ) { |
54 |
item.setBackgroundResource(mParams.lonelyResid); |
55 |
} else if (position > 0 && position < count - 1 ) { |
56 |
item.setBackgroundResource(mParams.middleResid); |
57 |
} else if (position == 0 ) { |
58 |
item.setBackgroundResource(mParams.topResid); |
59 |
} else { |
60 |
item.setBackgroundResource(mParams.bottomResid); |
61 |
} |
62 |
} |
63 | } |
2.[代码]使用示例
01 | /* |
02 |
* @(#)LocalAdapter.java Project:RTKSETTINGS |
03 |
* Date:2013-1-9 |
04 |
* |
05 |
* Copyright (c) 2013 Geek_Soledad. |
06 |
* All rights reserved. |
07 |
* |
08 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
09 |
* 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 |
22 | import java.util.ArrayList; |
23 | import java.util.List; |
24 |
25 | import android.app.TvManager; |
26 | import android.content.Context; |
27 | import android.view.View; |
28 | import android.view.ViewGroup; |
29 | import android.widget.BaseAdapter; |
30 |
31 | import com.lurencun.cfuture09.androidkit.widget.roundlist.RoundListAdapter; |
32 | import com.lurencun.cfuture09.androidkit.widget.roundlist.RoundListAdapter.RoundParams; |
33 | import com.realtek.msdx.rtksettings.activity.MainActivity; |
34 | import com.realtek.msdx.rtksettings.bean.LocalSettingsBean; |
35 |
36 | /** |
37 |
* @author Geek_Soledad (msdx.android@tom.com) |
38 |
*/ |
39 | public class LocalAdapter extends BaseAdapter { |
40 |
41 |
private RoundParams mParams; |
42 |
private Context mContext; |
43 |
44 |
public LocalAdapter(Context context, RoundParams params) { |
45 |
super (); |
46 |
mContext = context; |
47 |
mParams = params; |
48 |
} |
49 |
50 |
@Override |
51 |
public int getCount() { |
52 |
return 5 ; |
53 |
} |
54 |
55 |
@Override |
56 |
public Object getItem( int position) { |
57 |
return position; |
58 |
} |
59 |
60 |
@Override |
61 |
public long getItemId( int position) { |
62 |
return position; |
63 |
} |
64 |
65 |
@Override |
66 |
public View getView( int position, View convertView, ViewGroup parent) { |
67 |
// 在这里创建view, |
68 |
//SwitcherTextView view = new SwitcherTextView(mContext); |
69 |
// 然后在返回view前进行调用 |
70 |
RoundListAdapter.setItemBackground(position, view, mParams, |
71 |
getCount()); |
72 |
return view; |
73 |
} |
74 | } |