ListView自定义圆角的实现以及自动高度

26 篇文章 0 订阅
4 篇文章 0 订阅

参考:http://blog.csdn.net/jj120522/article/details/7944484

听说还可以用9path图片的方式,但下面是用shape的方式。

首先,要准备shape,如下:

这个是ListView的背景shape:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- ListView的完整背景的shape.圆角背景 -->
    <stroke
        android:width="1dp"
        android:color="@color/gray"/>
    
    <solid 
        android:color="@color/white"/>
    
    <corners android:radius="8dp"/>

</shape>

然后,为最前面被选择item的shape:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- listview的第一项selector后背景的shape -->
    <stroke 
        android:width="1dp"
        android:color="@color/gray"/>
    
    <solid android:color="@color/gray"/>
    
    <corners 
        android:topLeftRadius="8dp"
        android:topRightRadius="8dp"/>

</shape>
最后面的item:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- ListView底部selector后背景 -->
    <stroke 
        android:width="1dp"
        android:color="@color/gray"/>
    
    <solid android:color="@color/gray"/>
    
    <corners 
        android:bottomLeftRadius="8dp"
        android:bottomRightRadius="8dp"/>

</shape>

中间的item:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
     <!-- ListView中间selector后背景 -->
    <stroke 
        android:width="1dp"
        android:color="@color/gray"/>
    
    <solid android:color="@color/gray"/>

</shape>
shape设置完毕之后,接下来自定义ListView,继承ListView,重写onInterceptTouchEvent方法:

package com.nassoft.infomed.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.nassoft.infomed.R;
/**
 * 
 * @ClassName: UC_RoundedListView 
 * @Description: TODO(圆角自定义ListView) 
 * @author zyl 
 * @date 2012-9-12 下午3:30:43
 */
public class UC_RoundedListView extends ListView {

	public UC_RoundedListView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public UC_RoundedListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public UC_RoundedListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	/**
	 * 拦截触摸事件
	 */
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {  
        case MotionEvent.ACTION_DOWN:  
            int x = (int) ev.getX();  
            int y = (int) ev.getY();  
            int itemnum = pointToPosition(x, y);  
            if (itemnum == AdapterView.INVALID_POSITION)  
                break;  
            else {  
                if (itemnum == 0) {  
                    if (itemnum == (getAdapter().getCount() - 1)) {  
                        // 只有一项  
                        setSelector(R.drawable.listview_onlyone_shape);  
                    } else {  
                        // 第一项  
                        setSelector(R.drawable.listview_top_shape);  
                    }  
                } else if (itemnum == (getAdapter().getCount() - 1))  
                    // 最后一项  
                    setSelector(R.drawable.listview_bottom_shape);  
                else {  
                    // 中间项  
                    setSelector(R.drawable.listview_center_shape);  
                }  
            }  
            break;  
        case MotionEvent.ACTION_UP:  
            break;  
        }  
        return super.onInterceptTouchEvent(ev);  
	}

	
	/*** 
     * 动态设置listview的高度 
     *  
     * @param listView 
     */  
    public void setListViewHeightBasedOnChildren(ListView listView) {  
        ListAdapter listAdapter = listView.getAdapter();  
        if (listAdapter == null) {  
            return;  
        }  
        int totalHeight = 0;  
        for (int i = 0; i < listAdapter.getCount(); i++) {  
            View listItem = listAdapter.getView(i, null, listView);  
            listItem.measure(0, 0);  
            totalHeight += listItem.getMeasuredHeight();  
        }  
        ViewGroup.LayoutParams params = listView.getLayoutParams();  
        params.height = totalHeight  
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));  
        // params.height += 5;// if without this statement,the listview will be  
        // a  
        // little short  
        // listView.getDividerHeight()获取子项间分隔符占用的高度  
        // params.height最后得到整个ListView完整显示需要的高度  
        listView.setLayoutParams(params);  
    }
}



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值