简体   繁体   English

在Android活动之间共享视图的正确方法?

[英]Correct way to share a View between Android activities?

I want to be able to share a View between different Android activities. 我希望能够在不同的Android活动之间共享视图。 This view is a music player that I want to always be at the footer of every activity. 此视图是一个音乐播放器,我希望始终位于每项活动的页脚。 I also want to be able to access it from any class, so I have it referenced statically from my MainActivity. 我还希望能够从任何类访问它,因此我从MainActivity中静态引用了它。 The view is called Player . 该视图称为Player

My MainActivity will set it up.... 我的MainActivity将对其进行设置。

public class MainActivity extends Activity{
public static Player player;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    MainActivity.player = new Player(this);
}

The Player class is made by inflating my player.xml file. 通过player.xml我的player.xml文件来制作Player类。

public class Player extends LinearLayout{
private ImageView previousButton, playButton, nextButton, playlistButton;
private TextView songTitle;

public Player(Context context)
{
    super(context);
    init(context);

}


private void init(Context context)
{
    LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = layoutInflater.inflate(R.layout.player, this);

    this.previousButton = (ImageView) view.findViewById(R.id.playerPreviousButton);
    this.playButton = (ImageView) view.findViewById(R.id.playerPlayButton);
    this.nextButton = (ImageView) view.findViewById(R.id.playerNextButton);
    this.playlistButton = (ImageView) view.findViewById(R.id.playerPlaylistButton);
    this.songTitle = (TextView) view.findViewById(R.id.playerSongTitle);

}

How can I share this across multiple activities? 如何在多个活动中共享? I have a lot of functions in my Player class that I did not list that I need to be able to access from any class, not just the activity classes, thus I though of going the static route and having it only initialized once. 我的Player类中有很多功能,但没有列出我需要能够从任何类(不仅是活动类)访问的功能,因此我虽然走了静态路线,但只初始化了一次。

Could someone help me in letting me know the correct way to go about doing this? 有人可以帮助我让我知道执行此操作的正确方法吗?

If you only want to initialize your Player UI once, consider using Fragments instead of Activities. 如果您只想初始化Player用户界面一次,请考虑使用片段而不是活动。 You would be able to swap out various UIs in the top portion of your screen, while keeping your PlayerFragment active and untouched on the bottom. 您将可以在屏幕顶部换出各种UI,同时保持PlayerFragment处于活动状态并且在底部保持不变。 This would also mean you do not have to bind/unbind your UI to your audio service every time the user navigates to a different part of your application. 这也意味着您不必在用户每次导航到应用程序的不同部分时都将UI绑定/取消绑定到音频服务。

For your concern you need to define a separate XML layout for that. 考虑到您的需要,您需要为此定义一个单独的XML布局。

Now when you need to show that XML in another layout you may try like this ... 现在,当您需要显示另一布局中的XML时,可以尝试这样...

<include layout="@layout/your_music_layout"/>

Simply adjust at place where exactly you need this. 只需在您确实需要此的地方进行调整即可。

Secondly for accessing it's feature you should declare SUPER CLASS. 其次,要访问它的功能,您应该声明SUPER CLASS。 This super class will be extended by all other classes. 该超类将由所有其他类扩展。 Some thing like this .. 像这样的东西..

Public class MainActivity extends SuperClass{

}

Here your super class will extend Activity which indirectly will be extended by rest of all classes in your app. 在这里,您的超类将扩展Activity,而该活动将间接被应用中所有其他类的扩展。

That's it. 而已。 you are good to go with this. 您很高兴与此搭配。

Correct way is not to share a View across multiple activities. 正确的方法是不要在多个活动之间共享View Each view belongs to one activity and you shouldn't try to share it. 每个视图都属于一个活动,您不应该尝试共享它。 Each view is constructed by passing a Context to it and it's tied to this Context . 每个视图都是通过向其传递Context而构造的,并且与该Context绑定在一起。

You can create a parent class for all your activities and put the necessary code in there. 您可以为所有活动创建一个父类,然后在其中放置必要的代码。 This way you'll be able to share the code but still create separate View objects. 这样,您将可以共享代码,但仍可以创建单独的View对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM