ListView圆角实现

ScondActivity.java
package mykotlin.zhoujian.com.myapplication.activity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import mykotlin.zhoujian.com.myapplication.R;
import mykotlin.zhoujian.com.myapplication.view.CornerListView;

public class ScondActivity extends Activity {

    private CornerListView cornerListView;
    private SimpleAdapter adapter;
    private List> listData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cornerListView = (CornerListView) findViewById(R.id.setting_list);
        setListData();
        adapter = new SimpleAdapter(this, listData, R.layout.main_tab_setting_list_item, new String[]{"text"}, new int[]{R.id.tv_system_title});
        cornerListView.setAdapter(adapter);

    }

    private void setListData() {
        listData = new ArrayList>();

        Map map = new HashMap();
        map.put("text", "哈哈");
        listData.add(map);

        map = new HashMap();
        map.put("text", "哈哈");
        listData.add(map);

        map = new HashMap();
        map.put("text", "呵呵");
        listData.add(map);

        map = new HashMap();
        map.put("text", "啦啦");
        listData.add(map);

        map = new HashMap();
        map.put("text", "嘿嘿");
        listData.add(map);
    }
}

 

activity_main.xml



    

 

CornerListView.java
package mykotlin.zhoujian.com.myapplication.view;

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

import mykotlin.zhoujian.com.myapplication.R;

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 position = pointToPosition(x, y);
            if (position == AdapterView.INVALID_POSITION)
                break;  
            else {  
                if (position == 0) {
                    if (position == (getAdapter().getCount() - 1)) {
                        setSelector(R.drawable.list_corner_round);
                    } else {  
                        setSelector(R.drawable.list_corner_round_top);
                    }  
                } else if (position == (getAdapter().getCount() - 1))
                    setSelector(R.drawable.list_corner_round_bottom);
                else {  
                    setSelector(R.drawable.list_corner_round_center);
                }  
            }  
            break;  
        case MotionEvent.ACTION_UP:  
            break;  
        }  
        return super.onInterceptTouchEvent(ev);  
    }  
}  

 

 

list_corner_round.xml




   

    
    
  

 

list_corner_round_bottom.xml

 




    
 

 

 

list_corner_round_center.xml

 



    
   

list_corner_round_top.xml

 



    
 

main_tab_setting_list_item.xml

 




    

        

        
    


 

你可能感兴趣的:(Android基础)