RTL - Step 3 (PopUp Menu)

This commit is contained in:
Nazar-Kutz 2020-06-11 15:24:54 +03:00
parent 312d5775ee
commit cb105deda0
4 changed files with 188 additions and 40 deletions

View file

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/bottom_sheet_list_item_height">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/icon"
android:layout_width="@dimen/standard_icon_size"
android:layout_height="@dimen/standard_icon_size"
android:layout_gravity="center_vertical"
android:layout_marginLeft="@dimen/content_padding_half"
android:layout_marginStart="@dimen/content_padding_half"
tools:src="@drawable/ic_action_info_dark"
android:tint="?attr/primary_icon_color"/>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="?android:textColorPrimary"
android:layout_gravity="center_vertical"
android:paddingLeft="@dimen/content_padding"
android:paddingRight="@dimen/content_padding"
android:paddingTop="@dimen/content_padding_small"
android:paddingBottom="@dimen/content_padding_small"
android:textSize="@dimen/default_list_text_size"
tools:text="Title" />
</LinearLayout>

View file

@ -0,0 +1,78 @@
package net.osmand.plus;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.List;
public class SimplePopUpMenuItemAdapter
extends ArrayAdapter<SimplePopUpMenuItemAdapter.SimplePopUpMenuItem> {
private List<SimplePopUpMenuItem> items;
public SimplePopUpMenuItemAdapter(@NonNull Context context, int resource,
List<SimplePopUpMenuItem> items) {
super(context, resource);
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
if (convertView == null) {
convertView = inflater.inflate(R.layout.popup_menu_item, null);
}
SimplePopUpMenuItem item = getItem(position);
if (item != null) {
TextView tvTitle = convertView.findViewById(R.id.title);
tvTitle.setText(item.title);
ImageView ivIcon = convertView.findViewById(R.id.icon);
Drawable icon = item.icon;
if (icon != null) {
ivIcon.setImageDrawable(icon);
} else {
ivIcon.setVisibility(View.GONE);
}
}
return convertView;
}
@Nullable
@Override
public SimplePopUpMenuItem getItem(int position) {
return items.get(position);
}
public static class SimplePopUpMenuItem {
private CharSequence title;
private Drawable icon;
public SimplePopUpMenuItem(CharSequence title, Drawable icon) {
this.title = title;
this.icon = icon;
}
public CharSequence getTitle() {
return title;
}
public Drawable getIcon() {
return icon;
}
}
}

View file

@ -3,6 +3,7 @@ package net.osmand.plus;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
@ -19,6 +20,7 @@ import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
@ -31,6 +33,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.view.ContextThemeWrapper;
import androidx.appcompat.widget.ListPopupWindow;
import androidx.appcompat.widget.SwitchCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
@ -52,6 +55,8 @@ import net.osmand.plus.widgets.TextViewEx;
import org.apache.commons.logging.Log;
import java.util.List;
import gnu.trove.map.hash.TLongObjectHashMap;
public class UiUtilities {
@ -645,4 +650,43 @@ public class UiUtilities {
return spannable;
}
}
public static ListPopupWindow createListPopupWindow(Context themedCtx, View v,
List<SimplePopUpMenuItemAdapter.SimplePopUpMenuItem> items,
final AdapterView.OnItemClickListener listener) {
int contentPadding = themedCtx.getResources().getDimensionPixelSize(R.dimen.content_padding);
int contentPaddingHalf = themedCtx.getResources().getDimensionPixelSize(R.dimen.content_padding_half);
Paint paint = new Paint();
paint.setTextSize(themedCtx.getResources().getDimensionPixelSize(R.dimen.default_list_text_size));
CharSequence longestTitle = "";
for (SimplePopUpMenuItemAdapter.SimplePopUpMenuItem item : items) {
if (item.getTitle().length() > longestTitle.length()) {
longestTitle = item.getTitle();
}
}
float titleTextWidth = paint.measureText(longestTitle.toString());
float itemWidth = titleTextWidth + contentPadding;
float minWidth = v.getWidth();
SimplePopUpMenuItemAdapter adapter =
new SimplePopUpMenuItemAdapter(themedCtx, R.layout.popup_menu_item, items);
final ListPopupWindow listPopupWindow = new ListPopupWindow(themedCtx);
listPopupWindow.setAnchorView(v);
listPopupWindow.setContentWidth((int) (Math.max(itemWidth, minWidth)));
listPopupWindow.setDropDownGravity(Gravity.END | Gravity.TOP);
listPopupWindow.setVerticalOffset(-v.getHeight() + contentPaddingHalf);
listPopupWindow.setModal(true);
listPopupWindow.setAdapter(adapter);
listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (listener != null) {
listener.onItemClick(parent, view, position, id);
}
listPopupWindow.dismiss();
}
});
return listPopupWindow;
}
}

View file

@ -2,15 +2,14 @@ package net.osmand.plus.mapcontextmenu.other;
import android.content.Context;
import android.graphics.Matrix;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.PopupMenu;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry;
@ -33,9 +32,9 @@ import net.osmand.data.RotatedTileBox;
import net.osmand.plus.GpxSelectionHelper.GpxDisplayItem;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.SimplePopUpMenuItemAdapter;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.dialogs.DirectionsDialogs;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.helpers.GpxUiHelper;
import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetAxisType;
@ -653,23 +652,22 @@ public class TrackDetailsMenu {
yAxis.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu optionsMenu = new PopupMenu(v.getContext(), v);
DirectionsDialogs.setupPopUpMenuIcon(optionsMenu);
for (final GPXDataSetType[] types : availableTypes) {
MenuItem menuItem = optionsMenu.getMenu()
.add(GPXDataSetType.getName(app, types))
.setIcon(GPXDataSetType.getImageDrawable(app, types));
menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
Context themedContext = UiUtilities.getThemedContext(v.getContext(), true);
List<SimplePopUpMenuItemAdapter.SimplePopUpMenuItem> items = new ArrayList<>();
for (GPXDataSetType[] types : availableTypes) {
items.add(new SimplePopUpMenuItemAdapter.SimplePopUpMenuItem(
GPXDataSetType.getName(app, types),
GPXDataSetType.getImageDrawable(app, types)));
}
UiUtilities.createListPopupWindow(
themedContext, v, items, new AdapterView.OnItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem mItem) {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
GpxDisplayItem gpxItem = getGpxItem();
gpxItem.chartTypes = types;
gpxItem.chartTypes = availableTypes.get(position);
update();
return true;
}
});
}
optionsMenu.show();
}).show();
}
});
yAxisArrow.setVisibility(View.VISIBLE);
@ -697,28 +695,25 @@ public class TrackDetailsMenu {
xAxis.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final PopupMenu optionsMenu = new PopupMenu(v.getContext(), v);
DirectionsDialogs.setupPopUpMenuIcon(optionsMenu);
for (final GPXDataSetAxisType type : GPXDataSetAxisType.values()) {
MenuItem menuItem = optionsMenu.getMenu()
.add(type.getStringId()).setIcon(type.getImageDrawable(app));
menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
Context themedContext = UiUtilities.getThemedContext(v.getContext(), true);
List<SimplePopUpMenuItemAdapter.SimplePopUpMenuItem> items = new ArrayList<>();
for (GPXDataSetAxisType type : GPXDataSetAxisType.values()) {
items.add(new SimplePopUpMenuItemAdapter.SimplePopUpMenuItem(
app.getString(type.getStringId()), type.getImageDrawable(app)));
}
UiUtilities.createListPopupWindow(themedContext,
v, items, new AdapterView.OnItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem mItem) {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
GpxDisplayItem gpxItem = getGpxItem();
if (gpxItem != null) {
gpxItem.chartAxisType = type;
gpxItem.chartAxisType = GPXDataSetAxisType.values()[position];
gpxItem.chartHighlightPos = -1;
gpxItem.chartMatrix = null;
update();
return true;
} else {
return false;
}
}
});
}
optionsMenu.show();
}).show();
}
});
xAxisArrow.setVisibility(View.VISIBLE);