I have an Activity which contains a TabLayout and a ViewPager. The first tab contains a calendar and the second tab contains a timer.
Tapping on a date in the first tab should display a fragment with some event details while simultaneously hiding or removing that menu tab.
To this end, how can I use a FrameLayout as a fragment container when my layout already contains a ViewPager? Should I create a new Activity to get what I want, or is there a more efficient solution?
What you are describing is basically a list/details pattern. In short, yes, you should create a new activity for the details. It is the expected flow of an android app and it makes the implementation for you so much easier.
Related
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
im trying to implement an android aplication when your activity, cointains 3 or more 'main windows' like in the image -> 'A'. 'B'.'C'. so how is posible make when you slide you touch screen change from A, to B, for example, i was thinkin in a horizontal view, and inside of each item use a relative layout, but im not sure, its my first time with this kind of problem, thanks.
Try using android ViewPager. Each ImageView would be inside a Fragment that would reside inside your Activity. Check out this example
Use Tabbed Activity template when you want to make this type of Activity. ViewPager, Adapter and tab layout will be automatically implemented for you. Just make small changes in your Adapter according to your needs and use one Fragment for one tab and you can create as many tabs as you want in an Activity.
If you want i will post an example of an Adapter as i just implemented an Activity with 3 tabs.
To get your basics strong on ViewPager, tab layout and Fragment sections adapter read out this link.
First - sorry for the newbish question.
I've started building an app that has a single Activity and a navigation drawer. Each menu item loads a new fragment in the middle frame layout.
I want to create another fragment that:
has tabs
allows for swipe scrolling
It seems like the only way to do this is to create add a ViewPager and PagerTabStrip. All the tutorials I've read indicate the ViewPager requires extending out to FragmentActivity. I have a few questions:
Am I doing anything wrong by replacing the fragment content when navigating menu options?
If what I am doing is ok, is there anyway to incorporate swipe navigation without calling FragmentActivity?
If I need to use FragmentActivity for this one page, I'm assuming I'll call change pages via Intent. Doing so would result in losing the click actions in the navigation drawer. Do I have to call (or duplicate) my code from one activity to another?
EDIT
Sorry about the confusion. Here's what I'd like my application do:
Open app. MainActivity starts. Navigation drawer loads. Main content is loaded via a fragment.
User opens navigation drawer and selects this new menu item I'm creating. It is a new fragment that loads in the frame (like the other menu items). However, it has tabs and supports swiping.
ViewPager is just usual descendant of View so it can't require using of FragmentActivity.
It's absolutely ok.
You don't need to use FragmentActivity. I suppose you just read tutorial about "Implementation of drawer" where author of the tutorial used FragmentActivity.
Can't understand what do you mean. Pages of ViewPager is just views not activities. You don't need to use Intent.
PS Actually I can't understand your problem at all. It's absolutely unclear why you don't want to use FragmentActivity.
I have to show two tabs each containing ListFragment classes. On clicking any item of the list, it's details should open on the right panel on landscape views. This is much like the official Android fragments example.
What I want to achieve is that on the left side of the layout the list view should be in tabs, i.e. two list views within tabs. On clicking any item the details should open on the right. Till now, I can show tabs and details, but the details are not showing up on the right. They open as a new activity.
Tab Navigation can be an option, but due to some design restraints in my app, I can't use that. Please guide.
Please check this android blog http://android-developers.blogspot.com/2011/02/android-30-fragments-api.html It has a complete tutorial of how to use fragments as a Master-Details View.
I came to a solution. I am explaining how I did it, in case someone needs it.
I wanted to make tabs on the left fragment. Due to design constraints, I could not use Tab Navigation, as I had to use List Navigation too.
So, I made a new fragment placed over the left fragment and inflated it with two buttons. On different button clicks, I used FragmentTransaction to add new fragment to the left fragment.
On button click listener I used fragmentTransaction.replace method.
It is my first time using android fragment. I am following this tutorial to implement a fragment.
Everything is fine with this tutorial, I successfully get the result like below:
In the tutorial, the DetailsFragment simply shows a TextView containing the text of the currently selected item. That's the right part shows just some texts.
My question is how to show different activities on the right side instead of text views.
What I mean is illustrated in the following image, for example, the area of "1" in the image is an activity. How to show different activities when a list item on the left hand side has selected?
You do not show an activity, you show a fragment. Implement the Fragment class instead of the Activity class. Then you build your View just as you would in an Activity. Remember that for instances when you need access to an activity the Fragment class has the convenient getActivity() method.
Use FragmentManager.beginTransaction() to start FragmentTransaction. With that operation you can hide and show new Fragments. It is also managed with the android history stack.
https://developer.android.com/reference/android/app/FragmentManager.html#beginTransaction()
https://developer.android.com/reference/android/app/FragmentTransaction.html
And here is some code: Android Honeycomb: How to change Fragments in a FrameLayout, without re-creating them?