Navigation tabs with fragments to which to add another fragment - android

I have a recording app which I recently convert it on tabs layout. One tab is called Recorder and the other one is called Player. Both are integrated in a SherlockFragmentActivity. Each tab represent a fragment with its own layout. The Player fragment has a list with recordings and clicking on an item will open another activity which plays the recording.
All I want is to get rid of that activity and to incorporate a player control directly in the Player fragment. Basically I want to add another fragment in the player fragment (at bottom of the recordings list I want to add a fragment with play, pause, stop, etc buttons). Is it a good practice to add a fragment into another one? If not, how should I add that player control panel?(without simply adding the buttons in the same layout as the list is).

Is it a good practice to add a fragment into another one?
Sure if you decide that you want this, that's why nested fragments have been introduced on the platform. Based on your situation and due to the use of fragments as tabs I wouldn't use a nested fragment, I would just insert the controls in the fragment's layout and change the visibility for that part as the user plays/stops stuff.

Related

Display slidingUpPanel across all activities

I'm trying to add a slidingUpPanel(Sliding up panel) across all activities. This panel consists of a viewpager which in turn consists of two fragments. One of these fragments has a recyclerView (more than 1000 items) and the other fragment has some data that changes dynamically depending on the users' choice. This panel is very similar to that of google play music and soundcloud. Now to display this panel I tested two approaches:
1) I created a base activity and added the sliding up panel to it and then extended rest of the activities to base activity. So this way I have to create only one panel and viewpager.
2) I included the sliding up panel in all activities. But this approach is quite unmanageable as I have create viewpagers for all activities and if data changes in one activity I have to write the code to reflect that change across all activities.
Now the problem with both the methods is that each time I open a new activity, a new instance of the viewpager and fragments is created. So, suppose if I have 1000+ items in the recyclerview fragment, switching activities takes more than 2-3 seconds, because each time new instance of fragment is created and the data is loaded all again. This will definitely result in bad user experience.
Is there any way by which the viewpager and the fragments are created only once(when the app starts) and are destroyed only when user closes the app? And data should not load each time user switches activities. I just want to reduce the activity switching time. Any ideas?
Thank you.
Well, for such ui elements as your sliding panel, which stays the same for many items it is preferable to have a single activity.
So if you have 1 activity, you can have a viewpager inside sliding panel, and that part stays untouched. Next, inside your activity you can have FrameLayout wich can be used to host fragments. Doing this you can achieve single instance of sliding panel and navigation between content with fragments.
Having some heavy data collections makes you wishing minimum recreation of that items.
Two approaches for this are (assuming you're using SlidingUpPanelLayout by sothree)
With bottom navigation view
Easy way to do it is creating a bottom navigation view and keeping it in MainActivity and attaching the sliding up panel layout to the bottom navigation view ( layout_above = bottomnavbar_id ) since bottom navigation stays throughout the app so sliding up panel will also have to stay with it
Without bottom navigation view
Create a frame layout inside MainActivity give attributes width and height as match_parent and create slidinguppanelayout and give attributes gravity="bottom"
make the frame layout stay above that slidinguppanellayout , use that frame_layout to show content your want to show from fragments
that's all

Android: Fragments or Activity for my app?

I am new to Android programming. I started building an app using YouTube tutorials but I am a little bit confused and facing a problem. Should I use fragments or activity in my sliding tab? I am working on an Android project of employee attendance and payroll so I am thinking of using a slide tab (something like the screenshot below). The 1st tab may contain a form to add an employee and after adding it, will display information in list view and the other may contain salary. Should I try fragment or activity for my tabs?
A fragment is always located in an activity.
So you wil always need an Activity and then add the fragment to the activity.
if you say you want to slide?
you probably mean a viewpager, that can have n fragments. the viewpager needs a special viewpageradapter. where you define how many fragments you want. and at which index what fragment needs to be shown

How to include extra fragments onto a ViewPager, in Android?

I am currently using a ViewPager with a TabLayout, the ViewPager as of now consists of three separate fragments.
I would like to be able to switch to a fourth fragment using a button on one of the already present fragments.
However, I do not want the fourth fragment to be accessible through the usual ways (aka scrolling) without using the button to reach the fourth fragment. Similarly, I would like scrolling to be disabled when I am currently displaying my fourth fragment.
What would be the best way to do so?
The current hack that I can think of would be to create a hidden fragment within my main layout. I can then display it when the button is pressed while hiding the ViewPager at the same time.
Are there any better ways to do this?
Include your ViewPager in fragment and onClick make replace for the next fragment, and you can return to ViewPager`s fragment onBackPress.

Common footer layout in all activities

I am developing a local music player application in which I am having a common view (player) that has four button (play, pause, previous and next).
I need to use that view in all activities ie. song list, album list, artist list.
In sort user should be able to operate player from all screens.
One way is to create a common activity and extends that activity in all actvity and inflate player layout but in this case i have to implement click events in all activities.
what is the best way to implement this.
many thanks.
The best approach to this is by using Fragments. Create a fragment which connects to the service and provides an UI to control it, then add this fragment to all the activities where you need it or add the activity to a parent activity and extend it by using inheritance in all the child activities.
Make a footer layout file and then use include in all layout file you want to display that footer,
<include layout="#layout/footer" />
use weightSum to manage height of footer in all screen
OR
you can also try switching activities without animation.

Changing layout files for a fragment during runtime

I am trying to develop an app in which fragments are involved. There are 2 fragments on screen. The list fragment containing a Start and Stop button and a detail fragment on the right side. On click of the Start button an audio processing code runs in the detail fragment. So depending on the result i get out of the process I want to change the layout of the fragment. Basically I want to change the layout file of a single fragment during run time depending on the results I get while doing some process. How can I achieve it?
Thanks!!
You cannot switch to a different layout file in a fragment, the view you returned from onCreateView cannot be replaced.
However, you have several options:
You can show and hide views at runtime with View.setVisibility().
ViewStubs can be inflated at a later time.
Or you could replace your detail fragment with a new fragment.

Categories

Resources