Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Viewpager not displaying menu items for first fragment

I have list items onlcick of list item I take to DetailActivity. DetailActivity is swipable using viewpager and Fragments. The issue is when DetailActivity is opened and Fragment is loaded no menu options are displayed however if i swipe left or right i get the menu options then when i return to the initial fragment the menu items are visible.

I am inflating the menu options in fragment.

DetailActivity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_article_new);

        mViewPager = (CustomViewPager) findViewById(R.id.viewPager);

        int currentItemIndex = mNewsItemList.indexOf(newsItem);

        mArticleSwipeMap = new HashMap<>();
        mPagerAdapter = new ArticlePagerAdapter(getSupportFragmentManager());
        mPagerAdapter.swapList(mNewsItemList);
        mViewPager.setOnSwipeOutListener(this);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setCurrentItem(currentItemIndex);
        mViewPager.addOnPageChangeListener(this);
    }

Adapter class

private class ArticlePagerAdapter extends FragmentStatePagerAdapter{
        private Map<Integer, ArticleFragment> mPageReferenceMap = new HashMap<>();
        private List<NewsItem> newsItemList;

        public ArticlePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            ArticleFragment articleFragment = ArticleFragment.newInstance(newsItemList.get(position));
            mPageReferenceMap.put(position, articleFragment);
            return articleFragment;
        }

        @Override
        public int getCount() {
            return newsItemList.size();
        }

        public void swapList(List<NewsItem> newsItemList){
            this.newsItemList = newsItemList;
            notifyDataSetChanged();
        }
    }

DetailFragment

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int itemId = item.getItemId();
        switch(itemId) {
            case R.id.action_more:
                View menuItemView = mRootView.findViewById(R.id.action_more);
                onMenuClick(menuItemView);
                break;
            case R.id.action_share:
                shareTextUrl();
                break;
        }
        return true;
    }

Update 1 : When I swipe left, menu item are not shown when i swipe right menu items are shown, now if i swipe left menu item are shown.

Has it got to do anything with setting up viewpager This is how I am setting up viewpager in onCrete of Activity

mPagerAdapter = new ArticlePagerAdapter(getSupportFragmentManager());
        mPagerAdapter.swapList(mNewsItemList);
        mViewPager.setOnSwipeOutListener(this);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setCurrentItem(currentItemIndex);
        mViewPager.addOnPageChangeListener(this);

Update 2 :

The issue is when I use custom toolbar it is not working. When i use with the DarkActionBar it works fine but when I use with NoActionBar with my custom toolbar it creates prob. Here is the sample proj I created which has same issue link to github https://github.com/KumarVelu/viewPagerDemo

like image 480
Velu Avatar asked May 26 '17 13:05

Velu


People also ask

What is difference between ViewPager and ViewPager2?

ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .

How do you get ViewPager fragments?

ViewPager save Fragment in FragmentManager with particular tags, So We can get ViewPager's fragment by FragmentManager class by providing tag. And ViewPager provide tag to each fragment by following syntax : Fragment page = getSupportFragmentManager(). findFragmentByTag("android:switcher:" + R.

Can we add activity in ViewPager?

You can't use ViewPager to swipe between Activities . You need to convert each of you five Activities into Fragments , then combine everything in one FragmentActivity with the Adapter you use with ViewPager .


1 Answers

The problem is that you are trying to set more than one main ActionBar because each fragment has its own Toolbar. So, when ViewPager loads the current page and preloads the next page, you have 2 fragments trying to set their own Toolbar as the main ActionBar and they are conflicting.

If you want to use a Toolbar as main Action Bar, move the Toolbar in the Activity layout instead of the fragments layout and initialize it once in the Activity.

If you want a separate Toolbar in each fragment, don't set any Toolbar as main action bar and use Toolbar.inflateMenu() and Toolbar.setOnMenuItemClickListener() instead. Each fragment will then inflate its own menu items directly in its own Toolbar, without involving the Activity.

like image 196
BladeCoder Avatar answered Nov 07 '22 04:11

BladeCoder