Android: ViewPager and Fragments
16lz
2021-12-22
本文转自:http://tamsler.blogspot.co.uk/search/label/viewpager
- The following is a simple Android sample application that uses a ViewPager, which is part
of the Support Package,and Fragments.
The FragmentPagerActivity class creates the ViewPager and the associated FragmentPagerAdapter.
import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; public class FragmentPagerActivity extends FragmentActivity { private static final int NUMBER_OF_PAGES = 10; private ViewPager mViewPager; private MyFragmentPagerAdapter mMyFragmentPagerAdapter; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mViewPager = (ViewPager) findViewById(R.id.viewpager); mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager()); mViewPager.setAdapter(mMyFragmentPagerAdapter); } private static class MyFragmentPagerAdapter extends FragmentPagerAdapter { public MyFragmentPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int index) { return PageFragment.newInstance("My Message " + index); } @Override public int getCount() { return NUMBER_OF_PAGES; } } }
- The PageFragment only contains a TextView. It isinstantiated in the above
MyFragmentPagerAdapter's getItem(int index) method.
package org.thomasamsler.android; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class PageFragment extends Fragment { public static PageFragment newInstance(String title) { PageFragment pageFragment = new PageFragment(); Bundle bundle = new Bundle(); bundle.putString("title", title); pageFragment.setArguments(bundle); return pageFragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment, container, false); TextView textView = (TextView) view.findViewById(R.id.textView1); textView.setText(getArguments().getString("title")); return view; } }
- Sources:
- Fragments
- Horizontal View Swiping with ViewPager
- ViewPagerExample
- The PageFragment only contains a TextView. It isinstantiated in the above
- What are the different ways to get a reference to the currently visible fragment page in a ViewPager?
First Solution
You can set a unique tag on each fragment page:
getSupportFragmentManager().beginTransaction().add(myFragment, "Some Tag").commit();
... and then retrieve the fragment page via:
getSupportFragmentManager().findFragmentByTag("Some Tag");
Using this approach, you need to keep track of the string tags and associate them with all the fragment pages. You could use a map to store each tag along with the current page index, which is set at the time when the fragment page isinstantiated.
MyFragment myFragment = MyFragment.newInstance(); mPageReferenceMap.put(index, "Some Tag"); getSupportFragmentManager().beginTransaction().add(myFragment, "Some Tag").commit();
To get the tag for the currently visible page, you then call:
int index = mViewPager.getCurrentItem(); String tag =mPageReferenceMap.get(index);
... and then get the fragment page:
Fragment myFragment =getSupportFragmentManager().findFragmentByTag(tag);
Second Solution
Similar to the first solution, you keep track of all the "active" fragment pages. In this case, you keeptrack of the fragment pages in theFragmentStatePagerAdapter, which is used by the ViewPager.
public Fragment getItem(int index) { Fragment myFragment = MyFragment.newInstance(); mPageReferenceMap.put(index, myFragment); return myFragment; }
To avoid keeping a reference to "inactive" fragment pages, we need to implement the FragmentStatePagerAdapter's destroyItem(...) method:
publicvoid destroyItem(View container, int position, Object object) { super.destroyItem(container, position, object); mPageReferenceMap.remove(position); }
... and when you need to access the currently visible page, you then call:
int index = mViewPager.getCurrentItem(); MyAdapter adapter= ((MyAdapter)mViewPager.getAdapter()); MyFragment fragment = adapter.getFragment(index);
... where theMyAdapter'sgetFragment(int) method looks like this:
publicMyFragment getFragment(int key) { returnmPageReferenceMap.get(key); }
I am using the second solution in myproject, and it's working quite well. Here is the reference to the fullsource code.
代码:http://download.csdn.net/detail/jdsjlzx/4395451
更多相关文章
- Android网格布局实现--recyclerview
- Android(安卓)常用代码总结 工具类
- Android(安卓)Studio Service 篇一
- Ubuntu 10.04编译Android(安卓)2.2 源代码
- Android代码混淆
- BaseAdapter
- Android(安卓)Spinner控件的简单应用
- 调用android系统自带功能
- 关于android的des算法代码