操作栏操作视图不起作用

时间:2014-11-09 15:56:50

标签: java android android-actionbar android-studio

我正在尝试在Action Bar中创建一个可折叠的EditText。我跟着Android Developers guide。但是当我点击我的搜索图标时,没有任何反应。

我该怎么办?

这是我的代码。

活动:

public class ElementPagerActivity extends ActionBarActivity
                implements ElementListFragment.onElementClickListener,
                           CalculateFragment.OnCalculateClickListener{

ViewPager theViewPager;
private ActionBar actionBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    actionBar = getSupportActionBar();

    theViewPager = new ViewPager(this);
    theViewPager.setId(0x1);

    theViewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int position) {
            if(position == 0){
                return new ElementListFragment();
            } return Element.values()[position - 1].toFragment();
        }

        @Override
        public int getCount() {
            return Element.values().length + 1;
        }
    });

    theViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i2) {

        }

        @Override
        public void onPageSelected(int position) {
            invalidateOptionsMenu();
            if(position == 0){
                actionBar.setTitle(R.string.Element_info_activity_label);
                actionBar.selectTab(null);
            } else {
                actionBar.setTitle(Element.values()[position - 1].getName());
                actionBar.selectTab(actionBar.getTabAt(position - 1));
            }
        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    });


    actionBar = actionBar;
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.TabListener tabListener = new ActionBar.TabListener(){

        @Override
        public void onTabSelected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction fragmentTransaction) {
            int position = tab.getPosition();
            theViewPager.setCurrentItem(position + 1, true);
        }

        @Override
        public void onTabUnselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction fragmentTransaction) {

        }

        @Override
        public void onTabReselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction fragmentTransaction) {
            int position = tab.getPosition();
            theViewPager.setCurrentItem(position + 1, true);
        }
    };

    for (int i = 0; i < Element.values().length; i++){
    actionBar.addTab(
            actionBar.newTab()
            .setText(Element.values()[i].getName())
            .setTabListener(tabListener));
    }

    theViewPager.setCurrentItem(0);
    actionBar.selectTab(null);

    setContentView(theViewPager);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.element_pager, menu);
    menu.findItem(R.id.pager_activity_show_list_action).setVisible(!(theViewPager.getCurrentItem() == 0));
    menu.findItem(R.id.pager_activity_edit_text_action).expandActionView();
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.pager_activity_show_list_action){
        theViewPager.setCurrentItem(0, true);
        return true;
    } else if (id == R.id.pager_activity_edit_text_action){
        return false;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
    if (theViewPager.getCurrentItem() == 0){
        super.onBackPressed();
    } else {
        theViewPager.setCurrentItem(0);
    }
}

@Override
public void onElementClick(int position) {
    theViewPager.setCurrentItem(position + 1, true);
}

@Override
public Element onRequestElement() {
    return Element.values()[theViewPager.getCurrentItem() - 1];
}
}

菜单资源:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity" >

<item android:id="@+id/pager_activity_edit_text_action"
    app:showAsAction="always|collapseActionView"
    android:title="text here"
    android:icon="@android:drawable/ic_menu_search"
    android:actionLayout="@layout/test"/>

<item android:id="@+id/pager_activity_show_list_action"
    android:title="@string/action_bar_pager_show_list"
    android:orderInCategory="100"
    app:showAsAction="ifRoom|withText" />

</menu>

动作布局(test.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="element"
    android:inputType="textCapWords"/>

</LinearLayout>

here是一部展示问题的短片。

我感谢所有答案。

来自荷兰的问候

1 个答案:

答案 0 :(得分:0)

您可以使用SearchView,这将使您的工作变得更轻松。

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/search"
          android:title="@string/search_title"
          android:icon="@drawable/ic_search"
          android:showAsAction="collapseActionView|ifRoom"
          android:actionViewClass="android.widget.SearchView" />
</menu>

更多信息:http://developer.android.com/training/search/setup.html