1. 程式人生 > >ListView長按刪除效果

ListView長按刪除效果

主要是使用了對ListView的長按和點選,長按的時候顯示確認檢視,在裡面有刪除和取消選項。

效果:

長按的時候,顯示刪除和取消介面,點選刪除就刪除該項,取消就隱藏顯示的刪除和取消檢視。

顯示了刪除和取消檢視,點選其他選項就隱藏顯示的檢視。

活動:

public class MainActivity extends Activity {
    private MainActivity mContext;
    private int lastPress = 0;
    private boolean delState = false;
    private List<String> curList = new ArrayList<>();

    private ListView curListView;
    private CurAdapter adapter;

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

    private void init() {
        mContext = this;
        String[] strings = {"aa", "bb", "cc", "dd", "ee"};
        for (String s : strings) {
            curList.add(s);
        }

        curListView = (ListView) findViewById(R.id.lv_contents);
        adapter = new CurAdapter();
        curListView.setAdapter(adapter);

        curListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (delState) {
                    if (lastPress < parent.getCount()) {
                        View delview = parent.getChildAt(lastPress).findViewById(R.id.linear_del);
                        if (null != delview) {
                            delview.setVisibility(View.GONE);
                        }
                    }
                    delState = false;
                    return;
                } else {
                    Log.d("click:", position + "");
                }
//                lastPress = position;
            }
        });
        curListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            private View delview;

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                if (lastPress < parent.getCount()) {
                    delview = parent.getChildAt(lastPress).findViewById(R.id.linear_del);
                    if (null != delview) {
                        delview.setVisibility(View.GONE);
                    }
                }

                delview = view.findViewById(R.id.linear_del);
                delview.setVisibility(View.VISIBLE);

                delview.findViewById(R.id.tv_del).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        delview.setVisibility(View.GONE);
                        curList.remove(position);
                        adapter.notifyDataSetChanged();
                    }
                });
                delview.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        delview.setVisibility(View.GONE);
                    }
                });

                lastPress = position;
                delState = true;
                return true;
            }
        });
    }

    private class CurAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return curList.size();
        }

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (null == convertView) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_content_delete, null);
            }

            ((TextView) convertView.findViewById(R.id.tv_content)).setText(curList.get(position));
            return convertView;
        }
    }
}
Listivw行佈局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#FFFFFF"
    android:layout_height="40dp">

    <RelativeLayout
        android:id="@+id/rela_content"
        android:layout_width="match_parent"
        android:visibility="visible"
        android:layout_height="40dp">

        <TextView
            android:id="@+id/tv_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="content"
            android:textColor="#001122"
            android:gravity="center" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/linear_del"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:visibility="gone"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_del"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="刪除"
            android:textColor="#000000"
            android:background="#00ff00"
            android:gravity="center"
            android:layout_height="match_parent" />

        <TextView
            android:id="@+id/tv_cancel"
            android:layout_width="0dp"
            android:text="取消"
            android:textColor="#ffffff"
            android:background="#0000ff"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_height="match_parent" />
    </LinearLayout>
</RelativeLayout>
顯示效果: