No se puede hacer clic en la vista de acciones personalizadas
Estoy intentando agregar un ActionView
personalizado a mi ActionBar
.
Estoy tratando de agregar el botón de actualización común. ( ImageButton
, ProgressBar
dentro de un FrameLayout
) pero si uso un ActionView
onOptionsItemSelected()
nunca se llama.
- TextView setScaleX () / setScaleY () y setTextIsSelectable (true) selección
- La palabra @drawable no está correctamente escrita
- ¿Qué es lo que hace Bitmap # recycle () en Android Honeycomb?
- Cambiar ListView choiceMode de singleChoice a multipleChoiceModal
- Se busca: IDE sin conexión para desarrollar Html / JavaScript en tableta Android
Aquí está el código:
En mi Activity
:
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.messages_actionbar, menu); mRefreshView = (RefreshView) menu.findItem(R.id.messages_refresh).getActionView(); return super.onCreateOptionsMenu(menu); }
Src de messages_actionbar
:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/messages_refresh" android:title="title" android:icon="@drawable/icon" android:showAsAction="always" android:actionViewClass="com.blabla.RefreshView"/> </menu>
RefreshView
de RefreshView
:
public class RefreshView extends FrameLayout { private ImageView mButton; private ProgressBar mProgressBar; private boolean mLoading; public RefreshView(Context context) { super(context, null); initView(context); } public RefreshView(Context context, AttributeSet attrs) { super(context, attrs, 0); initView(context); } public RefreshView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(context); } private void initView(Context context) { LayoutInflater inflator = LayoutInflater.from(context); View v = inflator.inflate(R.layout.actionbar_refresh, this); mProgressBar = (ProgressBar) v.findViewById(R.id.action_refresh_progress); mButton = (ImageView) v.findViewById(R.id.action_refresh_button); } public void setLoading(boolean loading) { if (loading != mLoading) { mProgressBar.setVisibility(loading ? View.VISIBLE : View.GONE); mButton.setVisibility(loading ? View.GONE : View.VISIBLE); mLoading = loading; } } }
actionbar_refresh
de src de actionbar_refresh
:
<?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"> <ImageView android:id="@+id/action_refresh_button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:scaleType="center" android:background="@drawable/icon" /> <ProgressBar android:id="@+id/action_refresh_progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:visibility="gone" android:indeterminate="true" /> </FrameLayout>
Por otro lado, si establezco un clickListener
al ImageView
dentro de la clase RefreshView
se llama.
¿Alguien ya lo hizo?
- ActionBar Dropdown Spinner elemento por defecto al primer elemento
- OnClickListener se dispara después de onLongClickListener
- ¿Cómo encontrar Fragmento que fue cambiado dinámicamente?
- Barra de acciones de Android: pestañas personalizadas y desbordamiento
- Error al agregar fragmentos dinámicamente -java.lang.IllegalStateException:
- Barra de estado difusa de nido de abeja + minSdkVersion en el manifiesto <11
- Estrategias para Honeycomb y compatibilidad con versiones anteriores
- ¿Por qué android: layout_width = "0px" para fragmentos?
onOptionsItemSelected()
sólo se debe llamar si el elemento de acción está en el menú de desbordamiento que también debe manejar. (Se le obliga a "siempre" en la barra de acción, por lo que onOptionsItemSelected()
no se llamará).
En onCreateOptionsMenu()
después de inflar, debe configurar un OnMenuItemClickListener
para el elemento de menú.
Terminé usando el código src desde http://code.google.com/p/styled-action-bar/ .
He encontrado una solución de trabajo para mí y quiero compartirla con usted. Se basa en el primer enfoque de (@Macarse), con algunos cambios importantes.
Importante: adaptar el método initView en consecuencia
-
Establezca un onClickListener en el ImageView ( mButton )
-
Establecer la carga en true & informar la Actividad ( MyActivity ) sobre el clic
private void initView(final Context context) { final LayoutInflater inflator = LayoutInflater.from(context); final View actionView = inflator.inflate( R.layout.action_refresh_progress, this); mProgressBar = (ProgressBar) actionView .findViewById(R.id.action_refresh_progress); mButton = (ImageView) actionView .findViewById(R.id.action_refresh_button); mButton.setOnClickListener(new OnClickListener() { @Override public void onClick(final View view) { setLoading(true); ((MyActivity) context).handleRefreshButtonClick(); } }); }
-
Reaccione de acuerdo con el clic en la actividad ( MyActivity )
public void handleRefreshButtonClick() { // Start refreshing... }
Espero que mi enfoque podría ahorrarle tiempo encontrar una solución de trabajo!
- ¿Cuál es la diferencia entre getDir y getFilesDir en Android?
- Crear android forma de fondo de forma programática