1. 程式人生 > >當listview中有點選事件時的實現方法

當listview中有點選事件時的實現方法

1、首先在adapter中定義介面

private static  OnAddClickListener onItemAddClick;//activity傳值

//activity傳值

public interface OnAddClickListener { public void onItemClick(int position,String actionType); //0為跳轉到檢查內容頁面}public void setOnAddClickListener(OnAddClickListener onItemAddClick) { this.onItemAddClick = onItemAddClick;}

2、在adapter中定義各個按鈕的點選事件的方法

hold.custom_name_linearout.setOnClickListener(new View.OnClickListener() {
    @Override
public void onClick(View view) {
        //代表傳遞的是電話按鈕
onItemAddClick.onItemClick(position,"0");
    }
});
//右側的撥打電話功能
hold.dialing_phone.setOnClickListener(new View.OnClickListener() {
    @Override
public void onClick(View view) { //代表傳遞的是電話按鈕 onItemAddClick.onItemClick(position,"1"); } });
3、在activity中確定點選的按鈕事件
//點選列表顯示的內容
adapter.setOnAddClickListener(new MainCustomAdapter.OnAddClickListener() {
    @Override
public void onItemClick(int position, String actionType) {
        //說明點選的是左側的按鈕
if(actionType.equals("0")){ Intent intent = new Intent(MainCustomActivity.this, CustomInfoActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); startActivity(intent); overridePendingTransition(Animation.INFINITE, Animation.INFINITE); //點選的撥打電話功能 }else if(actionType.equals("1")){ } } });