栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

android开发之方形圆角listview代码分享

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

android开发之方形圆角listview代码分享

先看效果图:

首先,你得写一个类我们命名为CornerListView

[java]

复制代码 代码如下:

public class CornerListView extends ListView {
    public CornerListView(Context context) {
        super(context);
    }

    public CornerListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CornerListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @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.app_list_corner_round);
                        }else{
                            setSelector(R.drawable.app_list_corner_round_top);
                        }
                    }else if(itemnum==(getAdapter().getCount()-1))
                            setSelector(R.drawable.app_list_corner_round_bottom);
                    else{                            
                        setSelector(R.drawable.app_list_corner_shape);
                    }
                }

                break;
        case MotionEvent.ACTION_UP:
                break;
        }

        return super.onInterceptTouchEvent(ev);
    }
}


public class CornerListView extends ListView {
    public CornerListView(Context context) {
        super(context);
    }

    public CornerListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CornerListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @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.app_list_corner_round);
                        }else{
                            setSelector(R.drawable.app_list_corner_round_top);
                        }
                 }else if(itemnum==(getAdapter().getCount()-1))
                         setSelector(R.drawable.app_list_corner_round_bottom);
                 else{                          
                     setSelector(R.drawable.app_list_corner_shape);
                 }
                }

                break;
        case MotionEvent.ACTION_UP:
                break;
        }

        return super.onInterceptTouchEvent(ev);
    }
}


其中,app_list_corner_round

[html]

复制代码 代码如下:


            android:endColor="#40B9FF" 
        android:angle="270"/>
            android:topRightRadius="6dip"
        android:bottomLeftRadius="6dip"
        android:bottomRightRadius="6dip"/>



            android:endColor="#40B9FF"
        android:angle="270"/>
            android:topRightRadius="6dip"
        android:bottomLeftRadius="6dip"
        android:bottomRightRadius="6dip"/>


app_list_corner_round_top

[html]
复制代码 代码如下:


            android:endColor="#40B9FF" 
        android:angle="270"/>
            android:topRightRadius="6dip"/>



            android:endColor="#40B9FF"
        android:angle="270"/>
            android:topRightRadius="6dip"/>

app_list_corner_round_bottom

[html]
复制代码 代码如下:


            android:endColor="#40B9FF" 
        android:angle="270"/>
            android:bottomRightRadius="6dip" />



            android:endColor="#40B9FF"
        android:angle="270"/>
            android:bottomRightRadius="6dip" />


app_list_corner_shape
[html]
复制代码 代码如下:


            android:endColor="#40B9FF" 
        android:angle="270"/>
 



            android:endColor="#40B9FF"
        android:angle="270"/>


写好了之后,就可以在你的代码中直接像listview一样调用。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/165204.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号