Created
November 6, 2018 18:14
-
-
Save adeds/4b410e1c15146d9017e81642e1d0292a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context="com.example.verticalviewpager.MainActivity"> | |
<com.example.myapp.VerticalViewPager | |
android:id="@+id/viewpager" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.support.v4.app.Fragment; | |
import android.support.v4.app.FragmentManager; | |
import android.support.v4.app.FragmentPagerAdapter; | |
import android.support.v4.view.ViewPager; | |
public class MainActivity extends AppCompatActivity { | |
static final int NUMBER_OF_PAGES = 2; | |
MyAdapter mAdapter; | |
VerticalViewPager mPager; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mAdapter = new MyAdapter(getSupportFragmentManager()); | |
mPager = findViewById(R.id.viewpager); | |
mPager.setAdapter(mAdapter); | |
} | |
public static class MyAdapter extends FragmentPagerAdapter { | |
public MyAdapter(FragmentManager fm) { | |
super(fm); | |
} | |
@Override | |
public int getCount() { | |
return NUMBER_OF_PAGES; | |
} | |
@Override | |
public Fragment getItem(int position) { | |
switch (position) { | |
case 0: | |
return FragmentOne.newInstance(0, Color.WHITE); | |
case 1: | |
// return a different Fragment class here | |
// if you want want a completely different layout | |
return FragmentOne.newInstance(1, Color.CYAN); | |
default: | |
return null; | |
} | |
} | |
} | |
public static class FragmentOne extends Fragment { | |
private static final String MY_NUM_KEY = "num"; | |
private static final String MY_COLOR_KEY = "color"; | |
private int mNum; | |
private int mColor; | |
// You can modify the parameters to pass in whatever you want | |
static FragmentOne newInstance(int num, int color) { | |
FragmentOne f = new FragmentOne(); | |
Bundle args = new Bundle(); | |
args.putInt(MY_NUM_KEY, num); | |
args.putInt(MY_COLOR_KEY, color); | |
f.setArguments(args); | |
return f; | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
mNum = getArguments() != null ? getArguments().getInt(MY_NUM_KEY) : 0; | |
mColor = getArguments() != null ? getArguments().getInt(MY_COLOR_KEY) : Color.BLACK; | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
View v = inflater.inflate(R.layout.fragment_one, container, false); | |
v.setBackgroundColor(mColor); | |
TextView textView = v.findViewById(R.id.textview); | |
textView.setText("Page " + mNum); | |
return v; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://stackoverflow.com/a/46826363 | |
/* | |
* Copyright (C) 2014 The Android Open Source Project | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.android.deskclock; | |
import android.content.Context; | |
import android.support.v4.view.ViewPager; | |
import android.util.AttributeSet; | |
import android.view.MotionEvent; | |
import android.view.View; | |
public class VerticalViewPager extends ViewPager { | |
public VerticalViewPager(Context context) { | |
this(context, null); | |
} | |
public VerticalViewPager(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
/** | |
* @return {@code false} since a vertical view pager can never be scrolled horizontally | |
*/ | |
@Override | |
public boolean canScrollHorizontally(int direction) { | |
return false; | |
} | |
/** | |
* @return {@code true} iff a normal view pager would support horizontal scrolling at this time | |
*/ | |
@Override | |
public boolean canScrollVertically(int direction) { | |
return super.canScrollHorizontally(direction); | |
} | |
private void init() { | |
// Make page transit vertical | |
setPageTransformer(true, new VerticalPageTransformer()); | |
// Get rid of the overscroll drawing that happens on the left and right (the ripple) | |
setOverScrollMode(View.OVER_SCROLL_NEVER); | |
} | |
@Override | |
public boolean onInterceptTouchEvent(MotionEvent ev) { | |
final boolean toIntercept = super.onInterceptTouchEvent(flipXY(ev)); | |
// Return MotionEvent to normal | |
flipXY(ev); | |
return toIntercept; | |
} | |
@Override | |
public boolean onTouchEvent(MotionEvent ev) { | |
final boolean toHandle = super.onTouchEvent(flipXY(ev)); | |
// Return MotionEvent to normal | |
flipXY(ev); | |
return toHandle; | |
} | |
private MotionEvent flipXY(MotionEvent ev) { | |
final float width = getWidth(); | |
final float height = getHeight(); | |
final float x = (ev.getY() / height) * width; | |
final float y = (ev.getX() / width) * height; | |
ev.setLocation(x, y); | |
return ev; | |
} | |
private static final class VerticalPageTransformer implements ViewPager.PageTransformer { | |
@Override | |
public void transformPage(View view, float position) { | |
final int pageWidth = view.getWidth(); | |
final int pageHeight = view.getHeight(); | |
if (position < -1) { | |
// This page is way off-screen to the left. | |
view.setAlpha(0); | |
} else if (position <= 1) { | |
view.setAlpha(1); | |
// Counteract the default slide transition | |
view.setTranslationX(pageWidth * -position); | |
// set Y position to swipe in from top | |
float yPosition = position * pageHeight; | |
view.setTranslationY(yPosition); | |
} else { | |
// This page is way off-screen to the right. | |
view.setAlpha(0); | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment