Android:Fragment和ViewPager总是在第一个位置启动
我为我的Android应用程序使用主/详细设计。如果我在ItemListActivity中单击listview中的某个项目,则该位置将指定给我的ItemDetailActivity。这一个实现了ViewPager和FragmentStatePagerAdapter。所以我想在细节视图之间滑动 我不知道如何告诉ViewPager从所需的位置开始(ItemListActivity中的整数)。我的ViewPager始终从列表的位置0开始,无论我在列表上单击了哪个位置 这是我的代码: ItemDetailActivityAndroid:Fragment和ViewPager总是在第一个位置启动,android,listview,android-fragments,android-viewpager,Android,Listview,Android Fragments,Android Viewpager,我为我的Android应用程序使用主/详细设计。如果我在ItemListActivity中单击listview中的某个项目,则该位置将指定给我的ItemDetailActivity。这一个实现了ViewPager和FragmentStatePagerAdapter。所以我想在细节视图之间滑动 我不知道如何告诉ViewPager从所需的位置开始(ItemListActivity中的整数)。我的ViewPager始终从列表的位置0开始,无论我在列表上单击了哪个位置 这是我的代码: ItemDetai
public class ItemDetailActivity extends FragmentActivity {
RSSFeed feed;
int pos;
private static List<Fragment> fragments;
/**
* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.
*/
private ViewPager mPager;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private PagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// define layout of this activity
setContentView(R.layout.activity_item_detail);
// Get the feed object and the position from the Intent
feed = (RSSFeed) getIntent().getExtras().get("feed");
//position of the clicked item from the list
pos = getIntent().getExtras().getInt("pos");
//create all 20 ItemDetailFragments
fragments = new ArrayList<Fragment>();
for(int i=0; i<=20; i++)
fragments.add(ItemDetailFragment.create(i ,feed));
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.viewpager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setPageTransformer(true, new ZoomOutPageTransformer());
}
@Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
// If the user is currently looking at the first step, allow the system to handle the
// Back button. This calls finish() on this activity and pops the back stack.
super.onBackPressed();
} else {
// Otherwise, select the previous step.
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
/**
* A simple pager adapter that represents ItemDetailFragment objects, in
* sequence.
*
* Creates a class that extends the FragmentStatePagerAdapter abstract class and
* implements the getItem() method to supply instances of ScreenSlidePageFragment
* as new pages. The pager adapter also requires that you implement the getCount()
* method, which returns the amount of pages the adapter will create.
*/
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int page_number) {
return fragments.get(page_number);
}
@Override
public int getCount() {
//get the count of items in the feed
return feed.getItemCount();
}
}
活动项目详细信息.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/main_item_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="@+id/item_detail_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/adView"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
tools:context=".ItemDetailActivity"
tools:ignore="MergeRootFrame" >
</FrameLayout>
所以现在我可以把所有的项目都列出来,但它总是从第一个项目开始。我想从我在listview中单击的项目开始
我该怎么做呢?您实际上已经在使用您的
onBackPressed()方法中需要使用的方法了。
方法:
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
只需在包中传递初始位置,然后在设置时调用此方法。根据文档,您只需使用
mPager.setCurrentItem(pos);
您看过viewpager的文档了吗?非常感谢你的工作。请将其作为答案发布,我可以将其标记为解决方案
mPager.setCurrentItem(pos);