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

2016-02-19 09:26 5 1 收藏

想要天天向上,就要懂得享受学习。图老师为大家推荐android开发之方形圆角listview代码分享,精彩的内容需要你们用心的阅读。还在等什么快点来看看吧!

【 tulaoshi.com - 编程语言 】

先看效果图:

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

[java]

代码如下:

/**
 * 圆角ListView示例
 * @Description: 圆角ListView示例
 * @FileName: 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.SPAN style="COLOR: #ff0000"app_list_corner_round/SPAN);
                        }else{
                            setSelector(R.drawable.SPAN style="COLOR: #ff0000"app_list_corner_round_top/SPAN);
                        }
                    }else if(itemnum==(getAdapter().getCount()-1))
                            setSelector(R.drawable.SPAN style="COLOR: #ff0000"app_list_corner_round_bottom/SPAN);
                    else{                            
                        setSelector(R.drawable.SPAN style="COLOR: #ff0000"app_list_corner_shape/SPAN);
                    }
                }

                break;
        case MotionEvent.ACTION_UP:
                break;
        }

        return super.onInterceptTouchEvent(ev);
    }
}

/**
 * 圆角ListView示例
 * @Description: 圆角ListView示例
 * @FileName: 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);
    }
}


其中,app_list_corner_round

[html]

代码如下:

SPAN style="COLOR: #333333"?xml version="1.0" encoding="utf-8"?
shape xmlns:android="http://schemas.android.com/apk/res/android"
    gradient android:startColor="#BFEEFF" 
        android:endColor="#40B9FF" 
        android:angle="270"/
    corners android:topLeftRadius="6dip"
        android:topRightRadius="6dip"
        android:bottomLeftRadius="6dip"
        android:bottomRightRadius="6dip"/
/shape /SPAN

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

?xml version="1.0" encoding="utf-8"?
shape xmlns:android="http://schemas.android.com/apk/res/android"
    gradient android:startColor="#BFEEFF"
        android:endColor="#40B9FF"
        android:angle="270"/
    corners android:topLeftRadius="6dip"
        android:topRightRadius="6dip"
        android:bottomLeftRadius="6dip"
        android:bottomRightRadius="6dip"/
/shape


app_list_corner_round_top

[html]
代码如下:

SPAN style="COLOR: #333333"?xml version="1.0" encoding="utf-8"?
shape xmlns:android="http://schemas.android.com/apk/res/android"
    gradient android:startColor="#BFEEFF" 
        android:endColor="#40B9FF" 
        android:angle="270"/
    corners android:topLeftRadius="6dip"
        android:topRightRadius="6dip"/
/shape /SPAN

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

?xml version="1.0" encoding="utf-8"?
shape xmlns:android="http://schemas.android.com/apk/res/android"
    gradient android:startColor="#BFEEFF"
        android:endColor="#40B9FF"
        android:angle="270"/
    corners android:topLeftRadius="6dip"
        android:topRightRadius="6dip"/
/shape

app_list_corner_round_bottom

[html]
代码如下:

SPAN style="COLOR: #333333"?xml version="1.0" encoding="utf-8"?
shape xmlns:android="http://schemas.android.com/apk/res/android"
    gradient android:startColor="#BFEEFF" 
        android:endColor="#40B9FF" 
        android:angle="270"/
    corners android:bottomLeftRadius="6dip"
        android:bottomRightRadius="6dip" /
/shape /SPAN

?xml version="1.0" encoding="utf-8"?
shape xmlns:android="http://schemas.android.com/apk/res/android"
    gradient android:startColor="#BFEEFF"
        android:endColor="#40B9FF"
        android:angle="270"/
    corners android:bottomLeftRadius="6dip"
        android:bottomRightRadius="6dip" /
/shape

app_list_corner_shape
[html]
代码如下:

?xml version="1.0" encoding="utf-8"?
shape xmlns:android="http://schemas.android.com/apk/res/android"
    gradient android:startColor="#BFEEFF" 
        android:endColor="#40B9FF" 
        android:angle="270"/
/shape 

?xml version="1.0" encoding="utf-8"?
shape xmlns:android="http://schemas.android.com/apk/res/android"
    gradient android:startColor="#BFEEFF"
        android:endColor="#40B9FF"
        android:angle="270"/
/shape


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

来源:https://www.tulaoshi.com/n/20160219/1590793.html

延伸阅读
main.xml 文件:  代码如下: ?xml version="1.0" encoding="utf-8"?  LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"       android:layout_width="fill_parent"       android:layout_height="fill_parent"     &n...
在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示。抽空把对ListView的使用做了整理,并写了个小例子,如下图。  列表的显示需要三个元素: 1.ListVeiw 用来展示列表的View。 2.适配器 用来把数据映射到ListView上的中介。 3.数据    具...
代码如下: ListView android:id="listCWJ" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stackFromBottom="true" android:transcriptMode="alwaysScroll" /
下拉刷新在越来越多的App中使用,已经形成一种默认的用户习惯,遇到列表显示的内容时,用户已经开始习惯性的拉拉。在交互习惯上已经形成定性。之前在我的文章《IOS学习笔记34—EGOTableViewPullRefresh实现下拉刷新》中介绍过如何在IOS上实现下拉刷新的功能。今天主要介绍下在Android上实现下拉刷新的Demo,下拉控件参考自Github上开源项目Pull...
前言          Content Provider为存储数据和获取数据提供了统一的接口,它可以完成在不同应用程序下的数据共享,而在上一篇文章Android开发之SQLite的使用方法讲到的SQLite只能在同一个程序中共享数据。另外android为一些常见的数据,比如说音频,视频,图片,通讯录等提供了Content Provider,...

经验教程

661

收藏

96
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部