Android实现长按QQ列表时弹出菜单效果

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


布局文件

<?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;
	}
}

效果如下

技术分享


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。