我想创建一个水平可滚动的ViewPager。 我的ViewPager包含碎片。每个片段的宽度都大于屏幕,因此必须启用滚动。
我对我的片段项使用以下布局:
<HorizontalScrollView
android:id="@+id/view_horizontal_scroll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isScrollContainer="true" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<View
android:layout_width="100dp"
android:layout_height="fill_parent"
android:background="#AABBCC" />
<View
android:id="@+id/test"
android:layout_width="1500dp"
android:layout_height="fill_parent" />
<View
android:layout_width="100dp"
android:layout_height="fill_parent"
android:background="#ccBBaa" />
</LinearLayout>
</HorizontalScrollView>
我还创建了一个允许水平滚动的自定义ViewPager:
public class ScrollableViewPager extends ViewPager {
private GestureDetector mGestureDetector;
public ScrollableViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
mGestureDetector = new GestureDetector(getContext(),
new YScrollDetector());
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// Tell our parent to stop intercepting our events!
getParent().requestDisallowInterceptTouchEvent(true);
return super.onInterceptTouchEvent(ev)
&& mGestureDetector.onTouchEvent(ev);
}
}
YScrollDetector仅覆盖onScrollMethod。到现在为止还挺好。水平滚动工作正常。
但现在我遇到了问题:
假设我有一个带有5个片段的ViewPager,([0],[1],[2],[3],[4])并且想要从项目[2]开始。当我向后移动时,我希望前一项[1]的滚动位置在最大值上而不是0。
我的Framgents看起来像这样:
public class AbstractFragment extends Fragment {
private static final String _ID = "ID";
private int _id;
private ScrollableViewPager mPager;
public AbstractFragment() {
}
public AbstractFragment(int id, int colorId) {
this._id = id;
}
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d(getClass().getSimpleName(), "[" + _id + "]: onCreate");
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
_id = savedInstanceState.getInt(_ID);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.fragment_menu_container,
container, false);
setUpScrollView(fragmentView);
return fragmentView;
}
@Override
public void onResume() {
Log.d(getClass().getSimpleName(), "[" + _id + "]: onResume");
setUpScrollView(getView());
super.onResume();
}
private void setUpScrollView(View fragmentView) {
if (fragmentView == null) {
return;
}
HorizontalScrollView scrollView = (HorizontalScrollView) fragmentView
.findViewById(R.id.view_horizontal_scroll);
if (scrollView != null) {
int currentItem = getCurrentPagerItem();
if (_id < currentItem) {
// previous Item : X Scroll: width of content
View content = scrollView.getChildAt(0);
int width = content.getRight();
width = width - scrollView.getRight();
scrollView.scrollTo(width, 0);
Log.d(getClass().getSimpleName(), "[" + _id + "] width: "
+ width);
// scrollView.fullScroll(View.FOCUS_RIGHT);
} else if (_id > currentItem) {
// next Item - X Scroll: 0
scrollView.fullScroll(View.FOCUS_LEFT);
} else {
// current Item - Nothing to do her
}
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (outState == null) {
return;
}
outState.putInt(_ID, _id);
}
private ScrollableViewPager getPager() {
PagerActivity activity = (PagerActivity) getActivity();
return activity.getViewPager();
}
private int getCurrentPagerItem() {
if (mPager == null) {
mPager = getPager();
}
return mPager.getCurrentItem();
}
}
在我的片段中,我想设置前一个片段的滚动位置。但这不起作用,因为ScrollView和ContentView的宽度都是0。
有人知道答案吗? 感谢!!!
答案 0 :(得分:2)
我自己找到了解决方案:
private void setUpScrollView(final ViewGroup fragmentView) {
if (fragmentView == null) {
return;
}
fragmentView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
HorizontalScrollView scrollView = (HorizontalScrollView) fragmentView
.findViewById(R.id.view_horizontal_scroll);
if (scrollView != null) {
int currentItem = getCurrentPagerItem();
if (_id < currentItem) {
// previous Item
View content = scrollView.getChildAt(0);
int width = content.getWidth();
width = width - scrollView.getWidth();
scrollView.scrollTo(width, 0);
} else if (_id > currentItem) {
// next Item
scrollView.scrollTo(0, 0);
} else {
// current Item
}
}
}
});
}