android viewpager realizes vertical sliding effect
- 2021-09-20 21:23:29
- OfStack
Friends who have done android development must know what viewpager is, but viewpager can only slide horizontally. Now, some project guide pages also start to use vertical sliding. At this time, viewpager can't help us. What should I do? Only custom, today will briefly talk about the realization of viewpager vertical sliding, but this is to tell you how to realize it, and it must not be used in real projects, because there are still some details that have not been dealt with, just to tell you a train of thought!
First, create a new android project called Customviewpager
We also know that viewpager also inherits ViewGroup class, and here we write an CustomViewPager class that also inherits ViewGroup.
In fact, there are very few codes. First post the code and then explain:
CustomViewPager.java
public class CustomViewPager extends ViewGroup {
private static final String TAG = CustomViewPager.class.getSimpleName();
private float startX = 0;
private GestureDetector detector;
int a = 30;
public CustomViewPager(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
detector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener(){
/**
* distanceX The distance to move on the screen Instead of coordinates
*/
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
scrollBy(0, (int)distanceY);
return true;
}
});
}
public CustomViewPager(Context context) {
super(context);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
for(int i=0;i<getChildCount();i++){
View childView = getChildAt(i);
// childView.layout(i*getWidth(), 0, (i+1)*getWidth(), getHeight()); , // This is horizontal sliding
childView.layout(0, i*getHeight(), getWidth(), (i+1)*getHeight());// This is vertical sliding
}
}
/**
* This default super.onTouchEvent(event) For false
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
detector.onTouchEvent(event);
return true;
}
}
MainActivity.java
public class MainActivity extends ActionBarActivity {
private CustomViewPager custom_view_pager;
private int[] ids = {R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5,R.drawable.a6};
private List<ImageView> imageViews;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayUtil.init(this);
custom_view_pager = (CustomViewPager) findViewById(R.id.custom_view_pager);
initData();
}
private void initData() {
imageViews = new ArrayList<>();
for(int i=0;i<ids.length;i++){
ImageView imageView = new ImageView(this);
imageView.setBackgroundResource(ids[i]);
imageViews.add(imageView);
custom_view_pager.addView(imageView);
}
}
}
That's it, and then paste the layout file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<zhi.more.customviewpager.view.CustomViewPager
android:id="@+id/custom_view_pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
Note here that the method onTouchEvent () returns a value. What's the difference between returning true and false. To put it simply
Returning true indicates that you handled this sliding event. Returning false means passing it to the child view, but the current parent view no longer holds this sliding event. To understand this problem well, it is designed to pass the view event. You can return different values under down, move and up and call log to analyze the principle, which will not be explained here.