Android实现类似支付宝上的长按出现删除和置顶操作


效果图如下:


activity_list_pop.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ListPopActivity" >

    <ListView
        android:id="@+id/lv_test"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</LinearLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <View
        android:id="@+id/v_ancho"
        android:layout_width="match_parent"
        android:layout_height="0dp" />

    <TextView
        android:id="@+id/txt_name"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="10dp"
        android:gravity="center_vertical"
        android:text="姓名"
        android:textSize="20sp" />

</LinearLayout>

list_item_opt.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" 
    android:gravity="center_horizontal"
    android:background="#AADDDDDD"
    >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="置顶" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="删除" />

</LinearLayout>


主要代码如下:启动Activity

package com.example.finger;

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

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class ListPopActivity extends Activity {
	private ListView listView;
	PopupWindow pop;
	List<String> mData;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_pop);
		// 引入窗口配置文件
		LayoutInflater inflater = LayoutInflater.from(this);
		View view = inflater.inflate(R.layout.list_item_opt, null);
		// 创建PopupWindow对象
		pop = new PopupWindow(view, LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT, false);
		// 需要设置一下此参数,点击外边可消失
		pop.setBackgroundDrawable(new BitmapDrawable());
		// 设置点击窗口外边窗口消失
		pop.setOutsideTouchable(true);
		// 设置此参数获得焦点,否则无法点击
		pop.setFocusable(true);
		mData=getData();
		listView = (ListView) findViewById(R.id.lv_test);
		//listView.setAdapter(new ArrayAdapter<String>(this,
				//android.R.layout.simple_expandable_list_item_1, getData()));
		listView.setAdapter(new MyAdapter(this));
		
//		listView.setOnItemLongClickListener(new OnItemLongClickListener() {
//
//			@Override
//			public boolean onItemLongClick(AdapterView<?> parent, View view,
//					int position, long id) {
//				// Toast.makeText(ListPopActivity.this, "tt",
//				// Toast.LENGTH_SHORT).show();
//				Log.i("aa", view.getLeft() + "," + view.getTop());
//
//				// pop.showAtLocation(view.getParent()., Gravity.AXIS_CLIP, 0,
//				// 0);
//				// pop.showAsDropDown(view);
//				return true;
//			}
//
//		});

	}

	private List<String> getData() {

		List<String> data = new ArrayList<String>();
		data.add("测试数据1");
		data.add("测试数据2");
		data.add("测试数据3");
		data.add("测试数据4");
		return data;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.list_pop, menu);
		return true;
	}

	public final class ViewHolder {
		public View v_ancho;
		public TextView name;
	}

	public class MyAdapter extends BaseAdapter {

		private LayoutInflater mInflater;

		public MyAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return mData.size();
		}

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

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

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder = null;
			if (convertView == null) {

				holder = new ViewHolder();

				convertView = mInflater.inflate(R.layout.list_item, null);
				holder.v_ancho = convertView.findViewById(R.id.v_ancho);
				holder.name = (TextView) convertView
						.findViewById(R.id.txt_name);
				convertView.setTag(holder);

			} else {

				holder = (ViewHolder) convertView.getTag();
			}

			holder.name.setText((String) mData.get(position));
			convertView.setOnLongClickListener(new OnLongClickListener() {

				@Override
				public boolean onLongClick(View v) {
					pop.showAsDropDown(v.findViewById(R.id.v_ancho));
					return true;
				}
			});

			return convertView;
		}

	}
}


展开阅读全文

没有更多推荐了,返回首页