如何创建从MainActivity到片段的回调?

时间:2018-09-22 08:04:56

标签: android interface callback ads

我正在尝试创建从MainActivity到片段的回调。

在MainActivity中,我有5个标签。每个选项卡都有一个片段不同的UI。片段是通过viewpager添加的。我的目的是通知何时在MainAcitivty中加载广告。片段收到消息后,可以通过调用MainActivity方法显示添加自身。目前,我不想将任何消息发送回MainActivity。

  

MainActivity的代码为:

    public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    private DrawerLayout drawer;
    private ViewPager viewPager = null;
    private RewardedVideoAd mRewardedVideoAd;
    private NavigationView  navigationView;

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

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        this.setTitle(R.string.app_title);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText(R.string.Tab1));
        tabLayout.addTab(tabLayout.newTab().setText(R.string.Tab2));
        tabLayout.addTab(tabLayout.newTab().setText(R.string.Tab3));
        tabLayout.addTab(tabLayout.newTab().setText(R.string.Tab4));
        tabLayout.addTab(tabLayout.newTab().setText( R.string.Tab5));

        viewPager = (ViewPager) findViewById(R.id.pager);
        final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });

        drawer = (DrawerLayout) findViewById(R.id.drawerLayout);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        //handling navigation view item event
        navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setItemIconTintList(null);
        assert navigationView != null;
        navigationView.setNavigationItemSelectedListener(this);

        MobileAds.initialize(this, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);

        mRewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {

            @Override
            public void onRewarded(RewardItem rewardItem) {
                Toast.makeText(getApplication(),"Ad Rewarded.", Toast.LENGTH_SHORT).show();
                loadRewardedVideoAd();
            }

            @Override
            public void onRewardedVideoAdLoaded() {
                Toast.makeText(getApplication(),"Ad loaded.", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onRewardedVideoAdOpened() {
                Toast.makeText(getApplication(),"Ad opened.", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onRewardedVideoStarted() {
                Toast.makeText(getApplication(),"Ad started", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onRewardedVideoCompleted() {
                Toast.makeText(getApplication(),"Ad completed.", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onRewardedVideoAdClosed() {
                Toast.makeText(getApplication(),"Ad closed.", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onRewardedVideoAdLeftApplication() {
                Toast.makeText(getApplication(),"Ad left application.", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onRewardedVideoAdFailedToLoad(int i) {
                Toast.makeText(getApplication(),"Ad failed to load.", Toast.LENGTH_SHORT).show();

            }
        });
        loadRewardedVideoAd();
    }

    public void loadRewardedVideoAd() {
        mRewardedVideoAd.loadAd("xxxxxxxxxxxxxxxxxxxxxxxxxxxx",new AdRequest.Builder().build());
    }

    public void showRewardedVideoAd() {
        mRewardedVideoAd.show();
    }

    @Override
    public void onResume() {
        mRewardedVideoAd.resume(getParent());
        super.onResume();
    }

    @Override
    public void onPause() {
        mRewardedVideoAd.pause(getParent());
        super.onPause();
    }

    @Override
    public void onDestroy() {
        mRewardedVideoAd.destroy(getParent());
        super.onDestroy();
    }

    public interface onToggledListener{
        void onToggled();
    }

    private onToggledListener toggledListener;

    public void setOnToggledlistener(onToggledListener listener){
        toggledListener = listener;
    }

}//end of MainActivity
  

该片段的代码为:

    public class Tab1 extends Fragment implements MainActivity.onToggledListener {

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.tab1, container, false);
        buttonVideo     = (Button)   view.findViewById(R.id.videoAd);
        fab =(FloatingActionButton) view.findViewById(R.id.fab);

        return view;
    }
    //method implemented here
    @Override
    public void onToggled(){

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        buttonVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showad();// some method
            }
        });

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                add();//some method
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}// end of fragment class

这是我试图实现自己的目标的方法,但是我没有得到结果。回调不起作用

2 个答案:

答案 0 :(得分:1)

您不会调用方法 setOnToggledlistener 来初始化您的界面,然后您可以将其从MainActivity回调回Fragment。

因此,请从活动中调用 setOnToggledlistener

答案 1 :(得分:1)

活动中:

mpic++

片段中:

 @Override           
 public void onRewardedVideoAdLoaded() {       
      toggledListener.onToggled(); //trigger callback when video loaded.               
 }