效果如下:( 点击下载演示)
本人已经开源到了TaoCode,可以使用SVN免费更新下来:
http://code.taobao.org/svn/nestlistview/trunk
本人TaoCode首页:http://code.taobao.org/u/scorpioyou/mypro/
实现原理:顶部利用了ListView的HeadView来实现,然后其他每个item都用背景实现!
首先设置一些常量:
public class NestHelper {
public static final int BACK_COLOR = 0xFFF8F3D5;// 背景色
public static final int LINE_COLOR = 0xFF5A595A;// 线颜色
public static final int LINE_WIDTH = 2;// 线的宽度(要偶数)
public static final int LINE_SPAN = 4;// 线的间距
}
每个项目的背景实现最为核心,请看代码:
public class NestItemLayout extends FrameLayout implements NestNotifyData {
public NestItemLayout(Context context) {
super(context);
}
public NestItemLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NestItemLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@SuppressWarnings("deprecation")
@Override
public void notifyDataSetChanged(int count, int position) {
setBackgroundDrawable(new NestDrawable(count, position));
}
/** 绘制item Layout背景 两边跟底部 */
class NestDrawable extends Drawable {
private int linsCount = 0;// 两边各有多少条线
private int span = 0;// 一条线的宽度(线加线之间距)
public NestDrawable(int count, int position) {
linsCount = count - position;
if (linsCount > 5) {
linsCount = 5;// 一边最多5条
}
span = (NestHelper.LINE_WIDTH + NestHelper.LINE_SPAN);
setPadding(span * linsCount, 0, span * linsCount,
NestHelper.LINE_WIDTH);
}
@Override
public void draw(Canvas canvas) {
canvas.drawColor(NestHelper.BACK_COLOR);
Paint paint = new Paint();
paint.setColor(NestHelper.LINE_COLOR);
paint.setStrokeWidth(NestHelper.LINE_WIDTH);
// 绘制底部线
canvas.drawLine(span * (linsCount - 1), getHeight()
- NestHelper.LINE_WIDTH / 2, getWidth() - span
* (linsCount - 1), getHeight() - NestHelper.LINE_WIDTH / 2,
paint);
for (int i = 0; i < linsCount; i++) {
// 绘制左边线
canvas.drawLine(span * i + NestHelper.LINE_WIDTH / 2, 0, span
* i + NestHelper.LINE_WIDTH / 2, getHeight(), paint);
// 绘制右边线
canvas.drawLine(getWidth() - span * i - NestHelper.LINE_WIDTH
/ 2, 0, getWidth() - span * i - NestHelper.LINE_WIDTH
/ 2, getHeight(), paint);
;
}
}
@Override
public void setAlpha(int alpha) {
}
@Override
public void setColorFilter(ColorFilter cf) {
}
@Override
public int getOpacity() {
return 0;
}
}
}
最后重写ListView控件:
public class NestListView extends ListView implements NestNotifyData {
private NestHeadView headView;
public NestListView(Context context) {
super(context);
initView();
}
public NestListView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public NestListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView();
}
private void initView() {
headView = new NestHeadView(getContext());
addHeaderView(headView);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
@Override
public void notifyDataSetChanged(int count, int position) {
headView.notifyDataSetChanged(count, position);
}
public NestNotifyData getNotifyData() {
return this;
}
}
Adatper:
package com.cyy.nestlistview;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.cyy.widget.NestNotifyData;
/**
* @see http://blog.csdn.net/chenlove1
* @author chenyingyou
*
*/
public class NestAdapter extends BaseAdapter {
private List<String> list;
private Context context;
public NestAdapter(Context context, List<String> list) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView) {
convertView = LayoutInflater.from(context).inflate(
R.layout.item_nest, null);
}
((TextView) convertView.findViewById(R.id.tv_item)).setText(list
.get(position));
if (convertView instanceof NestNotifyData) {
((NestNotifyData) convertView).notifyDataSetChanged(list.size(),
position);
}
return convertView;
}
}