片段中的Andr​​oid setShareIntent [英] Android setShareIntent within fragment

查看:146
本文介绍了片段中的Andr​​oid setShareIntent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1。背景

我有,有一个屏幕

ShareActionProvider

ViewPager

使用片段。

我希望做的是得到当前可见的片段里面的一些信息来创建一个意图,然后我就可以设置的意图在ShareActionProvider。

What I was hoping to do was get some information from inside the currently visible fragment to create an intent, I would then be able to set the intent on the ShareActionProvider.

这是在code我用它来设置ShareActionProvider的意图:

This is the code I use to set the intent of the ShareActionProvider:

    MenuItem actionItem = men.getMenu().findItem(R.id.menu_item_share_action_provider_action_bar);
    ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
    actionProvider.setShareIntent(createShareIntent(mProduct.mProduct));

我在几个地方,如内的片段类的这些功能​​使用这一尝试:

I have tried using this in several places such as within these functions of the Fragment class :

onCreateView
onStart

我也曾尝试FragmentPagerAdapter类中使用它的这些功能​​:

I have also tried using it in these functions within the FragmentPagerAdapter class :

getItem

2。存在的问题

虽然意图实际上得到的ShareActionProvider内设定,所获得的信息是为下一个片段(没有被示出的一个)。例如:

Although the intent is actually getting set within the ShareActionProvider, the information that is obtained is for the next fragment (the one not currently being shown). For example:

如果我有4个片段:frag1,frag2,frag3,frag4 我目前看frag1的ShareActionProvider将尝试共享frag2。这是真实的,直到达到frag4而将共享正确的值。

If I have 4 fragments : frag1, frag2, frag3, frag4 and I am currently viewing "frag1" the ShareActionProvider will attempt to share "frag2". This is true until it reaches "frag4" where it will share the correct value.

我的猜测是,该片段寻呼机创建当前视图和下一个视图(隐藏),而这又是设置ShareActionProvider。如果是这种情况,那么哪里是正确的地方setShareIntent?

My guess is that the fragment pager creates the current view and the next view (hidden), which is in turn setting the ShareActionProvider. If this is the case then where is the correct place to "setShareIntent"?

推荐答案

我设法弄清楚如何做到这一点我自己。我最后做的是重写

I managed to figure out how to do this myself. What I ended up doing is overriding the

OnPageChangeListener

在fragmentactivity

in the fragmentactivity

private final OnPageChangeListener mOnPageChangeListener = new OnPageChangeListener() {

    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageSelected(int arg0) {
        // TODO Auto-generated method stub
        TestFragment frag = (TestFragment) adapter.mFragments.get(pager.getCurrentItem());

        frag.setShareActionIntent();

    }

};

这篇关于片段中的Andr​​oid setShareIntent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆