
我在Android应用程序中使用Master / Detail设计.如果我单击ItemListActivity中列表视图中的项目,则会将位置提供给我的ItemDetailActivity.这个实现了VIEwPager和FragmentStatePagerAdapter.所以我想在细节视图之间滑动.
我不知道如何告诉VIEwPager从所需的位置开始(ItemListActivity中的整数).无论我在列表中点击哪一个,我的VIEwPager始终位于列表的第0位.
这是我的代码:
ItemDetailActivity
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(); } }ItemDetailFragment:
/** * A fragment representing a single Item detail screen. This fragment is either * contained in a {@link ItemListActivity} in two-pane mode (on tablets) or a * {@link ItemDetailActivity} on handsets. */public class ItemDetailFragment extends Fragment { private static int fPos; static RSSFeed fFeed; private ShareActionProvIDer mShareActionProvIDer; Date pDate; /** * Mandatory empty constructor for the fragment manager to instantiate the * fragment (e.g. upon screen orIEntation changes). */ public ItemDetailFragment() { } /** * Factory method for this fragment class. Constructs a new fragment for the given page number. */ public static ItemDetailFragment create(int pos, RSSFeed Feed) { ItemDetailFragment fragment = new ItemDetailFragment(); Bundle args = new Bundle(); args.putSerializable("Feed", Feed); args.putInt("pos", pos); fragment.setArguments(args); return fragment; } @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // show Actionbar setHasOptionsMenu(true); // get data from DetailActivity fFeed = (RSSFeed) getArguments().getSerializable("Feed"); fPos = getArguments().getInt("pos"); } @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { // inflate the fragment with the custom detail fragment VIEw vIEw = inflater .inflate(R.layout.detail_fragment, container, false); return vIEw; }@Suppresslint("SetJavaScriptEnabled") @OverrIDe public voID onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //here I put the data in my webvIEw }activity_item_detail.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>所以目前我可以对所有项目进行平局,但始终从第一项开始.我想在我列表视图中点击的那个项目上启动它.
我怎样才能做到这一点?
解决方法:
根据文档,您需要做的就是使用
mPager.setCurrentItem(pos);http://developer.android.com/reference/android/support/v4/view/ViewPager.html#setCurrentItem(int)
总结以上是内存溢出为你收集整理的Android:Fragment和ViewPager始终在第一个位置启动全部内容,希望文章能够帮你解决Android:Fragment和ViewPager始终在第一个位置启动所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)