У меня есть вид, который содержится во фрагменте. Представление содержит несколько «кнопок» (каждая «кнопка» является ImageView) (см. Экран 1)
Экран 1
Моя цель заключается в том, чтобы при нажатии на одну из этих кнопок появлялось всплывающее меню (см. Экран 2).
Экран 2
В большинстве случаев это работает хорошо, но на некоторых «кнопках», когда нажимается «кнопка», PopupMenu обычно не отображается (хотя появляется всплывающее сообщение, указывающее, что «cick» был зарегистрирован).
Проблема проявляется в «кнопке» в верхнем левом углу и в кнопках внизу фрагмента. Иногда появляется меню (но обычно после многих «кликов»)
Мое рабочее предположение заключается в том, что по какой-то причине меню не отображается на экране (я считаю, что оно отображается, но находится за пределами экрана / не отображается).
Я исследовал проблему, но не могу найти никого с подобной проблемой.
Я пробовал разные настройки Gravity при создании PopupMenu, но ничего не изменило.
Код, который я использую для создания PopupMenu, выглядит классическим.
Заранее спасибо за любую помощь, которую вы можете предложить мне по этому вопросу.
Извлечь из RemoteView.java
// Constructors
public RemoteView(Context context) {
super(context);
init(context);
}
public RemoteView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public RemoteView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
rootview = inflate(context, R.layout.remote, this);
}
public class IconViewOnTouchListener implements View.OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (!edit) {
actionDOWN(v);
}
break;
case MotionEvent.ACTION_UP:
KeyMap keyMap=(KeyMap) v.getTag();
if (edit) {
if (movingButton) {
toIndex=keyMap.AMKeyItem;
toKey=keyMap.keyIndex;
exchangeButtons();
} else {
//popup edit options
if (keyMap.AMKeyItem >= 0) showPopup(v);
}
} else {
actionUP(v);
performClick();
}
break;
}
return true;
}
}
private void showPopup(final View view) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(context, view, Gravity.RIGHT, R.attr.actionOverflowMenuStyle, 0);
//Inflating the Popup using xml file
popup.getMenuInflater()
.inflate(R.menu.edit_button_menu, popup.getMenu());
Toast.makeText(context, "Popup Menu is visible",
Toast.LENGTH_SHORT).show();
popup.show(); //showing popup menu
}
remote.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<com.andy.andymote.CommunicatorView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/remote_communicator">
</com.andy.andymote.CommunicatorView>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/buttons"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="5"
android:gravity="center"
android:stretchMode="spacingWidthUniform"
>
</GridView>
</RelativeLayout>
</merge>
frag_device.xml
<?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:layout_gravity="top"
android:orientation="vertical"
>
<com.andy.andymote.RemoteView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/remote">
</com.andy.andymote.RemoteView>
</LinearLayout>
edit_button_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/moveButton"
android:title="Move"/>
<item
android:id="@+id/editButton"
android:title="Edit"/>
<item
android:id="@+id/insertRow"
android:title="Insert Row Above"/>
</menu>
Извлечение из DeviceACTIVITY.java
private void loadFragment(){
device = new DeviceFRAGMENT();
device.setDevice(GLOBALS.deviceList.getDevice(deviceName));
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
device).commit();
}
Извлечение из DeviceFRAGMENT.java
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.frag_device, container, false);
//Declare Widgets
remote = view.findViewById(R.id.remote);
remote.setKeys(device.getRemote());
return view;
}
RemoteKeysAdapter.java
import static com.andy.andymote.GLOBALS.iconSize;
public class RemoteKeysAdapter extends BaseAdapter {
private final Context mContext;
private final List<RemoteButton> remoteButtons;
private _resourceAccess resource;
private RemoteView.IconViewOnTouchListener iconViewOnTouchListener;
private Remote remote;
// 1
public RemoteKeysAdapter(Context context, Remote remote, List<RemoteButton> remoteButtons,
RemoteView.IconViewOnTouchListener iconViewOnTouchListener) {
this.mContext = context;
this.remote=remote;
this.remoteButtons = remoteButtons;
this.iconViewOnTouchListener=iconViewOnTouchListener;
resource = new _resourceAccess(context);
}
// 2
@Override
public int getCount() {
return remoteButtons.size();
}
// 3
@Override
public long getItemId(int position) {
return 0;
}
// 4
@Override
public Object getItem(int position) {
return null;
}
// 5
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Find the proper remoteButton for this cell by using the position index
final RemoteButton remoteButton = remoteButtons.get(position);
if (convertView == null) {
final LayoutInflater layoutInflater = LayoutInflater.from(mContext);
convertView = layoutInflater.inflate(R.layout.remote_button, null);
}
// create reference for the image in the XML layout file.
final AppCompatImageView imageView = convertView.findViewById(R.id.button);
// Set the image
int indexOfAndyMoteKeyItem=remoteButton.getFromToIndex();
if (indexOfAndyMoteKeyItem == -1){
imageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_nobutton));
} else {
AndyMoteKeyItem andyMoteKeyItem=remote.getRemoteLayout().get(indexOfAndyMoteKeyItem);
if (andyMoteKeyItem.getLircKeyItem().getIcon().length() == 0) {
displayText(imageView, andyMoteKeyItem.getLircKeyItem().getText());
} else {
displayIcon(imageView, andyMoteKeyItem.getLircKeyItem().getIcon());
}
}
KeyMap keyMap=new KeyMap();
keyMap.AMKeyItem=indexOfAndyMoteKeyItem;
keyMap.keyIndex=position;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.gravity= Gravity.CENTER;
params.width=iconSize;
params.height=iconSize;
convertView.setLayoutParams(params);
convertView.setOnTouchListener(iconViewOnTouchListener);
convertView.setPadding(16, 0, 0, 16);
convertView.setVisibility(remoteButton.getVisibility());
convertView.setTag(keyMap);
return convertView;
}
private void displayIcon(AppCompatImageView imageView, String icon) {
clIcon newIcon = factory.createIcon(icon);
imageView.setImageDrawable(resource.getDrawableFromUri(newIcon.getURI(mContext)));
}
private void displayText(AppCompatImageView imageView, String text) {
//https://github.com/amulyakhare/TextDrawable
TextDrawable drawable = TextDrawable.builder().beginConfig()
.textColor(Color.WHITE)
.bold()
.withBorder(4)
.useFont(Typeface.DEFAULT)
.fontSize(30) /* size in px */
.endConfig()
.buildRound(text, Color.DKGRAY);
imageView.setImageDrawable(drawable);
}
}