设计一个HorizontalScrollView ViewPager或使用片段内: [英] Design a HorizontalScrollView inside of ViewPager or use fragments:
问题描述
我需要设计了以下画面,我需要你的建议:
I need to design the following screen, and I need your advice:
说明:
标题是静态/固定的,我也不需要用它做什么。
The title is static/fixed and I don't need to do anything with it.
黄:这是有趣的部分,我需要设计一个 ViewPager
像屏幕,有能力滚动左/右为最大4屏幕。
Yellow: this is the interesting part, I need to design a ViewPager
like screen that has the capability to scroll left/right for Max 4 screens.
红:在每一个画面,我需要添加一个表/格,可以滚动,以及如果它不适合屏幕大小
Red: In every screen I need to add a Table/Grid that can be scrollable as well if it's not fits screen size.
绿色:页面切换可以使用绿色按钮在屏幕的底部或通过滚动来完成的 ViewPager
Green: The page switching can be done using the green buttons in the bottom of the screen or by scrolling the ViewPager
.
现在的问题是:可这种行为是实现使用 ViewPager
或者我应该用片段
?如果片段是要走的路,那么我将如何实现网页使用滑动手势切换?如果它是一个 ViewPager
再如何添加里面的滚动以及如何使用这些按钮在底部来控制它?
The Question Is: Can this behavior be achieve using a ViewPager
or should I use Fragments
? If Fragment is the way to go, then how would I implement the page switching using the sliding gesture? if it's a ViewPager
then how to add the inside scrolling and how to control it using the buttons at the bottom?
任何帮助将是AP preciated,谢谢你。
Any help would be appreciated, Thanks.
推荐答案
我想这应该与非Swipeable 的 ViewPager
加以解决。没有办法的看法寻呼机的和的底层片段
取值应到刷卡的手势回应。该方法重写以禁用内刷卡 ViewPager
是:
I think this should be tackled with a Non Swipeable ViewPager
. There is no way the view pager and the underlying Fragment
s should respond to the swiping gesture. The methods to override to disable swiping within the ViewPager
are:
- <一个href="http://developer.android.com/reference/android/support/v4/view/ViewPager.html#onTouchEvent%28android.view.MotionEvent%29"相对=nofollow>
的onTouchEvent()
- 返回假
- <一个href="http://developer.android.com/reference/android/support/v4/view/ViewPager.html#onInterceptTouchEvent%28android.view.MotionEvent%29"相对=nofollow>
onInterceptTouchEvent()
- 返回假
onTouchEvent()
- returnsfalse
.onInterceptTouchEvent()
- returnsfalse
.
请参照<一个href="http://stackoverflow.com/questions/9650265/how-do-disable-paging-by-swiping-with-finger-in-viewpager-but-still-be-able-to-s">this SO质疑关于如何实现这一目标的详细信息。
Refer to this SO question for more information on how to achieve this.
接下来要使用片段
秒内每一个传呼机持有人。因此,我们正在构建的以下布局:
Next up you want to be using Fragment
s within each of your pager holders. So we're building the following layout:
在父活动一 FragmentPagerAdapter
被实例化,你的标签添加标签:
Within the parent activity a FragmentPagerAdapter
is instantiated and your tabs added with a tag:
@Override
protected void onCreate(final Bundle saveInstanceState) {
final FragmentPagerAdapter myTabAdapter = new MyFragmentPagerAdapter(
<Your ViewPager View>, <Your activity context, this>);
myTabAdapter.addTab(getActionBar().newTab(), "YOUR TAG", "Your Title");
// etc...
}
因此,这为我们提供了上图中的帧。一个托管的活动,包含 ViewPager
和底层标签。接下来是获得片段
S(包含您的表)到每个相应的选项卡。这是由 FragmentPagerAdapter
实施处理:
So this gives us the frame of the diagram above. A hosting activity, containing a ViewPager
and the underlying tabs. Next up is getting the Fragment
s (containing your tables) into each of the respective tabs. This is handled by the FragmentPagerAdapter
implementation:
private class MyFragmentPagerAdapter extends FragmentPagerAdapter implements
ActionBar.TabListener, ViewPager.OnPageChangeListener {
/**
* Constructs a pager adapter to back a {@link ViewPager}.
*
* @param pager
* The {@link ViewPager} widget.
* @param activityContext
* The context the widget is being added under.
*/
public SpotMenuFragmentPagerAdapter(final ViewPager pager,
final Context activityContext) {
super(getFragmentManager());
pager.setAdapter(this);
this.context = activityContext;
}
/**
* Adds a tab to the hosting activity action bar.
*
* @param newTab
* The tab to add.
* @param tag
* The tab tag for id purposes.
* @param label
* The label of the tab displayed to the user.
*/
public void addTab(final ActionBar.Tab newTab, final String tag,
final String label) {
newTab.setTag(tag);
newTab.setText(label);
newTab.setTabListener(this);
getSupportActionBar().addTab(newTab);
}
/**
* This is where you do the work of building the correct fragment based
* on the tab currently selected.
*
* @see FragmentPagerAdapter#getItem(int)
*/
@Override
public Fragment getItem(final int position) {
final Tab tab = getActionBar().getTabAt(position);
if ("MY TAG".equals(tab.getTag().toString()) {
// instantiate the fragment (table) for "MY TAG"
} else {
// instantiate something else...
}
}
/**
* One fragment per tab.
*
* @see android.support.v4.view.PagerAdapter#getCount()
*/
@Override
public int getCount() {
return getSupportActionBar().getTabCount();
}
/**
* @see ViewPager.OnPageChangeListener#onPageScrollStateChanged(int)
*/
@Override
public void onPageScrollStateChanged(final int arg0) {
// No-op.
}
/**
* @see ViewPager.OnPageChangeListener#onPageScrolled(int, float, int)
*/
@Override
public void onPageScrolled(final int arg0, final float arg1,
final int arg2) {
// No-op.
}
/**
* @see ViewPager.OnPageChangeListener#onPageSelected(int)
*/
@Override
public void onPageSelected(final int position) {
getSupportActionBar().setSelectedNavigationItem(position);
}
/**
* @see TabListener#onTabSelected(app.ActionBar.Tab,
* app.FragmentTransaction)
*/
@Override
public void onTabSelected(final Tab tab, final FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
/**
* @see TabListener#onTabUnselected(ActionBar.Tab,
* app.FragmentTransaction)
*/
@Override
public void onTabUnselected(final Tab tab, final FragmentTransaction ft) {
// No-op.
}
/**
* @see TabListener#onTabReselected(ActionBar.Tab,app.FragmentTransaction)
*/
@Override
public void onTabReselected(final Tab tab, final FragmentTransaction ft) {
// No-op.
}
}
所以希望通过这一点,我们有举办一个非swipeable的观点寻呼机和机制,切换标签在标题下方的标签栏的形式(或旁边取决于屏幕大小)的活动。从这个角度,我相信你可以自定义一些导航箭头代替标签栏。
So hopefully by this point we have an activity hosting a 'non-swipeable' view pager and a mechanism for switching tabs in the form of the tab bar underneath the title (or alongside depending on the screen size). From this point I am sure you could customise to replace the tab bar with some navigational arrows.
注意:的大量的已写入从内存中,但希望我已经传达了,我会用这个去的要点
Note: A lot of that was written from memory but hopefully I've conveyed the gist of where I would go with this.
在响应于更新的问题:可以设置的标签是任何旧图。设置则tabspec 相应。道歉我还没有使用这个自己。
In response to the updated question: you can set the tab to be any old view. Set the TabSpec accordingly. Apologies I haven't used this myself.
这篇关于设计一个HorizontalScrollView ViewPager或使用片段内:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!