gpt4 book ai didi

android - 循环 FragmentPagerAdapter

转载 作者:搜寻专家 更新时间:2023-11-01 09:08:18 27 4
gpt4 key购买 nike

我有一个可以正常工作的 FragmentPagerAdapter 但我想知道的是,如果 FragmentPagerAdapter 在最后一个 View 中,我如何让它循环回到首先查看并像往常一样继续

这是我正在使用的示例代码

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);
}
}

}

基本上,当它结束时,我如何让它显示第一个 fragment ,因为它会转到任何其他 fragment ?

即。 A->B, B->CC->A 和向后的 A->CC->BB->A

最佳答案

这是我的解决方案,我有“n”个屏幕(在本例中为 5 个),因为我想回到列表末尾的第一个屏幕,所以我“添加另一个屏幕”,这只是一个精确的副本最后一个屏幕的最后一个屏幕,也为相反的方向执行此操作。基本上我现在有“n”个屏幕+2(在这种情况下现在是 7 个)。屏幕设置如下 copy of C,A,B,C,copy of A 然后我设置了一个手势监听器来观察某些页面上的滑动。

如果在列表的末尾(“A 的副本”)并且我检测到向左滑动 > 一定数量的像素 我移动到页面 A并继续在正常的页面旋转中向后滑动。

向相反方向滑动也是如此。

问题

当从结束页面之一移动以模拟循环时,它确实会有点慢,因为 ViewPager 会加载您当前所在页面之前和之后的页面,但由于您在最后或页面列表的开头,它不会像通常那样加载下一页,因为从技术上讲没有下一页。只有当您对每个列表行都有复杂的自定义 View 时才会发生这种情况

你不能将它与动画一起用于页面滚动,因为它看起来不会无缝,并且当你从复制的页面移动到实际页面时会动画,即 A 的副本到 A。

而且有时它似乎确实卡在了页面的末尾,我不知道为什么

这是我基于 android 开发者网站上的 viewpager 示例的完整演示应用程序

public class FragmentPager extends FragmentActivity{
MyAdapter mAdapter;
public static ViewPager mPager;
static int currentPage = 1;
static final int NUM_ITEMS = 7;
public static View vContainer;
float dxInitial=0;

@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);
mPager.setCurrentItem(1,false);

mPager.setOnTouchListener(new OnTouchListener(){

@Override
public boolean onTouch(View v, MotionEvent event) {


if(event.getAction() == MotionEvent.ACTION_DOWN){
dxInitial=event.getX();
Log.d("INITIAL X", String.valueOf(dxInitial));
}else if(event.getAction() == MotionEvent.ACTION_MOVE){
if(event.getX() - dxInitial > 100 && currentPage==1){
mPager.setCurrentItem(6,false);
}else if(dxInitial - event.getX() > 100 && currentPage==5){
mPager.setCurrentItem(0,false);
}else if(dxInitial - event.getX() > 50 && currentPage==6){
mPager.setCurrentItem(1,false);
}else if(event.getX() - dxInitial > 50 && currentPage==0){
mPager.setCurrentItem(5,false);
}
}

/*if(Math.abs(dx) > Math.abs(dy)) {
if(dx>75){ directiion = "right";
}else if{
direction = "left";
}
} else {
if(dy>0) direction = "down";
else direction = "up";
}*/

return false;
}

});
mPager.setOnPageChangeListener(new OnPageChangeListener(){

@Override
public void onPageScrollStateChanged(int state) {
// TODO Auto-generated method stub
if(state == ViewPager.SCROLL_STATE_IDLE){

}
}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int arg0) {
currentPage=arg0;

}

});
}

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);
}

@Override
public Object instantiateItem(View container, int position) {
vContainer=container;
return super.instantiateItem(container, 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);
if(mNum == 6){
mNum=1;
}else if(mNum==7){
mNum=5;
}else if(mNum==0){
mNum=5;
}
((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);
}
}

}

关于android - 循环 FragmentPagerAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10247117/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com