Add Footer for whole Android Application

雨燕双飞 提交于 2019-12-11 04:39:27

问题


In my application I have a footer for each activity. Basically I have an ImageView in footer which get Images (adds Images) from Web service and change images after some time interval (like sliding adds). What I am doing is adding footer in each activity and call web service in every activity to load images into ImageView (which is going to be a very heavy processing for my app because I have many Java classes(Activities) in my app), so my question is... Is there any way in android that I can add footer for whole application? like if I add a footer in first activity at bottom, now if I move to second activity, third activity and so on....the footer will still there which I've added in the first activity. (same like in iPhone Navigation controller is used )

Thanks in advance


回答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.




回答2:


Make one footer xml file which you want to display on all screens of your application then follow below steps:

Create One Footer.java Activity

public class Footer extends Activity {
        public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.footer);
        }
 }

Then extend that class on another class for use like below:

public class Test extends Footer 
{
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            ViewGroup vg = (ViewGroup) findViewById(R.id.lldata);
            ViewGroup.inflate(Home.this, R.layout.test, vg);
        }
}


来源:https://stackoverflow.com/questions/19265093/add-footer-for-whole-android-application

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