我創建了自定義對話框,並希望將其綁定到我的列表項目。 我想要這個對話框長時間點擊列表項時的行爲就像上下文菜單一樣。打開對話框正好在上下文菜單上單擊項目
換句話說,我不希望這個對話框出現在屏幕的中心,而是出現在列表中的項目附近。
我花了很多時間尋找方式,但不幸的是沒有結果。有一些很好的解決方案嗎?
我創建了自定義對話框,並希望將其綁定到我的列表項目。 我想要這個對話框長時間點擊列表項時的行爲就像上下文菜單一樣。打開對話框正好在上下文菜單上單擊項目
換句話說,我不希望這個對話框出現在屏幕的中心,而是出現在列表中的項目附近。
我花了很多時間尋找方式,但不幸的是沒有結果。有一些很好的解決方案嗎?
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// infalte menu
PopupMenu popup = new PopupMenu(activity, v);
popup.getMenuInflater().inflate(R.menu.clipboard_popup,
popup.getMenu());
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.install:
// Or Some other code you want to put
// here.. This is just an example.
Toast.makeText(
activity,
" Install Clicked at position " + " : "
+ position, Toast.LENGTH_LONG)
.show();
break;
case R.id.addtowishlist:
Toast.makeText(
activity,
"Add to Wish List Clicked at position "
+ " : " + position,
Toast.LENGTH_LONG).show();
break;
default:
break;
}
return true;
}
});
}
});
更新的答案與自定義佈局
PopupWindow popupwindow_obj = popupDisplay();
popupwindow_obj.showAsDropDown(clickbtn, -40, 18);
// where u want show on
//view click event popupwindow.showAsDropDown(view, x, y);
public PopupWindow popupDisplay()
{
final PopupWindow popupWindow = new PopupWindow(this);
// inflate your layout or dynamically add view
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.mylayout, null);
Button item = (LinearLayout) view.findViewById(R.id.button1);
popupWindow.setFocusable(true);
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(view);
return popupWindow;
}
mylayout.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Window test" />
</LinearLayout>
我不想使用菜單!我有一個自定義佈局,其中包含3個按鈕和水平方向 –
我的問題是在這裏:popup.getMenuInflater()。inflate(R.menu.clipboard_popup, popup.getMenu()); –
它不是一個菜單!其佈局 –
如果你知道你想你的對話框出現,你可以做這樣的位置(注:X:左 - >右Y:頂部 - >底部)
AlertDialog dialog = builder.create();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams abc= dialog.getWindow().getAttributes();
abc.gravity = Gravity.TOP | Gravity.LEFT;
abc.x = 100; //x position
abc.y = 100; //y position
dialog.show();
我不知道如何計算列表中單擊項目的位置查看:/ –
您有listview的ID。檢查它的參數。這是onItemClick方法的第四個參數。 – gRaWEty
使用'PopupWindow'可以幫助您在視圖上顯示窗口。 –
謝謝,我會搜索它 –
http://rajeshandroiddeveloper.blogspot.in/2013/07/android-popupwindow-example-in-listview.html –