为什么不只检查项目相对于列表大小的位置:
填充:
public View getView(int position, View convertView, ViewGroup parent)
{
//recycle views and whatever else you normally would do goes here..
//...
//...
if (position == 0){
convertView.setPadding(0, 10, 0, 0); //padding on top for top item
}
else if (position == getCount() - 1){
convertView.setPadding(0, 0, 0, 10); //padding on bottom for bottom item
}
else{
convertView.setPadding(0, 0, 0, 0); //no padding
}
}
对于边距使用
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
convertView.setLayoutParams(lp);
例如:
public View getView(int position, View convertView, ViewGroup parent)
{
//recycle views and whatever else you normally would do goes here..
//...
//...
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
if (position == 0){
lp.setMargins(0, 10, 0, 0); //margin on top for top item
}
else if (position == getCount() - 1){
lp.setMargins(0, 10, 0, 10); //margin on bottom for bottom item
}
else{
lp.setMargins(0, 0, 0, 0); //no margin
}
convertView.setLayoutParams(lp);
}
前提是您已经为您的适配器正确实现了 getCount() 方法。