user3471174
user3471174

Reputation: 35

How to use fragments of ViewPager?

I have one MainActivity and ViewPagerAdapter and 3 Tab fragments which I am displaying in my MainActivity with ViewPager in it.(Im using navigation drawer also but its ok.)

As a beginner in android fragments I do not understand how to send data from MainActivity to Tab1,Tab2,Tab3 of ViewPager. I tried to use this Send data from activity to fragment in android method to send data from MainActivity to Tab1 but application crashes with NullPointerException. So how can I use fragments such that I can send data from MainActivity to Tab1 (Which is fragment.) and setText and Image in it and update that fragment in Viewpager?

MainActivity.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.thewelp.materialtoolbartest1.MainActivity">

        <include
            android:id="@+id/app_bar"
            layout="@layout/app_bar">

        </include>
        <org.technoarena.tabs.SlidingTabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:layout_below="@+id/app_bar"/>

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_below="@+id/tabs"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:layout_weight="1"
            >

        </android.support.v4.view.ViewPager>

    </RelativeLayout>

    <fragment
        android:layout_width="@dimen/drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:id="@+id/fragment_drawer"
        app:layout="@layout/fragment_navigation_drawer"
        android:name="org.technoarena.technoarena2015.NavigationDrawer"
        tools:layout="@layout/fragment_navigation_drawer">

    </fragment>


</android.support.v4.widget.DrawerLayout>

Tab1.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">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="You Are In Tab 1"
        android:id="@+id/textView"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Upvotes: 1

Views: 107

Answers (1)

Sandeep Agrawal
Sandeep Agrawal

Reputation: 576

You can have a List for data and pass the data to viewpager adapter in constructor and in view pager you can pass the data to fragment in getItem() function.

Upvotes: 0

Related Questions