如何为整个应用程序制作固定页脚?

时间:2022-10-04 22:41:37

I am making a music app that has SlidingTab layout with viewpager. I have included a footer at the bottom of the main activity layout (just like Google play music). Now each of those viewpager fragments contain lists or grids. On clicking each grid/list item (say music album/genre/artist etc) it starts a new activity that displays the details of the grid item(number of songs etc). But I want this new activity to be placed above the footer and I don't want to include a footer in each activity.

我正在制作一个带有viewpager的SlidingTab布局的音乐应用程序。我在主要活动布局的底部包含了一个页脚(就像Google播放音乐一样)。现在每个viewpager片段都包含列表或网格。在点击每个网格/列表项目(比如音乐专辑/流派/艺术家等)时,它会启动一个新活动,显示网格项目的详细信息(歌曲数量等)。但是我希望将这个新活动放在页脚上方,我不想在每个活动中包含一个页脚。

Should I replace activity with fragment, if yes, how?

我应该用片段替换活动,如果是,怎么样?

Please see this image to get an idea of what I intent to do http://i.stack.imgur.com/fgL0u.png

请参阅此图片以了解我打算做什么http://i.stack.imgur.com/fgL0u.png

My question is should the green section screen be a fragment? If yes, how do I launch it?

我的问题是绿色部分屏幕应该是片段吗?如果是,我该如何推出它?

2 个解决方案

#1


0  

Based on you requirements, You can add a static footer in the activity layout. Switcher view in your Viewpager could be different fragments.

根据您的要求,您可以在活动布局中添加静态页脚。 Viewpager中的切换器视图可能是不同的碎片。

So fragments would keep on changing and footer would be persistent. There are many tutorials available out there related with using fragments with activities.

所以片段会不断变化,页脚会持续存在。有许多教程可用于将片段与活动一起使用。

One could be found here. Also have a look at this one.

一个人可以在这里找到。还看看这一个。

Update[as per the image updated in the question]: There could be many ways to achieve the UI you mentioned. Currently i could think of these ways:

更新[根据问题中更新的图像]:可以通过多种方式实现您提到的UI。目前我可以想到这些方式:

  1. You can have Base Activity class that will be extended by all the activities. In that base class you could add the code for dynamically adding footer at the bottom. In this approach you will have to take care of providing margins at the bottom of all your activity layouts.
  2. 您可以拥有将由所有活动扩展的Base Activity类。在该基类中,您可以添加用于在底部动态添加页脚的代码。在这种方法中,您将不得不在所有活动布局的底部提供边距。

  3. Another approach could be designing footer.xml and including it all you layout files using <include/> tag
  4. 另一种方法是设计footer.xml并使用 标签将所有布局文件包括在内

  5. Or alternatively you can create a Singleton class that would inflate footer.xml. This singleton instance can be used to display persistent footer
  6. 或者,您可以创建一个会使footer.xml膨胀的Singleton类。此单例实例可用于显示持久页脚

#2


0  

footer should be fragment. put it in your xml layout activities by fragment tag. in onCreateView() update your views, check if media is playing and so on ...
this way you have a single class to take care of and there is no duplicity.
also initializing views as static variables might help

页脚应该是碎片。通过fragment标签将它放在xml布局活动中。在onCreateView()中更新您的视图,检查媒体是否正在播放等等...这样您就可以使用单个类进行处理,并且没有任何重复性。将视图初始化为静态变量可能会有所帮助

hope this helps:

希望这可以帮助:

public class PlayFragment extends Fragment implements MyStaticStreamAudio.OnCompleteListener {

static boolean isPlaying = false;
static ToggleButton play;
static Button next, back;
static TextView titleView;


public PlayFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_play, container, false);

    play = (ToggleButton) rootView.findViewById(R.id.tbPlay);
    next = (Button) rootView.findViewById(R.id.bNext);
    back = (Button) rootView.findViewById(R.id.bBack);
    titleView = (TextView) rootView.findViewById(R.id.tvTitle);

    if (isPlaying) {
        play.setEnabled(true);
        play.setChecked(true);
    }

    play.setOnClickListener(new PlayChangeListener());
    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (canNext()) {
                goNext();
                updateView();
            }
        }
    });
    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (canBack()) {
                goBack();
                updateView();
            }
        }
    });

    return rootView;
}

private void goBack() {
MyStaticStreamAudio.goBack();
    resetAndPlay();
}

private void goNext() {
MyStaticStreamAudio.goNext();
    resetAndPlay();

}

public void pauseMode() {
    isPlaying = false;
    play.setChecked(false);
    play.setEnabled(true);
}

private void resetAndPlay() {
    MyStaticStreamAudio.reset();
    MyStaticStreamAudio.initializePlayer(getActivity());
    MyStaticStreamAudio.play();
}

boolean canNext() {
    int current = MyStaticStreamAudio.getCurrentTrack();
    return current < MyStaticStreamAudio.getTracks();
}

boolean canBack() {
    int current = MyStaticStreamAudio.getCurrentTrack();
    return current > 1;
}


public void updateView() {

    if (canNext())
        next.setEnabled(true);
    else
        next.setEnabled(false);
    if (canBack())
        back.setEnabled(true);
    else
        back.setEnabled(false);

    if (play != null) {
        play.setEnabled(false);
        play.setChecked(true);
    }
}

public void startStream() {
    isPlaying = true;
    updateView();
}


@Override
public void onComplete() {
    play.setChecked(false);
    progress.setIndeterminate(false);
}

public void playMode() {
    play.setEnabled(true);
    play.setChecked(true);
}

}

#1


0  

Based on you requirements, You can add a static footer in the activity layout. Switcher view in your Viewpager could be different fragments.

根据您的要求,您可以在活动布局中添加静态页脚。 Viewpager中的切换器视图可能是不同的碎片。

So fragments would keep on changing and footer would be persistent. There are many tutorials available out there related with using fragments with activities.

所以片段会不断变化,页脚会持续存在。有许多教程可用于将片段与活动一起使用。

One could be found here. Also have a look at this one.

一个人可以在这里找到。还看看这一个。

Update[as per the image updated in the question]: There could be many ways to achieve the UI you mentioned. Currently i could think of these ways:

更新[根据问题中更新的图像]:可以通过多种方式实现您提到的UI。目前我可以想到这些方式:

  1. You can have Base Activity class that will be extended by all the activities. In that base class you could add the code for dynamically adding footer at the bottom. In this approach you will have to take care of providing margins at the bottom of all your activity layouts.
  2. 您可以拥有将由所有活动扩展的Base Activity类。在该基类中,您可以添加用于在底部动态添加页脚的代码。在这种方法中,您将不得不在所有活动布局的底部提供边距。

  3. Another approach could be designing footer.xml and including it all you layout files using <include/> tag
  4. 另一种方法是设计footer.xml并使用 标签将所有布局文件包括在内

  5. Or alternatively you can create a Singleton class that would inflate footer.xml. This singleton instance can be used to display persistent footer
  6. 或者,您可以创建一个会使footer.xml膨胀的Singleton类。此单例实例可用于显示持久页脚

#2


0  

footer should be fragment. put it in your xml layout activities by fragment tag. in onCreateView() update your views, check if media is playing and so on ...
this way you have a single class to take care of and there is no duplicity.
also initializing views as static variables might help

页脚应该是碎片。通过fragment标签将它放在xml布局活动中。在onCreateView()中更新您的视图,检查媒体是否正在播放等等...这样您就可以使用单个类进行处理,并且没有任何重复性。将视图初始化为静态变量可能会有所帮助

hope this helps:

希望这可以帮助:

public class PlayFragment extends Fragment implements MyStaticStreamAudio.OnCompleteListener {

static boolean isPlaying = false;
static ToggleButton play;
static Button next, back;
static TextView titleView;


public PlayFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_play, container, false);

    play = (ToggleButton) rootView.findViewById(R.id.tbPlay);
    next = (Button) rootView.findViewById(R.id.bNext);
    back = (Button) rootView.findViewById(R.id.bBack);
    titleView = (TextView) rootView.findViewById(R.id.tvTitle);

    if (isPlaying) {
        play.setEnabled(true);
        play.setChecked(true);
    }

    play.setOnClickListener(new PlayChangeListener());
    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (canNext()) {
                goNext();
                updateView();
            }
        }
    });
    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (canBack()) {
                goBack();
                updateView();
            }
        }
    });

    return rootView;
}

private void goBack() {
MyStaticStreamAudio.goBack();
    resetAndPlay();
}

private void goNext() {
MyStaticStreamAudio.goNext();
    resetAndPlay();

}

public void pauseMode() {
    isPlaying = false;
    play.setChecked(false);
    play.setEnabled(true);
}

private void resetAndPlay() {
    MyStaticStreamAudio.reset();
    MyStaticStreamAudio.initializePlayer(getActivity());
    MyStaticStreamAudio.play();
}

boolean canNext() {
    int current = MyStaticStreamAudio.getCurrentTrack();
    return current < MyStaticStreamAudio.getTracks();
}

boolean canBack() {
    int current = MyStaticStreamAudio.getCurrentTrack();
    return current > 1;
}


public void updateView() {

    if (canNext())
        next.setEnabled(true);
    else
        next.setEnabled(false);
    if (canBack())
        back.setEnabled(true);
    else
        back.setEnabled(false);

    if (play != null) {
        play.setEnabled(false);
        play.setChecked(true);
    }
}

public void startStream() {
    isPlaying = true;
    updateView();
}


@Override
public void onComplete() {
    play.setChecked(false);
    progress.setIndeterminate(false);
}

public void playMode() {
    play.setEnabled(true);
    play.setChecked(true);
}

}