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

來源:互聯網
上載者:User

標籤:android   listview   划動刪除   

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<String> list = new ArrayList<String>();        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<String> arrays = null;    private Context mContext;    private Button curDel_btn;    private float x,ux;    private MainActivity mActivity;        public MyAdapter(MainActivity activity, Context mContext, List<String> 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        <span style="font-size:18px;"><strong>view.setOnClickListener(new OnClickListener() {                        public void onClick(View v) {                // TODO Auto-generated method stub                mActivity.openActivity();            }        });</strong></span>        //為每一個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;    }}





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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.