如何建立一个无限的行为,并检测哪一方有人偷走一个viewpager? [英] How to build a viewpager that acts infinite and detects which side it was swiped?
问题描述
据我所知,viewpager可以用,但我有一个很难找到一个很好的资源,解释如何使用viewpager带有以下附加功能。
如果是刷卡向左或向右,而无论哪种方式,只需重新加载新的内容(如果离开,我希望它没有记录,右为它记录是)以记录。
我希望它是无穷的,真正的大,像10000。
最后,可以添加按钮,它下面还有。
在摘要存在任何方向,教程或例子是AP preciated。
- 的任何选项卡的 - 只是避免在创建视图时,寻呼机实施选项卡监听器和标签,包括
- 阅读挥笔
- 加载新的内容
- 无穷的,真正的大
- 的有它下面的按钮的 - 通过包括它放在抽屉布局完成
希望这个清除的东西了:
更新:
这里是我想读刷卡场景 - 如果不记录没有,如果正确记录的话,那么从缓存中抓住新的内容和显示它,用相同的两种刷卡的选择,我想也许我就可以有3个页面并向左滑动记录了行动,那么它重置到位置(中间 - 1),然后加载在缓存中下一个可用的内容...唯一的事情是我不知道该怎么称呼复位,并要求内容的刷新...
编辑:
没有标签已经解决了,只需添加code,像这样:
// viewpager适配器
私人PageAdapter mAdapter;
私人ViewPager viewPager;
//初始化寻呼机 - 在上创建
viewPager =(ViewPager)findViewById(R.id.random_pager);
FragmentManager FM = getFragmentManager();
mAdapter =新PageAdapter(getSupportFragmentManager(),新UserCreatedFragment(),新UserUpVotesFragment(),新UserDownVotesFragment());
viewPager.setAdapter(mAdapter);
//在这里,您将宣布在创建访问哪些页面
viewPager.setCurrentItem(1);
这是适配器:
公共类PageAdapter扩展FragmentPagerAdapter {
私人片段frag1;
私人片段frag2;
私人片段frag3; 公共PageAdapter(FragmentManager FM,片段frag1,片段frag2,片段frag3){
超(FM);
this.frag1 = frag1;
this.frag2 = frag2;
this.frag3 = frag3;
} @覆盖
公共片段的getItem(INT指数){
开关(指数){
情况下0:
//片段应记录并没有恢复到新的内容早在位置1的相同FRAG 返回frag2;
情况1:
对于活动//片段
返回frag2;
案例2:
//片段应该是记录和复位,以新的内容早在位置1的相同FRAG
返回frag2;
} 返回null;
}
//返回计数标签/刷卡次数
@覆盖
公众诠释的getCount(){
返回3;
}
}
活动的XML:
<?XML版本=1.0编码=UTF-8&GT?;<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent机器人:layout_height =match_parent>
< android.support.v4.widget.DrawerLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / drawer_layout
机器人:layout_width =match_parent
机器人:layout_height =match_parent> <! - 的FrameLayout显示片段 - >
<的FrameLayout
机器人:ID =@ + ID / frame_container
机器人:layout_width =match_parent
机器人:layout_height =match_parent/> <! - 列表视图显示滑块菜单 - >
< ListView控件
机器人:ID =@ + ID / list_slidermenu
机器人:layout_width =240dp
机器人:layout_height =match_parent
机器人:layout_gravity =开始
机器人:choiceMode =singleChoice
机器人:分=@彩色/ list_divider
机器人:dividerHeight =1DP
机器人:listSelector =@绘制/ list_selector
机器人:背景=@色/ list_background/>
<! - 抽屉里面查看传呼机,所以当打开它显示 - >
<的LinearLayout
机器人:方向=垂直
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =300dp
机器人:ID =@ + ID / viewPageLinear>
< android.support.v4.view.ViewPager
机器人:ID =@ + ID / random_pager
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =300dp> < /android.support.v4.view.ViewPager>
<按钮
机器人:layout_width =80dp
机器人:layout_height =80dp
机器人:比重=中心
机器人:ID =@ + ID /否
机器人:layout_below =@ + ID / viewPageLinear/>
<按钮
机器人:layout_width =80dp
机器人:layout_height =80dp
机器人:比重=中心
机器人:layout_below =@ + ID /否/>
< / LinearLayout中> < /android.support.v4.widget.DrawerLayout>
< / RelativeLayout的>
片段的XML加载:
<?XML版本=1.0编码=UTF-8&GT?;<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent机器人:layout_height =match_parent>
< ImageView的
机器人:layout_width =200dp
机器人:layout_height =200dp
机器人:比重=中心
机器人:layout_alignParentTop =真
机器人:ID =@ + ID / imagehere/>
< / RelativeLayout的>
我们推荐使用 FragmentStatePagerAdapter
而不是 FragmentPagerAdapter
。
请参阅的http://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html以供参考。
当有大量这种寻呼机的版本是更有益
页面,变得更像一个列表视图。
块引用>除此之外,它不是很清楚我的问题(或问题)是什么。
I understand that a viewpager can be used, but I am having a hard time finding a good resource explaining how to use a viewpager with the following additions.
The view to record if the swipe was left or right, and either way just reload a new content (if left I want it to record no, right for it to record yes).
I want it to be infinite or really large, like 10000.
Finally, be able to add buttons underneath it as well.
In summary any direction, tutorials, or examples would be appreciated.
- no tabs - just avoided implementing a tab listener and including tabs when creating the view pager
- read swipes
- load new content
- infinite or really large
- have buttons underneath it - done by including it in the drawer layout
Hopefully this clears things up:
UPDATE: Here is the scenario I am trying to read swipes - if left record no and if right record yes, then grab new content from cache and display it, with the same two swipe options, I was thinking maybe I can just have 3 "pages" and swiping left records the action then it resets to position (middle - 1) and then loads the next content available in the cache... the only thing is I do not know how to call the reset and ask for a refresh of the content...
EDIT:
No tabs has been solved by just adding the code like so:
//viewpager adapter private PageAdapter mAdapter; private ViewPager viewPager; // Initializing pager - in On Create viewPager = (ViewPager) findViewById(R.id.random_pager); FragmentManager fm = getFragmentManager(); mAdapter = new PageAdapter(getSupportFragmentManager(), new UserCreatedFragment(), new UserUpVotesFragment(),new UserDownVotesFragment()); viewPager.setAdapter(mAdapter); // Here you would declare which page to visit on creation viewPager.setCurrentItem(1);
This is the adapter:
public class PageAdapter extends FragmentPagerAdapter{ private Fragment frag1; private Fragment frag2; private Fragment frag3; public PageAdapter(FragmentManager fm, Fragment frag1, Fragment frag2, Fragment frag3) { super(fm); this.frag1 = frag1; this.frag2 = frag2; this.frag3 = frag3; } @Override public Fragment getItem(int index) { switch (index) { case 0: //fragments should record no and reset to the same frag with new content back in position 1 return frag2; case 1: //fragments for activity return frag2; case 2: //fragments should record yes and reset to the same frag with new content back in position 1 return frag2; } return null; } //return count for number of tabs/swipes @Override public int getCount() { return 3; } }
xml of activity:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- Framelayout to display Fragments --> <FrameLayout android:id="@+id/frame_container" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- Listview to display slider menu --> <ListView android:id="@+id/list_slidermenu" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:choiceMode="singleChoice" android:divider="@color/list_divider" android:dividerHeight="1dp" android:listSelector="@drawable/list_selector" android:background="@color/list_background"/> <!--View pager inside drawer so when opens it displays --> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="300dp" android:id="@+id/viewPageLinear"> <android.support.v4.view.ViewPager android:id="@+id/random_pager" android:layout_width="wrap_content" android:layout_height="300dp"> </android.support.v4.view.ViewPager> <Button android:layout_width="80dp" android:layout_height="80dp" android:gravity="center" android:id="@+id/no" android:layout_below="@+id/viewPageLinear"/> <Button android:layout_width="80dp" android:layout_height="80dp" android:gravity="center" android:layout_below="@+id/no"/> </LinearLayout> </android.support.v4.widget.DrawerLayout> </RelativeLayout>
xml of fragment loaded:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="200dp" android:layout_height="200dp" android:gravity="center" android:layout_alignParentTop="true" android:id="@+id/imagehere"/> </RelativeLayout>
解决方案It's recommended to use
FragmentStatePagerAdapter
instead ofFragmentPagerAdapter
.See http://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html for reference.
This version of the pager is more useful when there are a large number of pages, working more like a list view.
Other than that, it's not quite clear to me what the question (or the problem) is.
这篇关于如何建立一个无限的行为,并检测哪一方有人偷走一个viewpager?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!