A common footer for an entire application [Android]

☆樱花仙子☆ 提交于 2020-01-11 12:36:34

问题


OK, I wanna know if this can be done..

All I need is a common footer like bar, which will contain ads that are going to be displayed. I was wondering if there was any way by which I can have this part of my application as a common one.

I am aware of the include tag, but all that does is to add a that particular layout anywhere it is referenced. What that does is it prompts a reload of the ad, everytime I move from one activity to another. This is very annoying as there is a new ad request that is being sent everytime I move to a new activity.

I am using admob to display ads. Hope I have made the question clear.


回答1:


Create your view programatically then store it in a singleton object which can be accessed throughout the application, this will avoid having to create a new View each time.

Singleton class example:

  public class MySingleFooter
{
    MySingleFooter mySingleFooter;
    View myFooter;

    private MySingleFooter()
{

}

    public static MySingleFooter getInstance()
    {
    if (mySingleFooter == null)
        {
        mySingleFooter = new MySingleFooter();
        }

    return mySingleFooter;
    }

    void setFooter(View myFooter)
    {
        this.myFooter = myFooter;
    }

    View getFooter()
    {
        return myFooter;
    }

}

You can set the footer from all activities like this:

MySingleFooter footerStore = MySingleFooter.getInstance();
footerStore.setFooter(thefooter);

You can retrieve the footer from all activities like this:

MySingleFooter footerStore = MySingleFooter.getInstance();
View myview = footerStore.getFooter(thefooter);

Then add it to the current activity programatically.



来源:https://stackoverflow.com/questions/5408336/a-common-footer-for-an-entire-application-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!