需要viewflipper也可以在此布局中垂直滚动
问题描述:
此刻只有我的GridView
可滚动,但我需要我的ViewFlipper
也可以滚动。我需要别人帮助ViewFlipper
垂直滚动需要viewflipper也可以在此布局中垂直滚动
这里是我的布局的时刻:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_grids"
android:layout_width="match_parent"
android:overScrollMode="always"
android:layout_height="match_parent">
<ViewFlipper
android:layout_marginTop="56dp"
android:layout_width="match_parent"
android:layout_height="150dp"
android:autoStart="true"
android:id="@+id/vf1z"
android:flipInterval="3000">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:background="@drawable/front"
android:id="@+id/imageViewz"/>
</ViewFlipper>
<GridView
android:numColumns="3"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="@+id/grid"
android:layout_marginTop="208dp"/>
<include layout="@layout/search"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/include">
</include>
</RelativeLayout>
答
您可以在ScrollView
这样的所有元素添加在你的布局。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_grids"
android:layout_width="match_parent"
android:overScrollMode="always"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ViewFlipper
android:layout_marginTop="56dp"
android:layout_width="match_parent"
android:layout_height="150dp"
android:autoStart="true"
android:id="@+id/vf1z"
android:flipInterval="3000">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:background="@drawable/front"
android:id="@+id/imageViewz"
/>
</ViewFlipper>
<GridView
android:numColumns="3"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="@+id/grid"
android:layout_marginTop="208dp"
/>
</ScrollView>
<include layout="@layout/search"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/include">
</include>
</RelativeLayout>
或者,如果您只想ViewFlipper
是滚动的,你可以只添加ViewFlipper
在ScrollView
答
您可以使用VerticalViewPager它的自定义实现
/**
* Uses a combination of a PageTransformer and swapping X & Y coordinates
* of touch events to create the illusion of a vertically scrolling ViewPager.
*
* Requires API 11+
*
*/
public class VerticalViewPager extends ViewPager {
public VerticalViewPager(Context context) {
super(context);
init();
}
public VerticalViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
// The majority of the magic happens here
setPageTransformer(true, new VerticalPageTransformer());
// The easiest way to get rid of the overscroll drawing that happens on the left and right
setOverScrollMode(OVER_SCROLL_NEVER);
}
private class VerticalPageTransformer implements ViewPager.PageTransformer {
@Override
public void transformPage(View view, float position) {
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);
} else if (position <= 1) { // [-1,1]
view.setAlpha(1);
// Counteract the default slide transition
view.setTranslationX(view.getWidth() * -position);
//set Y position to swipe in from top
float yPosition = position * view.getHeight();
view.setTranslationY(yPosition);
} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
}
/**
* Swaps the X and Y coordinates of your touch event.
*/
private MotionEvent swapXY(MotionEvent ev) {
float width = getWidth();
float height = getHeight();
float newX = (ev.getY()/height) * width;
float newY = (ev.getX()/width) * height;
ev.setLocation(newX, newY);
return ev;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev){
boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
swapXY(ev); // return touch coordinates to original reference frame for any child views
return intercepted;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
return super.onTouchEvent(swapXY(ev));
}
}
见@给以前的答案Brett