[英]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.) 我有一个MainActivity和ViewPagerAdapter以及3个Tab片段,它们在其中带有ViewPager的MainActivity中显示。(我也使用导航抽屉,但还可以)。
As a beginner in android fragments I do not understand how to send data from MainActivity to Tab1,Tab2,Tab3 of ViewPager. 作为android片段的初学者,我不了解如何将数据从MainActivity发送到ViewPager的Tab1,Tab2,Tab3。 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. 我试图使用此发送数据从活动到android方法中的片段,以将数据从MainActivity发送到Tab1,但应用程序崩溃并出现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发送到Tab1(哪个是片段。)并在其中添加setText和Image并在Viewpager中更新该片段?
MainActivity.xml 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 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>
您可以拥有一个数据列表,并将数据传递给构造函数中的viewpager适配器,而在视图分页器中,可以将数据传递给getItem()函数中的fragment。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.