实现长按item弹出菜单
话不多说,看源码
MainActivity
package com.bjyz.test;
import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.Text;
import android.R.integer;
import android.R.menu;
import android.R.string;
import android.nfc.Tag;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements
android.view.View.OnClickListener {
private ListView listview;
public static Adapter adapter;
private int a;
private Dialog dialog;
private List- adapterList = new ArrayList
- ();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
// 添加list内容
adapterList.add(new Item(false));
adapterList.add(new Item(false));
adapterList.add(new Item(false));
adapterList.add(new Item(false));
adapterList.add(new Item(false));
adapterList.add(new Item(false));
adapterList.add(new Item(false));
adapterList.add(new Item(false));
adapterList.add(new Item(false));
adapterList.add(new Item(false));
adapterList.add(new Item(false));
adapterList.add(new Item(false));
adapterList.add(new Item(false));
adapterList.add(new Item(false));
adapterList.add(new Item(false));
}
private void init() {
listview = (ListView) findViewById(R.id.listView1);
adapter = new Adapter(this, R.layout.item, adapterList);
listview.setAdapter(adapter);// 关联适配器
/*
* 使用activity实现弹窗
*/
listview.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView> parent, View view,
int position, long id) {
Intent intent=new Intent(MainActivity.this,MenuWindow.class);
intent.putExtra("position", position);
startActivity(intent);
return false;
}
});
}
MenuWindow.java
package com.bjyz.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MenuWindow extends Activity implements OnClickListener {
private TextView Execute_OR_cancel;
private TextView message;
private TextView breakOff;
private TextView into;
private TextView output_quantity;
private int position;
// private Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
init();
}
private void init() {
Execute_OR_cancel = (TextView) findViewById(R.id.Execute_OR_cancel1);
message = (TextView) findViewById(R.id.message1);
breakOff = (TextView) findViewById(R.id.breakOff1);
into = (TextView) findViewById(R.id.into1);
output_quantity = (TextView) findViewById(R.id.output_quantity1);
// 获取当前点击item的position
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
position=bundle.getInt("position");
//获取当前界面,将界面至于底部并修改其大小
Window window= this.getWindow();
window.setGravity(Gravity.BOTTOM);
WindowManager.LayoutParams layoutParams=window.getAttributes();//获取当前窗口参数
layoutParams.x=0;//新位置的x坐标
layoutParams.y=0;//新位置的y坐标
layoutParams.width=getResources().getDisplayMetrics().widthPixels; //宽度
window.setAttributes(layoutParams);
Execute_OR_cancel.setOnClickListener(this);
message.setOnClickListener(this);
breakOff.setOnClickListener(this);
into.setOnClickListener(this);
output_quantity.setOnClickListener(this);
}
// 实现onTouchEvent触屏函数但点击屏幕时销毁本Activity
@Override
public boolean onTouchEvent(MotionEvent event) {
finish();
return true;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Execute_OR_cancel1:
if (MainActivity.adapter.getItem(position).getCheckBox() == false) {
MainActivity.adapter.getItem(position).setCheckBox(
true);
} else {
MainActivity.adapter.getItem(position).setCheckBox(
false);
}
MainActivity.adapter.notifyDataSetChanged();
break;
case R.id.message1:
Toast.makeText(this, "详细信息", Toast.LENGTH_LONG).show();
break;
case R.id.breakOff1:
Toast.makeText(this, "中断执行", Toast.LENGTH_LONG).show();
break;
case R.id.into1:
Toast.makeText(this, "转入记录", Toast.LENGTH_LONG).show();
break;
case R.id.output_quantity1:
Toast.makeText(this, "转入输出量", Toast.LENGTH_LONG).show();
break;
default:
break;
}
finish();
}
}
Adapter.java
package com.bjyz.test;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
public class Adapter extends BaseAdapter{
private Context context;
private int resource;
private List- adapterList;
public Adapter(Context context, int resource,List
- adapterList) {
this.context = context;
this.resource = resource;
this.adapterList = adapterList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
Item item= getItem(position);
if(convertView==null){
convertView=View.inflate(context, resource, null);
viewHolder= new ViewHolder();
viewHolder.checkBox=(CheckBox)convertView.findViewById(R.id.checkBox);
convertView.setTag(viewHolder);//将viewHolder对象存储在view
}
else {
viewHolder=(ViewHolder)convertView.getTag();//重新获取viewHolder
}
viewHolder.checkBox.setChecked(item.getCheckBox());
return convertView ;
}
class ViewHolder{
CheckBox checkBox;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return adapterList.size();
}
@Override
public Item getItem(int position) {
// TODO Auto-generated method stub
return adapterList.get(position);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
}
Item.java
package com.bjyz.test;
public class Item {
private Boolean checkBox;
public Item(Boolean checkBox){
this.checkBox=checkBox;
}
public Boolean getCheckBox() {
return this.checkBox;
}
public void setCheckBox(Boolean checkBox) {
this.checkBox = checkBox;
}
}
activity_main.xml
item.xml
menu.xml
注意要在AndroidManifest.xml文件中对MenuWindow Activity文件进行注册
在values 中的styles.xml添加Activity样式
同时添加进出动画
在res下新建anim文件夹,放入动画文件
push_bottom_in.xml
push_buttom_out.xml