package com.example.administrator.myapplication; import android.graphics.drawable.ColorDrawable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { PopupWindow mPopupWindow; int screenHeight; int screenWidth; int downX; int downY; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 獲取屏幕的高寬 screenHeight = getResources().getDisplayMetrics().heightPixels; screenWidth = getResources().getDisplayMetrics().widthPixels; ListView mListView = (ListView) findViewById(R.id.listview); mListView.setAdapter(new CustomAdapter()); } private void showPopupWindow(final View anchorView) { View contentView = LayoutInflater.from(this).inflate(R.layout.popup_content_layout, null); View.OnClickListener menuItemOnClickListener = new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(v.getContext(), "Click " + ((TextView) v).getText(), Toast.LENGTH_SHORT).show(); if (mPopupWindow != null) { mPopupWindow.dismiss(); } } }; contentView.findViewById(R.id.menu_item1).setOnClickListener(menuItemOnClickListener); contentView.findViewById(R.id.menu_item2).setOnClickListener(menuItemOnClickListener); contentView.findViewById(R.id.menu_item3).setOnClickListener(menuItemOnClickListener); contentView.findViewById(R.id.menu_item4).setOnClickListener(menuItemOnClickListener); contentView.findViewById(R.id.menu_item5).setOnClickListener(menuItemOnClickListener); mPopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); mPopupWindow.setBackgroundDrawable(new ColorDrawable()); //計算View位置 final int windowPos[] = new int[2]; final int anchorLoc[] = new int[2]; // 獲取錨點View在屏幕上的左上角坐標位置 contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); // 計算contentView的高寬 final int popuHeight = contentView.getMeasuredHeight(); final int popuWidth = contentView.getMeasuredWidth(); // 判斷Y坐標 if (downY > screenHeight / 2) { //向上彈出 windowPos[1] = downY - popuHeight; } else { //向下彈出 windowPos[1] = downY; } // 判斷X坐標 if (downX > screenWidth / 2) { //向左彈出 windowPos[0] = downX - popuWidth; } else { //向右彈出 windowPos[0] = downX; } int xOff = -20; // 調(diào)整偏移 windowPos[0] -= xOff; mPopupWindow.showAtLocation(anchorView, Gravity.TOP | Gravity.START, windowPos[0], windowPos[1]); } class CustomAdapter extends BaseAdapter { @Override public int getCount() { return 20; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { final ViewHolder viewHolder; if (convertView == null) { convertView = LayoutInflater.from(getBaseContext()).inflate(R.layout.listview_item, null); viewHolder = new ViewHolder(); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.Root = convertView.findViewById(R.id.root); viewHolder.Root.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { downX = (int) motionEvent.getRawX(); downY = (int) motionEvent.getRawY(); return false; } }); viewHolder.Root.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { showPopupWindow(viewHolder.Root); return false; } }); return convertView; } } class ViewHolder { View Root; } } <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas./apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_normal_bg" android:orientation="vertical" android:padding="1dp"> <LinearLayout android:layout_width="110dp" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/menu_item1" android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center" android:text="復制" android:textColor="#000"/> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#ccc"/> <TextView android:id="@+id/menu_item2" android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center" android:text="撤回" android:textColor="#000"/> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#ccc"/> <TextView android:id="@+id/menu_item3" android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center" android:text="轉(zhuǎn)發(fā)" android:textColor="#000"/> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#ccc"/> <TextView android:id="@+id/menu_item4" android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center" android:text="收藏" android:textColor="#000"/> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#ccc"/> <TextView android:id="@+id/menu_item5" android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center" android:text="刪除" android:textColor="#000"/> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#ccc"/> <TextView android:id="@+id/menu_item6" android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center" android:text="更多" android:textColor="#000"/> </LinearLayout> </LinearLayout> <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas./apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/root" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="15dp" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="標題標題" android:textColor="@android:color/black" android:textSize="18sp" /> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:text="內(nèi)內(nèi)容內(nèi)容測試試內(nèi)容測試測試" android:textColor="@android:color/darker_gray" android:textSize="14sp" /> </LinearLayout> <LinearLayout android:id="@+id/more_root" android:layout_width="wrap_content" android:layout_height="50dp" android:gravity="center" android:layout_marginRight="5dp" android:layout_gravity="right|center_vertical"> <ImageView android:id="@+id/more_imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_launcher" /> </LinearLayout> </FrameLayout> |
|
來自: DeepReading > 《待分類》