Actionbar's onNavigationItemSelected(int position, long itemId) not triggered after horizontal swipe in Gingerbread
Asked Answered
L

3

27

I have implemented Tab view as well as Horizontal swipe view(to change tabs) with Navigation mode as Lists for the application. I am using ActionBarSherlock & TabPageIndicator. I used fragments for reloading the contents after changing the tabs.

In drop down spinner, I have 2 items. These are getting selected whenever activity reloads or after hitting on Tabs.

But, if swipe is performed to select other tab, then onNavigationItemSelected(int position, long itemId) will not getting triggered in Gingerbread version :( We have tested it on Gingerbread 2.3.6, 2.3.3.

In logcat, I got this warning message "View:onTouchEvent:ViewFlags" is DISABLED

But, it works perfectly fine on ICS, Froyo, etc.

Your help would be greatly appreciated.

following is the code snippet:

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);

    }

}
Lipase answered 28/11, 2012 at 8:30 Comment(1)
have you looked at my code below?Deliberate
H
1

Try doing this

dependencies {

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

and use support library instead.

Hippocrates answered 26/5, 2015 at 10:54 Comment(0)
D
0

As far as I remember there were some issues with updating the navigation list after swiping (in ABS). This is what I did to solve this issue and it works with Android 2.3.7 (ABS) as well as with Android 4.0 (native 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();
    }
});

Internally it reselects the page - that's not so nice - however that was the only solution I found.

Cheers

Deliberate answered 25/2, 2013 at 19:20 Comment(0)
L
0

Use This code..

Inside Your Fragment Use this code.

@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));


        }
    }
Lavernelaverock answered 31/10, 2013 at 8:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.