I currently have 1 activity that jas 2 fragments top half and botton half. I want to swipe the bottom fragment only. (java implented)
Before i go learning the wrong method could someone please tell me which method I should use.
Do you sill use pageView and a fragment adapter or something.
Any usefull tutorials that do similar would be good so i can figure it out.
Define fragment and viewpager in the same activity. in the fragment show thetop fragment and in viewpager, write one pager adapter and invoke all the child fragments into that pager. This solves your problem.
Your activity layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<space
android:id="#+id/layout"
android:layout_width="match_parent"
center_horizontal="true"
center_vertical="true"
android:layout_height="0.5dp">
<fragment
android:id="#+id/layout"
android:layout_width="match_parent"
layout_above="#id/space_layout"
android:layout_height="match_parent"/>
<android.support.v7.widget.viewpager
android:id="#+id/layout"
android:layout_width="match_parent"
layout_below="#id/space_layout"
android:layout_height="match_parent">
Then define PagerAdapter class and show all your child fragments in that.
all the best
Related
I have an app, that has a few fragments in it. I'm using ViewPager 2 to swipe between them.
Lately I've added Navigation component to make a way to get into one specific fragment that is not avilable for user just by swipe.
I've found out, that whever I place FragmentContainerView over the ViewPager2 in my MainActivity layout, I can go to this unvailable fragment, but I can't swipe. Same thing whener I switch FragmentContainerView with ViewPager2. Is there any way to make them both work at the same time?
Here's my layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph" />
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
They can work together but not like this, at least not for what you want.
Navigation component is made to handle traveling between destinations. ViewPager2 is a widget to be used in a destination, not for traveling between them.
For what you are describing you will need to have FragmentA that will contain ViewPager2 where all of the swiping will take place and handling of fragments (let's say Fragment1, Fragment2, Fragment3) in the ViewPager2.
Those fragments will have nothing to do with the Navigation component.
Navigation component will come to play when you will want to go from FragmentA to FragmentB.
I am looking for a way to run activity and fragment at the same time where activity will show two buttons to transition from one activity to the other whereas fragment will show live camera view in the background.
is it possible?
Fragments are the part of activity. So it's obvious that activity and fragment run simultaneously run. Fragment does not have it's own context. After read your question I think you want that you can change the fragments but buttom buttom are place there for all fragment. You can do this like:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container"
android:layout_above="#+id/button"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/button"/>
</RelativeLayout>
add and replace your fragment on framelayout so each fragment have the button at bottom which is the part of activity.
How can I have my BottomNavigationView in several activities but without declaring it there?
i declared my Bottom navigation view in my main_activity.xml and handle the click logic within MainActivity class, but since I want to switch between 3 activities (Map, Friends, Chats) with the Bottomnavigationview i'd have to have 3 bottom navigation views over three activities, but since that's performance inefficient, I want to handle all the click events and initialisation of the view inside one class and one Layout
(MainActivity class and main_activity.xml ).
What can I do? or is there a better way of doing this?
You should use fragments instead of activities. Have only 1 activity ie MainActivity, add BottomNavigationView and fragments above the BottomNavigationView. And on clicking the BottomNavigationView, replace the fragments.
Your XML would be something like,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="#+id/bottom_nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_above="#id/bottom_nav_view"></FrameLayout>
From your activity, add fragments in FrameLayout. Your BottomNavigationView will remain intact. Just replace fragments in this FrameLayout on clicking NavigationView tabs.
I'm currently working on app with a view hierarchy of CoordinatorLayout -> Viewpager -> Framelayout (contained in a fragment). The CoordinatorLayout ensures that the actionbar scrolls away when scrolling in the fragment.
The problem I'm facing is, that I would like to have a view in the bottom of the fragment that does not scroll, but instead always stays at the bottom of the fragment. The view should only exist in one of the fragments, meaning placing it outside the Viewpager does not work.
Is it possible to create a static view in the fragment (Framelayout)?
im not 100% sure if this is what your looking for if it isnt explain (with code) what your looking for.
good luck with the project
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
android:id="#+id/coordinatorLayout"
android:fitsSystemWindows="true">
//Part that will be static
<ScrollView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/imageView2">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
//part in the scrollfiew
</LinearLayout>
</ScrollView>
</RelativeLayout>
I have a Toolbar and some fragments in FrameLayout. However in such a case I am asked to make an ImageView in a fragment which its half will be on the Toolbar. I couldn't figure it out. I considered to cut the image and make it an Options menu item and rest of it will be on the top of the fragment, but I think this won't be very effective for different devices. Can you please help me?
I don't sure what exactly u need.
But if I get it. Here is something u can do.
U just need to make your fragment on whole screen and draw toolbar over fragment.
Here is the snippet. I don't tested it:
your_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Toolbar //name possibly incorrect(change to correct one)
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/toolbar_height"/>
</RelativeLayout>
Then inside fragment layout:
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView //or any other container for your stuff
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="#dimen/toolbar_height"
android:clipToPadding="false"/>
<ImageView //your imageView that will draw halfover toolbar
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/half_toolbar_height"/>
</FrameLayout>
The snippet above possibly won't work correctly. But anyway it can be start point to solve your problem. Good luck!