我的ActionBar图标不可点击

时间:2015-02-03 16:20:35

标签: java android android-fragments

我对此图标有一些问题,问题是我可以执行onLongPress然后它会显示此项目文本的祝酒词。

我的菜单" new_offer_menu.xml"

    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/newOffer"
        android:icon="@drawable/ic_action_new"
        android:title="@string/nueva_lista"
        android:alphabeticShortcut="r"
        android:orderInCategory="200"
        android:showAsAction="ifRoom" />
</menu>

这个图标不在我的所有片段中,但当我转到正确的片段(必须显示它)时,当我点击它时什么也不做。在我的ListaProductosFragment.java中,我有:

    package info.androidhive.slidingmenu;

import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class ListaProductosFragment extends ListFragment {
    private List<ListViewItem> mItems;        // ListView items list
    private Menu optionsMenu;
    public ListaProductosFragment(){}


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);



        // initialize the items list
        mItems = new ArrayList<ListViewItem>();
        Resources resources = getResources();

        mItems.add(new ListViewItem(resources.getDrawable(R.drawable.tomate_oferta), getString(R.string.aim), getString(R.string.aim_precio), getString(R.string.aim_descuento), getString(R.string.aim_data)));
        mItems.add(new ListViewItem(resources.getDrawable(R.drawable.levadura_oferta), getString(R.string.youtube), getString(R.string.youtube_precio), getString(R.string.youtube_descuento), getString(R.string.youtube_data)));
        mItems.add(new ListViewItem(resources.getDrawable(R.drawable.sopa_oferta), getString(R.string.bebo), getString(R.string.bebo_precio), getString(R.string.bebo_descuento), getString(R.string.aim_data)));
        mItems.add(new ListViewItem(resources.getDrawable(R.drawable.zumo_oferta), getString(R.string.pew), getString(R.string.pew_precio), getString(R.string.pew_descuento), getString(R.string.aim_data)));
        mItems.add(new ListViewItem(resources.getDrawable(R.drawable.tomate_oferta), getString(R.string.aim), getString(R.string.aim_precio), getString(R.string.aim_descuento), getString(R.string.aim_data)));
        // initialize and set the list adapter
        setListAdapter(new ListViewDemoAdapterListaProductos(getActivity(), mItems));
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // remove the dividers from the ListView of the ListFragment
        getListView().setDivider(null);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // retrieve theListView item
        ListViewItem item = mItems.get(position);

        // do something
        Toast.makeText(getActivity(), item.title, Toast.LENGTH_SHORT).show();

    }


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.new_offer_menu,menu );

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar actions click


        switch (item.getItemId()) {

            case R.id.newOffer:
                Toast.makeText(getActivity(), "this is my Toast message!!! =)",
                        Toast.LENGTH_LONG).show();
                Intent intent = new Intent(getActivity(), TipusNouProducte.class);
                startActivity(intent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }

    }
}

我已经创建了一个祝酒词,看看问题是否是Intent但是它无论如何都不会出现,它只会在我对这个项目进行LongPress时执行某些操作。 我做错了什么?

1 个答案:

答案 0 :(得分:1)

我相信你应该改变

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/newOffer"
    android:icon="@drawable/ic_action_new"
    android:title="@string/nueva_lista"
    android:alphabeticShortcut="r"
    android:orderInCategory="200"
    android:showAsAction="ifRoom" />
</menu>

<menu xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:yourAppName="http://schemas.android.com/apk/res-auto">   <--
<item android:id="@+id/newOffer"
    android:icon="@drawable/ic_action_new"
    android:title="@string/nueva_lista"
    android:alphabeticShortcut="r"
    android:orderInCategory="200"
--> yourAppName:showAsAction="ifRoom" />
</menu>

如果我没弄错的话,LongPress的行为是一种常规行为,系统会向用户解释menuItem的含义,从标题标签中获取字符串

android:title="@string/nueva_lista"

你不是直接管理它,Android本身可以管理它,给你错误的感觉。