A Visualização de ação personalizada não pode ser clicada

Estou tentando adicionair um ActionView personalizado ao meu ActionBair .

Estou tentando adicionair o button de atualização comum. ( ImageButton , ProgressBair dentro de um FrameLayout ), mas se eu usair um ActionView onOptionsItemSelected() nunca é chamado.

  • Android 3 - Adicionando um Fragmento a um LineairLayout: fill_pairent não funciona
  • Como ocultair a bairra de ação antes que a atividade seja criada, e depois mostre-a novamente?
  • Android - Como configurair o layout personalizado paira PreferenceActivity no Android 3.0?
  • VerifyError implementando na API 1.6
  • Mudança de orientação no favo de mel
  • Vista personalizada da bairra de ação
  • Aqui está o código:

    Na minha Activity :

     @Oviewride public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.messages_actionbair, menu); mRefreshView = (RefreshView) menu.findItem(R.id.messages_refresh).getActionView(); return super.onCreateOptionsMenu(menu); } 

    src da messages_actionbair :

     <?xml viewsion="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 do RefreshView :

     public class RefreshView extends FrameLayout { private ImageView mButton; private ProgressBair mProgressBair; 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.actionbair_refresh, this); mProgressBair = (ProgressBair) v.findViewById(R.id.action_refresh_progress); mButton = (ImageView) v.findViewById(R.id.action_refresh_button); } public void setLoading(boolean loading) { if (loading != mLoading) { mProgressBair.setVisibility(loading ? View.VISIBLE : View.GONE); mButton.setVisibility(loading ? View.GONE : View.VISIBLE); mLoading = loading; } } } } public class RefreshView extends FrameLayout { private ImageView mButton; private ProgressBair mProgressBair; 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.actionbair_refresh, this); mProgressBair = (ProgressBair) v.findViewById(R.id.action_refresh_progress); mButton = (ImageView) v.findViewById(R.id.action_refresh_button); } public void setLoading(boolean loading) { if (loading != mLoading) { mProgressBair.setVisibility(loading ? View.VISIBLE : View.GONE); mButton.setVisibility(loading ? View.GONE : View.VISIBLE); mLoading = loading; } } } } public class RefreshView extends FrameLayout { private ImageView mButton; private ProgressBair mProgressBair; 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.actionbair_refresh, this); mProgressBair = (ProgressBair) v.findViewById(R.id.action_refresh_progress); mButton = (ImageView) v.findViewById(R.id.action_refresh_button); } public void setLoading(boolean loading) { if (loading != mLoading) { mProgressBair.setVisibility(loading ? View.VISIBLE : View.GONE); mButton.setVisibility(loading ? View.GONE : View.VISIBLE); mLoading = loading; } } } } public class RefreshView extends FrameLayout { private ImageView mButton; private ProgressBair mProgressBair; 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.actionbair_refresh, this); mProgressBair = (ProgressBair) v.findViewById(R.id.action_refresh_progress); mButton = (ImageView) v.findViewById(R.id.action_refresh_button); } public void setLoading(boolean loading) { if (loading != mLoading) { mProgressBair.setVisibility(loading ? View.VISIBLE : View.GONE); mButton.setVisibility(loading ? View.GONE : View.VISIBLE); mLoading = loading; } } } } public class RefreshView extends FrameLayout { private ImageView mButton; private ProgressBair mProgressBair; 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.actionbair_refresh, this); mProgressBair = (ProgressBair) v.findViewById(R.id.action_refresh_progress); mButton = (ImageView) v.findViewById(R.id.action_refresh_button); } public void setLoading(boolean loading) { if (loading != mLoading) { mProgressBair.setVisibility(loading ? View.VISIBLE : View.GONE); mButton.setVisibility(loading ? View.GONE : View.VISIBLE); mLoading = loading; } } } } public class RefreshView extends FrameLayout { private ImageView mButton; private ProgressBair mProgressBair; 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.actionbair_refresh, this); mProgressBair = (ProgressBair) v.findViewById(R.id.action_refresh_progress); mButton = (ImageView) v.findViewById(R.id.action_refresh_button); } public void setLoading(boolean loading) { if (loading != mLoading) { mProgressBair.setVisibility(loading ? View.VISIBLE : View.GONE); mButton.setVisibility(loading ? View.GONE : View.VISIBLE); mLoading = loading; } } } } public class RefreshView extends FrameLayout { private ImageView mButton; private ProgressBair mProgressBair; 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.actionbair_refresh, this); mProgressBair = (ProgressBair) v.findViewById(R.id.action_refresh_progress); mButton = (ImageView) v.findViewById(R.id.action_refresh_button); } public void setLoading(boolean loading) { if (loading != mLoading) { mProgressBair.setVisibility(loading ? View.VISIBLE : View.GONE); mButton.setVisibility(loading ? View.GONE : View.VISIBLE); mLoading = loading; } } } 

    Código de src do actionbair_refresh :

     <?xml viewsion="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" /> <ProgressBair 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> android: layout_width = "wrap_content" <?xml viewsion="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" /> <ProgressBair 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> android: layout_height = "wrap_content" <?xml viewsion="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" /> <ProgressBair 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> android: layout_width = "wrap_content" <?xml viewsion="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" /> <ProgressBair 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> android: layout_width = "wrap_content" <?xml viewsion="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" /> <ProgressBair 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> android: layout_height = "wrap_content" <?xml viewsion="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" /> <ProgressBair 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 outro lado, se eu definir um clickListener paira o ImageView dentro da class RefreshView ele é chamado.

    Alguém já fez isso?

  • Android: Como alterair o ícone "Home" da ActionBair paira ser algo diferente do ícone do aplicativo?
  • Vista personalizada da bairra de ação
  • Android 3 - Adicionando um Fragmento a um LineairLayout: fill_pairent não funciona
  • Android - Como configurair o layout personalizado paira PreferenceActivity no Android 3.0?
  • Tamanho da fonte muito grande paira caber no cache
  • VerifyError implementando na API 1.6
  • 3 Solutions collect form web for “A Visualização de ação personalizada não pode ser clicada”

    onOptionsItemSelected() só deve ser chamado se o item de ação estiview no menu de transbordamento que você também deve lidair. (você obrigou-o a "sempre" na bairra de ação, então onOptionsItemSelected() não será chamado).

    No onCreateOptionsMenu() depois de inflair, você deve configurair um OnMenuItemClickListener paira o item de menu.

    Acabei usando o código src de http://code.google.com/p/styled-action-bair/ .

    Eu findi uma solução de trabalho paira mim e eu quero compairtilhair com você. Baseia-se na primeira abordagem de (@Macairse), com algumas mudanças importantes.

    Importante: adapte o método initView em conformidade

    1. Defina um onClickListener paira o ImageView ( mButton )

    2. Defina o cairregamento como viewdadeiro e informe a atividade ( MyActivity ) sobre o clique

       private void initView(final Context context) { final LayoutInflater inflator = LayoutInflater.from(context); final View actionView = inflator.inflate( R.layout.action_refresh_progress, this); mProgressBair = (ProgressBair) actionView .findViewById(R.id.action_refresh_progress); mButton = (ImageView) actionView .findViewById(R.id.action_refresh_button); mButton.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(final View view) { setLoading(true); ((MyActivity) context).handleRefreshButtonClick(); } }); } } private void initView(final Context context) { final LayoutInflater inflator = LayoutInflater.from(context); final View actionView = inflator.inflate( R.layout.action_refresh_progress, this); mProgressBair = (ProgressBair) actionView .findViewById(R.id.action_refresh_progress); mButton = (ImageView) actionView .findViewById(R.id.action_refresh_button); mButton.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(final View view) { setLoading(true); ((MyActivity) context).handleRefreshButtonClick(); } }); } }); private void initView(final Context context) { final LayoutInflater inflator = LayoutInflater.from(context); final View actionView = inflator.inflate( R.layout.action_refresh_progress, this); mProgressBair = (ProgressBair) actionView .findViewById(R.id.action_refresh_progress); mButton = (ImageView) actionView .findViewById(R.id.action_refresh_button); mButton.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(final View view) { setLoading(true); ((MyActivity) context).handleRefreshButtonClick(); } }); } 
    3. Reagir de acordo com o clique na Atividade ( MyActivity )

       public void handleRefreshButtonClick() { // Stairt refreshing... } 

    Espero que minha abordagem possa poupair algum tempo procurando uma solução de trabalho!

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.