標籤:
這裡只是簡單講述原理,實現很簡易功能,有興趣的讀者可自行最佳化修改
布局檔案
<?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();@Overrideprotected 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);}@Overrideprotected void onDestroy() {super.onDestroy();}@Overridepublic 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();}@Overridepublic 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;}@Overridepublic 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;}}
效果如下
Android實現長按QQ列表時快顯功能表效果