Replace ViewPager in other fragment - android

I have a viewpager in my main activity, and viewpager contains a few pages (exactly a few fragments). I want to go from my viewpager to another fragment which isn't in viewpager. How can I do this?
Here is my activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.slpd.Activities.SLPDActivity">
<com.slpd.SlpdViewPager
android:id="#+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>

Why do you want to go to another fragment?
If there is one or more fragments you want to transition to, simply add a new activity which looks like your SLDPActivity and inflates a layout with another ViewPager.
If it is only one fragment you want to transition to, why not simply making it a new activity in the first place?

Related

How to dynamically show more or less fragments within 1 activity?

My app has a log_in activity, a main activity, and a settings activity.
The main activity is currently working by flowing between multiple fragments:
In the first fragment, the user selects a topic (from a dynamically-populated recyclerview of cardviews), the topic is then parceled and sent to the second fragment.
The second fragment displays the appropriate list (another dynamically-populated recyclerview of cardviews (formatted very differently, with different cardviews). The user can go back to the first fragment and choose another topic, and that choice is again parceled and sent to the second fragment to display the relevant list etc.
I want to set up the 2nd "page" so that it has 1 fragment at the top, which covers the top 80% of the screen, and 1 fragment at the bottom, which covers the bottom 20% of the screen, while still having only 1 fragment on the first "page" that covers 100% of the screen.
This is how I'm switching between each fragment in the flow right now:
main.java
bundle = new Bundle();
bundle.putParcelable("itemlist", itemlist);
fragment = new fragment_2();
fragment.setArguments(bundle);
fragmentManager = getSupportFragmentManager();
transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_container, fragment).commit();
And my activity_main.xml (for the main activity) has this layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/ColorBG"
tools:context="com.app.activity.main">
<FrameLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
So main_container is the FrameLayout that I'm replacing with the relevant fragment.
I can't find any clean example of a way to do this, can anyone show me?
The only thing I can think of is to have 2 FrameLayouts in the activity_main.xml, and have the 2nd container start as layout_width="0" and layout_height="0", but I feel this is a bad way to do it. Is there any way to replace the LinearLayout itself with another LinearLayout that has 2 FrameLayouts in it, and then assign the appropriate fragments to those containers?
Or what is the best way to do it?
I want to do things "the right way" (so I do not want to have separate activities) and I'm targeting API 16, but I may be willing to go up to API 23/24.
You can use weightSum property of LinearLayout to divide in 80:20 ratio.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3"
android:orientation="vertical">
<FrameLayout
android:id="#+id/frame1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"/>
<FrameLayout
android:id="#+id/frame2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
For using the same layout where you want to give 100% you can set the visibility of frame2 to GONE and can dynamically set the layout of frame1 to 3 (total of weightSum which will make it 100%).

How to add RecyclerView in extended fragment to display the list

I have extended 3 fragment in main activity..also how do i add recyclerview to display a list in one of my fragment.
Please help
Never mind you are begineer :) . Look first of all look how the fragment works.Fragment requires a activity . In activity you have to create framelayout and load or commit fragment in framelayout(Activity).
Now if you want to change any view in the activity you have to change it in fragment and call respective fragment in activity of change its view.
This means you have to add recyclerview in the activity but first off all you must add that fragment containing recyclerview to framelayout of the activity.
You are looking for something like this:
Note: Make sure you add this to your app gradle:
compile 'com.android.support:recyclerview-v7:23.4.0'
You have fragment1.java with layout fragment1.xml:
fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/fragment1_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
Initialize this RecyclerView inside fragment1.java and write is adapter.
For more on this, try these links:
How to implement RecyclerView with CardView rows in a Fragment with TabLayout
http://www.androidhive.info/2016/01/android-working-with-recycler-view/

ViewPager with setOffscreenPageLimit hiding my initial Activity title

I'm using a standard ViewPager inside an Activity. Very basic stuff:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/toolbar" />
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/new_teal"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Now, in my acitivity's code, I have an OnPageChangeListener which I'm using to switch the title in the toolbar whenever the viewpager's views are paged through.
I'm also calling setTitle(...) to set the title for the initial screen.
The issue with all of this is that I'm setting setOffscreenPageLimit(...), and when I do, that initial call to setTitle(...) gets overridden or hidden or something. The title just disappears!!!
If I remove setOffscreenPageLimit(...), then the entire thing misbehaves, as expected.
Any ideas???
For a effective solution we need to see your fragment's code but if you do not set a offScreenPageLimit to a view pager it create 1 previous and one next fragment of your current fragment. All these three fragments gets created and calls default fragment lifecycle methods such as onCreate, onViewCreated and etc.
If you are changing your title in one of your fragments which will be created when you set offScreenPageLimit to 2 or more, you can get this behave.
For example let's say your current Fragment is C and your view pager contains a b C d e and you change title in a or e fragments or other fragments in second or more position.
I hope this'll help you.

Android fragments overlap previous view and button listeners

I have an activity A with a fragment A inside.
Activity A uses layout X, and fragment A uses layout A.
code of layout X:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
android:id="#+id/fragment1"
android:name="android.app.DialogFragment"
android:layout_width="wrap_content"
android:layout_height="500dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="113dp"
class="com.example.fragtester.FragA" />
</RelativeLayout>
Layout A is just textview + linearlayout.
I set up another fragment B that uses layout B.
Now that I use the following code in activity A to change the fragments:
Fragment f = new FragB();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment1, f);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
I end up having layout B displaying under layout A.
So I use a FrameLayout to wrap the fragment in layout X and use
ft.replace(R.id.FrameLayout1, f);
Now the view is working nicely. Though, another problem arises.
Although layout B covers layout A, but the buttons are still active.
That means when I am viewing layout B, I can still click buttons on layout A, even if I am not seeing it.
And even when I add fragment C/D/E..... (layouts C/D/E....), the buttons on layout A is still active.
Can anybody explain why is that? Am I using fragments wrongly? Thanks!
A way to get through is to make layout A blank, and use other layout to cover it. But it doesn't seems to be the "right" way??
Remove the fragment and add a FrameLayout
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff" >
</FrameLayout>
then add fragments programmatically.
In android fragment button click pass through the fragments (i dont know if the fragments are suppose to work like that). what I used to do in such a situation is to make the layout of the fragment clickable. so the clicks wont pass through.
Instead of having fragment in your xml, try to create empty container for a fragments. For example empty frame layout. And then programmatically put your fragments in there.
Add the following attribute to the XML root layout of the fragment that goes on top.
android:clickable="true"
This will ensure that touch events will not propagate further than the top layer.

add Viewpager dynamically in a framelayout

Is it possible to replace a fragment in a FrameLayout with the .replace() to use a ViewPager? since both are ViewGroups.
For example, in xml, I have only
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id ="#+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
and I would like to replace the FrameLayout with id content that is holding a fragment to a ViewPager of fragments in runtime. Is it possible??
i would like to replace the framelayout with id content that is
holding a fragment to a viewpager of fragments in runtime. is it
possible??
You can't replace a Fragment with a simple ViewGroup like a ViewPager, but thanks to the new changes in 4.2 you could put the ViewPager itself in a Fragment and use nested Fragments as the children of the ViewPager. Then you could replace the initial Fragment with the Fragment that holds the ViewPager.
See this guide, nested Fragments are also available to lower APIs through the compatibility package. See this question for an implementation example.

Categories

Resources