在Gingerbread中水平滑动后不触发Actionbar的onNavigationItemSelected(int position,long itemId)

时间:2012-11-28 08:30:39

标签: java android actionbarsherlock flags android-gesture

我已经使用导航模式实现了Tab视图和水平滑动视图(以更改标签)作为应用程序的列表。我正在使用ActionBarSherlock& TabPageIndicator。我在更改选项卡后使用片段重新加载内容。

在下拉式微调器中,我有2个项目。每当活动重新加载或在Tabs上点击后,都会选择这些选项。

但是,如果执行滑动以选择其他选项卡,那么onNavigationItemSelected(int position,long itemId)将不会在Gingerbread版本中被触发:(我们已经在Gingerbread 2.3.6,2.3.3上进行了测试。

logcat中,我收到了warning条消息"View:onTouchEvent:ViewFlags" is DISABLED

但是,它在ICS,Froyo等上运行得非常好。

非常感谢您的帮助。

以下是代码段:

public class MainPageActivity extends SherlockFragmentActivity implements
        ActionBar.OnNavigationListener {

    MenuItem menuItem;
    EditText searchText;
    public static Context mContext;
    public static DataAdapter dataAdapter;
    ArrayList<String> accountList;
    public static List<View> currentViewBeingChanged = new ArrayList<View>();

    public static LayoutInflater layoutInflater;
    public static String checktab = "running", nameComp;
    int isRefresh;
    public static boolean isAutoRefresh = true;
    public static ArrayList<String> listItems;
    public static boolean check;
    public static boolean isAtleastOnce = true, listToBeRefreshed = false;
    public static ActionBar actionBar;
    public static int itemPosition, listPosition;
    public static FragmentManager fragmentManager;
    InputMethodManager imm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        mContext = this;

        layoutInflater = getLayoutInflater();

        fragmentManager = getSupportFragmentManager();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_page);

        actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

        imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

        Context context = getSupportActionBar().getThemedContext();

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
                android.R.layout.simple_spinner_dropdown_item);
        adapter.clear();
        for (int i = 0; i < ManageAccountLists.accountList.size(); i++) {
            adapter.add(ManageAccountLists.accountList.get(i));
        }

        actionBar.setListNavigationCallbacks(adapter, this);

        tabPagerFunction();

    }

    public void tabPagerFunction() {

        FragmentPagerAdapter adapter = new GoogleMusicAdapter(
                getSupportFragmentManager());
        adapter.notifyDataSetChanged();

        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);

        checktab = "running";

        final TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
        indicator.setViewPager(pager);

        pager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {

                System.out.println("Tab selected..................");

                if (arg0 == 0) {

                    // Running tab is selected
                    indicator.setCurrentItem(arg0);

                    checktab = "running";
                    RunningInstancesActivity runningInstancesActivity = new RunningInstancesActivity();
                    runningInstancesActivity.callThread();

                } else {

                    // Stopped tab is selected
                    indicator.setCurrentItem(arg0);
                    checktab = "stopped";
                    StoppedInstancesActivity stoppedInstancesActivity = new StoppedInstancesActivity();
                    stoppedInstancesActivity.callThread();
                    InstanceStatusUpdate.isOnPostExecute = false;

                }

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });

    }

    class GoogleMusicAdapter extends FragmentPagerAdapter {

        private final String[] CONTENT = new String[] { "Running", "Stopped" };

        public GoogleMusicAdapter(FragmentManager fm) {
            super(fm);
            notifyDataSetChanged();

        }

        @Override
        public Fragment getItem(int position) {

            switch (position) {
            /** Running tab is selected */
            case 0:
                checktab = "stopped";
                return RunningInstancesActivity.newInstance(0);

                /** Stopped tab is selected */
            case 1:

                checktab = "running";
                return StoppedInstancesActivity.newInstance(1);
            }
            return null;

        }

        @Override
        public CharSequence getPageTitle(int position) {
            return CONTENT[position % CONTENT.length].toUpperCase();
        }

        public CharSequence getPageTitle(int position, int number) {
            return CONTENT[position % CONTENT.length].toUpperCase();
        }

        @Override
        public int getCount() {
            return CONTENT.length;
        }
    }

    @Override
    public boolean onNavigationItemSelected(int itemPosition, long itemId) {

             System.out.println("Navigation item selected");

        // Performs fetching
    }



    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        System.out.println("Dispatch touvh event..");

        return super.dispatchTouchEvent(ev);

    }

}

3 个答案:

答案 0 :(得分:1)

尝试这样做

dependencies {

compile "com.android.support:appcompat-v7:18.0.+"
}

并改为使用支持库。

答案 1 :(得分:0)

据我记得,滑动后更新导航列表存在一些问题(在ABS中)。这就是我为解决这个问题所做的工作,它适用于Android 2.3.7(ABS)以及Android 4.0(原生AB)。

pager.setOnPageChangeListener(new OnPageChangeListener() {
    @Override
    public void onPageScrollStateChanged(int state) {
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        if (navigatioBarIsUsed) actionBar.setSelectedNavigationItem(position);
        if (tabsAreUsed) actionBar.getTabAt(position).select();
    }
});

在内部它会重新选择页面 - 这不太好 - 但这是我找到的唯一解决方案。

干杯

答案 2 :(得分:0)

使用此代码..

片段内部使用此代码。

@Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        // TODO Auto-generated method stub
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            Activity activity = getActivity();
            ActionBar actionBar = ((ActionBarActivity) activity)
                    .getSupportActionBar();
            actionBar.setIcon(getResources().getDrawable(R.drawable.nearby));


        }
    }