Android 带片段的PagerView
我有一个关于书籍的列表视图。当我单击该行时,我可以在newfragment中看到书籍的详细信息,类似于中的内容。我想滑动书本(右->列表中的下一个元素,左确定,这样您就有了一个显示ListFragment(Android 带片段的PagerView,android,android-fragments,android-viewpager,Android,Android Fragments,Android Viewpager,我有一个关于书籍的列表视图。当我单击该行时,我可以在newfragment中看到书籍的详细信息,类似于中的内容。我想滑动书本(右->列表中的下一个元素,左确定,这样您就有了一个显示ListFragment(RecycleServicewFragment)的活动。单击列表项时,ListFragment将被显示书本详细信息的PagerViewFragment替换 尝试此操作。创建一个包含ViewPager的片段。单击项目时,此片段将替换ListFragment。我们将其称为BookDetailsCo
RecycleServicewFragment
)的活动。单击列表项时,ListFragment将被显示书本详细信息的PagerViewFragment
替换
尝试此操作。创建一个包含ViewPager的片段。单击项目时,此片段将替换ListFragment。我们将其称为BookDetailsContainer
,并将接收列表项目id:
BookDetailsContainer newFragment = BookDetailsContainer.newInstance(id);
在此片段中,您设置了viewpager:
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getChildFragmentManager());
mPager.setAdapter(mPagerAdapter);
ViewPager需要getChildFragmentManager()
来处理嵌套片段(BookDetailsContainer中的PagerViewFragment)。由于您将列表位置id传递给了BookDetailsContainer
,因此您可以设置位置:
mPager.setCurrentItem(positionId);
在screensilepageradapter
中,您将创建一个PagerViewFragment
实例,
根据
定位,使用Bundle将该数据传递给片段,然后返回片段:
@Override
public Fragment getItem(int position) {
PagerViewFragment frag = new PagerViewFragment();
Bundle args = new Bundle();
//get data from your data source based on the "position"
args.putString("BookTitle", ...);
args.putString("BookAuthor", ...);
...
frag.setArguments(args);
return frag;
}
这基本上就是你可以做到的。@ILovemyPoncho当然,我知道这个页面,但如何在我的情况下实现它。我离找到解决方案更近了,但你的答案还没有解决我的问题。我希望你现在就能够解决它。似乎你没有夸大
活动\u screen\u slide.xml
,当你试图找到ViewPager时,它是eturns null,无法设置适配器。不要在getItem()内设置TextView,应在onCreateView()中完成。包含viewpager的片段不应与从getItem()返回的片段相同。您需要一个片段来充气activity\u screen\u slide.xml
在那里您将找到ViewPager并设置适配器。另一个不同的片段用于充气pager\u fragment.xml
在那里您将找到并设置文本视图,这个片段将从getItem()返回.@ILovemyPancho您有任何示例代码吗?教程?似乎要困难得多。适配器应该在PagerViewFragment中。在本课程中,我应该如上所述为“BookDetailsContainer”充气。在“BookDetailsContainer”中,我应该为mPager和mAdapter充气?这是mate
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/blank_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.szymon.fragmentexample.BlankFragment">
<TextView
android:id="@+id/street_article"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="50dp"
android:text="hello_blank_fragment"
android:textSize="23dp" />
<TextView
android:id="@+id/artist_article"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/street_article"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:text="hello_blank_fragment"
android:textSize="15dp" />
<TextView
android:id="@+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/artist_article"
android:layout_marginLeft="50dp"
android:layout_marginTop="100dp"
android:text="here will be the picture"
android:textSize="15dp" />
</RelativeLayout>
</ScrollView>
BookDetailsContainer newFragment = BookDetailsContainer.newInstance(id);
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getChildFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setCurrentItem(positionId);
@Override
public Fragment getItem(int position) {
PagerViewFragment frag = new PagerViewFragment();
Bundle args = new Bundle();
//get data from your data source based on the "position"
args.putString("BookTitle", ...);
args.putString("BookAuthor", ...);
...
frag.setArguments(args);
return frag;
}