Android QQ列表长按弹出PopWindow菜单

原理

这里只是简单讲述原理,实现很简单功能,有兴趣的读者可自行优化修改。

popWindow具有在屏幕上绝对定位的能力,因此,我们主要利用popWindow的showAtLocation实现pop菜单的弹出,此外还需要通过View.getLocationAt获取view在在屏幕中的位置信息。

代码实战

布局文件

<?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" >

    <ListView
        android:id="@+id/data_list"
        android:layout_width="match_parent"
        android:divider="@android:color/black"
        android:dividerHeight="0.65dip"
        android:layout_height="wrap_content" >
    </ListView>
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/data_empty_li"
        android:orientation="vertical"
        android:gravity="center"
        >
        
         <ImageView
            android:layout_marginTop="100dip"
	        android:id="@+id/imageView1"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dip"
        android:text="Click to Refresh" />
        
    </LinearLayout>


</LinearLayout>

关键代码

public class WindowTipActivity  extends Activity implements OnClickListener, OnItemLongClickListener, OnTouchListener
{
	
	private ListView mListView;
	
	private ViewGroup mNoDataPanel;
	
	private final List<String> dataList = new ArrayList<String>();
	
	private Point pRaw = new Point();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wintip_list_layout);
		
		mListView = (ListView) findViewById(R.id.data_list);
		mNoDataPanel = (ViewGroup) findViewById(R.id.data_empty_li);
		
		mListView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , dataList));
		mListView.setEmptyView(mNoDataPanel);
		
		mNoDataPanel.setOnClickListener(this);
		
		mListView.setOnItemLongClickListener(this);
		mListView.setOnTouchListener(this);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onClick(View arg0)
	{
		if(dataList.size()>=20)
		{
			return;
		}
		for (int i = 1; i <=20; i++) 
		{
			dataList.add("菜单-选项("+i+")");
		}
		
		ArrayAdapter adapter = (ArrayAdapter) mListView.getAdapter();
		adapter.notifyDataSetChanged();
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View v, int position,long itemId)
	{
		
		    LinearLayout layout = new LinearLayout(this); 
		    layout.setGravity(Gravity.CENTER);
	        layout.setBackgroundColor(Color.GRAY);  
	        TextView tv = new TextView(this);  
	        tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
	        tv.setText(dataList.get(position));  
	        tv.setTextColor(Color.WHITE);
	        tv.setSingleLine(true);
	        layout.addView(tv);  
	        
	        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
	        
	        PopupWindow popupWindow = new PopupWindow(layout,200,v.getHeight());  
	          
	        popupWindow.setFocusable(true);  
	        popupWindow.setOutsideTouchable(true);  
	        popupWindow.setBackgroundDrawable(new BitmapDrawable());  
	          
	        int[] location = new int[2];  
	        v.getLocationOnScreen(location);  
	          
	        popupWindow.showAtLocation(v, Gravity.NO_GRAVITY,parent.getWidth()/2-100 , location[1]-popupWindow.getHeight());  
	        
		return false;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event)
	{
		if(event.getAction()==MotionEvent.ACTION_DOWN)
		{
			int x = (int) event.getRawX();
			int y = (int) event.getRawY();
			pRaw.set(x, y);
			Log.d("onTouch", "x="+x+" , y="+y);
		}
		
		return false;
	}
}

 

效果

111150_6uSV_2256215.png

 

转载于:https://my.oschina.net/ososchina/blog/394569

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值