当前位置:编程学习 > wap >>

ListView圆角



本人实学Android
这是网上下的一个ListView圆角的示例。
假如我想把设置1 item、设置2 item、设置3 item间隔开来,当然四个角都是圆角

大侠们,该如何实现??

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);
    }
}


RoundCornerActivity.java

public class RoundCornerActivity extends Activity {
private CornerListView mListView = null;
ArrayList<HashMap<String, String>> map_list1 = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

getDataSource1();
// getDataSource2();

SimpleAdapter adapter1 = new SimpleAdapter(this, map_list1,
R.layout.simple_list_item_1, new String[] { "item" },
new int[] { R.id.item_title });
mListView = (CornerListView) findViewById(R.id.list1);
mListView.setAdapter(adapter1);
mListView.setOnItemClickListener(new OnItemListSelectedListener());
}
public ArrayList<HashMap<String, String>> getDataSource1() {

map_list1 = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map1 = new HashMap<String, String>();
HashMap<String, String> map2 = new HashMap<String, String>();
HashMap<String, String> map3 = new HashMap<String, String>();

map1.put("item", "设置1");
map2.put("item", "设置2");
map3.put("item", "设置3");

map_list1.add(map1);
map_list1.add(map2);
map_list1.add(map3);

return map_list1;
}

class OnItemListSelectedListener implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if (arg2 == 0) {
System.out.println("0");
}else{
System.out.println("1");
}
}
}
}


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"
    android:background="@color/white"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingBottom="5dp"
        android:paddingLeft="25dp"
        android:paddingTop="15dp" >

        <TextView
            android:id="@+id/menu_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/setting"
            android:textColor="@color/gray" />
    </LinearLayout>

    <com.corner.test.CornerListView
        android:id="@+id/list1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:background="@drawable/shape_bg_listview"
        android:cacheColorHint="@null"/>

</LinearLayout>


simple_list_item_1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_marginTop="10dip"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="10dip"
        android:paddingLeft="15dip"
        android:paddingRight="5dip"
        android:paddingTop="10dip" >

        <TextView
            android:id="@+id/item_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:textColor="@color/gray"
            android:textSize="15sp" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="16dp"
            android:src="@drawable/arrow" />
        
    </RelativeLayout>

</LinearLayout>
--------------------编程问答-------------------- 首先你要会弄只有两个圆角的layout  http://blog.xianqu.org/2012/04/android-borders-and-radius-corners/ 可以参考这里

然后将上面是圆角的layout 弄到head 下面圆角的弄成foot 

当然你也可以都当做普通的弄这个要看你自己的填充 --------------------编程问答--------------------
引用 1 楼 shen332401890 的回复:
首先你要会弄只有两个圆角的layout  http://blog.xianqu.org/2012/04/android-borders-and-radius-corners/ 可以参考这里

然后将上面是圆角的layout 弄到head 下面圆角的弄成foot 

当然你也可以都当做普通的弄这个要看你自己的填充
--------------------编程问答-------------------- 学习了,有用 --------------------编程问答--------------------
引用 2 楼 guoyoulei520 的回复:
Quote: 引用 1 楼 shen332401890 的回复:

首先你要会弄只有两个圆角的layout  http://blog.xianqu.org/2012/04/android-borders-and-radius-corners/ 可以参考这里

然后将上面是圆角的layout 弄到head 下面圆角的弄成foot 

当然你也可以都当做普通的弄这个要看你自己的填充


哈哈  来混分的 顺便 说一下别的方式呗。。 --------------------编程问答--------------------
引用 4 楼 shen332401890 的回复:
Quote: 引用 2 楼 guoyoulei520 的回复:

Quote: 引用 1 楼 shen332401890 的回复:

首先你要会弄只有两个圆角的layout  http://blog.xianqu.org/2012/04/android-borders-and-radius-corners/ 可以参考这里

然后将上面是圆角的layout 弄到head 下面圆角的弄成foot 

当然你也可以都当做普通的弄这个要看你自己的填充


哈哈  来混分的 顺便 说一下别的方式呗。。

不错不错,抓紧啊.快月底了 --------------------编程问答--------------------
引用 5 楼 guoyoulei520 的回复:
Quote: 引用 4 楼 shen332401890 的回复:

Quote: 引用 2 楼 guoyoulei520 的回复:

Quote: 引用 1 楼 shen332401890 的回复:

首先你要会弄只有两个圆角的layout  http://blog.xianqu.org/2012/04/android-borders-and-radius-corners/ 可以参考这里

然后将上面是圆角的layout 弄到head 下面圆角的弄成foot 

当然你也可以都当做普通的弄这个要看你自己的填充


哈哈  来混分的 顺便 说一下别的方式呗。。

不错不错,抓紧啊.快月底了



好多人 都没有形成良好的 结贴习惯。。。。估计不够吧 才500左右 --------------------编程问答-------------------- 其实有一种很简单的解决方式,用一个“9.PNG"格式的图片作为背景就好了,这种格式的图片,无限拉伸也不会改变圆角的弧度的 --------------------编程问答--------------------
引用 5 楼 guoyoulei520 的回复:
Quote: 引用 4 楼 shen332401890 的回复:

Quote: 引用 2 楼 guoyoulei520 的回复:

Quote: 引用 1 楼 shen332401890 的回复:

首先你要会弄只有两个圆角的layout  http://blog.xianqu.org/2012/04/android-borders-and-radius-corners/ 可以参考这里

然后将上面是圆角的layout 弄到head 下面圆角的弄成foot 

当然你也可以都当做普通的弄这个要看你自己的填充


哈哈  来混分的 顺便 说一下别的方式呗。。

不错不错,抓紧啊.快月底了

你拿过花了,大家都轮轮啊 --------------------编程问答--------------------
引用 8 楼 cclovescw 的回复:
Quote: 引用 5 楼 guoyoulei520 的回复:

Quote: 引用 4 楼 shen332401890 的回复:

Quote: 引用 2 楼 guoyoulei520 的回复:

Quote: 引用 1 楼 shen332401890 的回复:

首先你要会弄只有两个圆角的layout  http://blog.xianqu.org/2012/04/android-borders-and-radius-corners/ 可以参考这里

然后将上面是圆角的layout 弄到head 下面圆角的弄成foot 

当然你也可以都当做普通的弄这个要看你自己的填充


哈哈  来混分的 顺便 说一下别的方式呗。。

不错不错,抓紧啊.快月底了

你拿过花了,大家都轮轮啊

这个月我没怎么答 --------------------编程问答--------------------
引用 9 楼 guoyoulei520 的回复:
Quote: 引用 8 楼 cclovescw 的回复:

Quote: 引用 5 楼 guoyoulei520 的回复:

Quote: 引用 4 楼 shen332401890 的回复:

Quote: 引用 2 楼 guoyoulei520 的回复:

Quote: 引用 1 楼 shen332401890 的回复:

首先你要会弄只有两个圆角的layout  http://blog.xianqu.org/2012/04/android-borders-and-radius-corners/ 可以参考这里

然后将上面是圆角的layout 弄到head 下面圆角的弄成foot 

当然你也可以都当做普通的弄这个要看你自己的填充


哈哈  来混分的 顺便 说一下别的方式呗。。

不错不错,抓紧啊.快月底了

你拿过花了,大家都轮轮啊

这个月我没怎么答



我了个去 --------------------编程问答-------------------- 这个效果这用xml方式就可以直接实现了
--------------------编程问答-------------------- 哪那么麻烦啊

如果列数固定,就直接用LinerLayout布死,不用listview了。
不固定,重写adapter ,getView的时候判断下,如果是第一个,换个背景,最后一个,换个背景
其他的就是默认背景
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,