Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 趙雅智_AndroidUI_listview的item滑動刪除效果

趙雅智_AndroidUI_listview的item滑動刪除效果

編輯:關於Android編程

package com.example.listsildedel;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {

    private ListView mListView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mListView = (ListView) this.findViewById(R.id.list);
        List list = new ArrayList();
        for(int i=0;i<10;i++){
            list.add("選項"+i);
        }
        //實例化自定義內容適配類
        MyAdapter adapter = new MyAdapter(this, this,list);
        //為listView設置適配
        mListView.setAdapter(adapter);
    }

    public void openActivity(){
        Intent intent  = new Intent(this, TestActivity.class);
        startActivity(intent);
    }
    
}


adapter

package com.example.listsildedel;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {

    private List arrays = null;
    private Context mContext;
    private Button curDel_btn;
    private float x,ux;
    private MainActivity mActivity;
    
    public MyAdapter(MainActivity activity, Context mContext, List arrays) {
        this.mContext = mContext;
        this.arrays = arrays;
        mActivity = activity;

    }

    public int getCount() {
        return this.arrays.size();
    }

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

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View view, ViewGroup arg2) {
        ViewHolder viewHolder = null;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item, null);
            viewHolder.tvTitle = (TextView) view.findViewById(R.id.title);
            viewHolder.btnDel = (Button) view.findViewById(R.id.del);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        //換掉了原來listview中的onItemClick
        view.setOnClickListener(new OnClickListener() {
            
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mActivity.openActivity();
            }
        });


        //為每一個view項設置觸控監聽
        view.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                
                final ViewHolder holder = (ViewHolder) v.getTag();
                
                //當按下時處理
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    
//                    //設置背景為選中狀態
//                    v.setBackgroundResource(R.drawable.mm_listitem_pressed);
                    //獲取按下時的x軸坐標
                    x = event.getX();
                    //判斷之前是否出現了刪除按鈕如果存在就隱藏
                    if (curDel_btn != null) {
                        if(curDel_btn.getVisibility() == View.VISIBLE){
                            curDel_btn.setVisibility(View.GONE);
                            return true;
                        }
                    }
                    
                } else if (event.getAction() == MotionEvent.ACTION_UP) {// 松開處理
                    
                    //設置背景為未選中正常狀態
                    //v.setBackgroundResource(R.drawable.mm_listitem_simple);
                    //獲取松開時的x坐標
                    ux = event.getX();
                    
                    //判斷當前項中按鈕控件不為空時
                    if (holder.btnDel != null) {
                        
                        //按下和松開絕對值差當大於20時顯示刪除按鈕,否則不顯示
                        
                        if (Math.abs(x - ux) > 20) {
                            holder.btnDel.setVisibility(View.VISIBLE);
                            curDel_btn = holder.btnDel;
                            return true;
                        }
                    }
                } else if (event.getAction() == MotionEvent.ACTION_MOVE) {//當滑動時背景為選中狀態
                    return true;
                    //v.setBackgroundResource(R.drawable.mm_listitem_pressed);
                    
                    
                } else {//其他模式
                    //設置背景為未選中正常狀態
                    //v.setBackgroundResource(R.drawable.mm_listitem_simple);
                    
                }

                return false;
            }
        });
        viewHolder.tvTitle.setText(this.arrays.get(position));
        
        //為刪除按鈕添加監聽事件,實現點擊刪除按鈕時刪除該項
        viewHolder.btnDel.setOnClickListener(new OnClickListener() {
            
            public void onClick(View v) {
                if(curDel_btn!=null)
                    curDel_btn.setVisibility(View.GONE);
                arrays.remove(position);
                notifyDataSetChanged();
                
            }
        });
        return view;

    }

    final static class ViewHolder {
        TextView tvTitle;
        Button btnDel;
    }
}

\




  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved