Come autore originale di questo (sono uno dei principali sviluppatori Android per Astrid) sarei felice di condividere come fa Astrid. Pubblicherò le basi qui, ma puoi trovare ulteriori dettagli nel nostro repository github (https://github.com/todoroo/astrid). L'idea di base è di estendere QuickActionWidget di GreenDroid come suggerisce Hanry. La sottoclasse sembra qualcosa di simile:
public class MenuPopover extends QuickActionWidget {
protected DisplayMetrics metrics;
protected LinearLayout content;
public MenuPopover(Context context) {
super(context);
setContentView(R.layout.my_layout);
content = (LinearLayout) getContentView().findViewById(R.id.content);
metrics = context.getResources().getDisplayMetrics();
setFocusable(true);
setTouchable(true);
}
@Override
protected void populateQuickActions(List<QuickAction> quickActions) {
// Do nothing
}
@Override
protected void onMeasureAndLayout(Rect anchorRect, View contentView) {
contentView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
contentView.measure(MeasureSpec.makeMeasureSpec(getScreenWidth(), MeasureSpec.EXACTLY),
ViewGroup.LayoutParams.WRAP_CONTENT);
int rootHeight = contentView.getMeasuredHeight();
int offsetY = getArrowOffsetY();
int dyTop = anchorRect.top;
int dyBottom = getScreenHeight() - anchorRect.bottom;
boolean onTop = (dyTop > dyBottom);
int popupY = (onTop) ? anchorRect.top - rootHeight + offsetY : anchorRect.bottom - offsetY;
setWidgetSpecs(popupY, onTop);
}
}
Il file di layout my_layout.xml è piuttosto semplice:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dip">
<LinearLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/gdi_arrow_up"
android:orientation="vertical"/>
<ImageView
android:id="@+id/gdi_arrow_up"
android:layout_width="27dip"
android:layout_height="27dip"
android:layout_marginLeft="-10dip"
android:scaleType="fitCenter"
android:layout_marginBottom="-8dip"
android:src="?attr/asListArrowUp" />
<ImageView
android:id="@+id/gdi_arrow_down"
android:layout_width="27dip"
android:layout_height="27dip"
android:scaleType="fitCenter"
android:layout_marginBottom="-8dip"
android:layout_below="@android:id/list"/>
</RelativeLayout>
</FrameLayout>
Poi, si può semplicemente aggiungere un metodo di supporto semplice alla classe popover per aggiungere viste (vale a dire le righe , con gli ascoltatori opzionali) al corpo principale del popover:
public void addViewToContent(View v, OnClickListener listener) {
content.addView(v);
if (listener != null) {
v.setOnClickListener(listener);
}
}
Dopo aver creato un'istanza della pop-up, è possibile mostrare chiamando
menuPopover.show(anchorView);
Questa è una versione un po 'semplificata - in pratica, allega alcune informazioni aggiuntive, ascoltatori, ecc. Per far sì che facciano realmente le cose quando si fa clic. Se lo desideri, puoi controllare il codice completo al numero https://github.com/todoroo/astrid - la classe è com.todoroo.astrid.ui.MainMenuPopover.
Grazie per aver scelto Astrid!
è un menu contestuale –