IT开发者百科  > 所属分类  >  ListView   
[0] 评论[0] 编辑

怎样设置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。

附件列表


0

Java-Android手机千人开发交流QQ群:38088312,PHP开发千人高级交流QQ群:50194090,欢迎加入学习!本站为
非赢利站点,挖掘网络资源,分享个人兴趣,如有侵犯您的版权,请联系我们,我们会第一时间删除内容或添加转载出处,敬请谅解!

同义词

暂无同义词