我有一个FragmentPagerAdapter
,它的工作方式正常,但是我想知道如果FragmentPagerAdapter
在最后一页,如何使其循环回到第一页并像正常情况一样继续进行。
这是我正在使用的示例代码:
public class FragmentPager extends FragmentActivity {
MyAdapter mAdapter;
ViewPager mPager;
static final int NUM_ITEMS = 5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return NUM_ITEMS;
}
@Override
public Fragment getItem(int position) {
return MyFragment.newInstance(position);
}
}
public static class MyFragment extends ListFragment{
int mNum;
/**
* Create a new instance of CountingFragment, providing "num"
* as an argument.
*/
static MyFragment newInstance(int num) {
MyFragment f = new MyFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
/**
* When creating, retrieve this instance's number from its arguments.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments() != null ? getArguments().getInt("num") : 1;
}
/**
* The Fragment's UI is just a simple text view showing its
* instance number.
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
View tv = v.findViewById(R.id.text);
((TextView)tv).setText("Fragment #" + mNum);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, new String[] {"test1","test2","test3","test4","Test5"}));
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("FragmentList", "Item clicked: " + id);
}
}
}
基本上当它到达最后一个片段时,我该如何让它显示第一个片段,就像它会去任何其他片段一样?
例如:
A->B
,B->C
,C->A
和反向 A->C
,C->B
,B->A
。
C
的副本,A
,B
,C
,A
的副本然后我设置手势监听器并跟踪我所在的页面,如果在列表末尾,我将移动到第一页,即最后一页的副本,模拟循环。 - tyczj