在android中的recyclerView中显示弹出按钮的确切位置
作者:互联网
我正在使用android中的recyclerView处理gridLayout.每个网格项都有一个选项,我想在其中显示另一个类的弹出活动.请看图片-
每个项目都有一个菜单选项.我的弹出活动Java名称是CustomPop.Class.我使用一个recyclerView来显示gridViews,它的holder方法就像
public void onBindViewHolder(ViewHolder holder, int position) {
holder.img_chatroom_menu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), ChatroomPopup.class);
view.getContext().startActivity(intent);
}
});
}
这样,我可以显示弹出活动.但是问题是它出现在活动中间,但我希望它在菜单选项旁边打开.
解决方法:
as per my above comment可以使用Popup Menu
Android Popup Menu displays the menu below the anchor text if space is available otherwise above the anchor text. It disappears if you click outside the popup menu.
试试这个创建菜单文件
文件:poupup_menu.xml
<menu xmlns:androclass="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/one"
android:title="One"/>
<item
android:id="@+id/two"
android:title="Two"/>
<item
android:id="@+id/three"
android:title="Three"/>
</menu>
比使用像这样的创建弹出菜单
holder.img_chatroom_menu.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainActivity.this, button1);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();//showing popup menu
}
});//closing the setOnClickListener method
这是示例演示链接how to create pop-up menu in android
标签:android,android-recyclerview,android-popupwindow 来源: https://codeday.me/bug/20191014/1911838.html