Android 圆角Listview

Android 圆角Listview_第1张图片Android 圆角Listview_第2张图片


使Listview更加好看些,新浪微博,腾讯的都是圆角的,最基本的技能了

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="#ffffff"
    android:orientation="vertical" >

    <com.xzq.roundlistview.CornerListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:background="@drawable/shape_bg_listview"
        android:cacheColorHint="#00000000" />

</LinearLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="10dip"
        android:paddingLeft="10dip"
        android:paddingTop="10dip"
        android:textColor="#000000"
        android:textSize="16dip" />

</RelativeLayout>

app_list_corner_round_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <gradient
        android:angle="270"
        android:endColor="#76D37B"
        android:startColor="#B5E7B8" />

    <corners
        android:bottomLeftRadius="4dip"
        android:bottomRightRadius="4dip" />

</shape>

app_list_corner_round_top.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <gradient
        android:angle="270"
        android:endColor="#76D37B"
        android:startColor="#B5E7B8" />

    <corners
        android:topLeftRadius="4dip"
        android:topRightRadius="4dip" />

</shape>

app_list_corner_round.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <gradient
        android:angle="270"
        android:endColor="#76D37B"
        android:startColor="#B5E7B8" />

    <corners
        android:bottomLeftRadius="4dip"
        android:bottomRightRadius="4dip"
        android:topLeftRadius="4dip"
        android:topRightRadius="4dip" />

</shape>

app_list_corner_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <gradient
        android:angle="270"
        android:endColor="#76D37B"
        android:startColor="#B5E7B8" />

</shape>

shape_bg_listview.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:angle="180"
        android:endColor="#FFCCCCCC"
        android:startColor="@android:color/white" />

    <stroke
        android:width="1px"
        android:color="@color/gray" />

    <solid android:color="@android:color/white" />

    <corners
        android:bottomLeftRadius="10px"
        android:bottomRightRadius="10px"
        android:topLeftRadius="10px"
        android:topRightRadius="10px" />

</shape>

MainActivity.java

package com.xzq.roundlistview;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class MainActivity extends Activity {

    private CornerListView listview;
    private ArrayList<String> listData;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        listview = (CornerListView) findViewById(R.id.listview);
        setListData();
        listview.setAdapter(new CornerAdapter(this, listData));
    }

    /**
     * 设置列表数据
     */
    private void setListData() {
        listData = new ArrayList<String>();
        listData.add("下载目录");
    }
}


package com.xzq.roundlistview;

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

/**
 * 圆角Listview
 * 
 * 
 */
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);
    }

}


package com.xzq.roundlistview;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

class CornerAdapter extends BaseAdapter {

    private ArrayList<String> listData;
    private LayoutInflater inflater;

    public CornerAdapter(Context context, ArrayList<String> listData) {
        this.listData = listData;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.list_item, null);
        }
        TextView textview = (TextView) convertView.findViewById(R.id.textview);
        textview.setText(listData.get(position));

        return convertView;
    }
}




你可能感兴趣的:(Android 圆角Listview)