怎样设置listview item选中时部分高亮?

怎样设置listview item选中时部分高亮?
1, 有一个现成的回调接口给选中item可用: AbsListView.SelectionBoundsAdjuster,新建一个自己的类如下:
public class ItemHighLightScope extends LinearLayout implements SelectionBoundsAdjuster{

    TextView sortIndex;
    private static int PADDING_TOP = 0;
    
    public ItemHighLightScope(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }
    
    @Override
    protected void onFinishInflate() {
        // TODO Auto-generated method stub
        try{
        super.onFinishInflate();
        sortIndex = (TextView)findViewById(R.id.date_header_text);
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

    @Override
    public void adjustListItemSelectionBounds(Rect bounds) {
        // TODO Auto-generated method stub
        if(sortIndex.getVisibility() == View.VISIBLE){
            bounds.top += (sortIndex.getHeight() + PADDING_TOP);
        }        
    }    
}

注意:要实现ItemHighLightScope(Context context, AttributeSet attrs) 这个构造器,否则会报错cannot inflate。
    要获得控件的view在onFinishInflate()里inflate,这时控件的view已被创建,才能被取到高度等信息。
    在adjustListItemSelectionBounds(Rect bounds)里设定高亮范围。此处的坐标应该是相对父view的。
2, 在item的xml里,把总LinearLayout改成自己定义的类名,比如com.test.zipcategory.ItemHighLightScope。

原文地址:https://www.cnblogs.com/lionfight/p/2881817.html