Android實現長按QQ列表時快顯功能表效果

來源:互聯網
上載者:User

標籤:

這裡只是簡單講述原理,實現很簡易功能,有興趣的讀者可自行最佳化修改


布局檔案

<?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列表時快顯功能表效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.