-
-
Save devunwired/8cbe094bb7a783e37ad1 to your computer and use it in GitHub Desktop.
ViewPager container implementation that allows surrounding pages be visible (although not interactive). PagerContainer holds a ViewPager (in this case centered), has clipChildren() set to false, and has some extra code to redraw itself and forward touche
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
<!-- Copyright (c) 2012 Wireless Designs, LLC | |
Permission is hereby granted, free of charge, to any person obtaining | |
a copy of this software and associated documentation files (the | |
"Software"), to deal in the Software without restriction, including | |
without limitation the rights to use, copy, modify, merge, publish, | |
distribute, sublicense, and/or sell copies of the Software, and to | |
permit persons to whom the Software is furnished to do so, subject to | |
the following conditions: | |
The above copyright notice and this permission notice shall be | |
included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
--> | |
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<com.example.pagercontainer.PagerContainer | |
android:id="@+id/pager_container" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="#CCC"> | |
<android.support.v4.view.ViewPager | |
android:layout_width="150dp" | |
android:layout_height="100dp" | |
android:layout_gravity="center_horizontal" /> | |
</com.example.pagercontainer.PagerContainer> | |
</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
/* | |
* Copyright (c) 2012 Wireless Designs, LLC | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining | |
* a copy of this software and associated documentation files (the | |
* "Software"), to deal in the Software without restriction, including | |
* without limitation the rights to use, copy, modify, merge, publish, | |
* distribute, sublicense, and/or sell copies of the Software, and to | |
* permit persons to whom the Software is furnished to do so, subject to | |
* the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be | |
* included in all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
package com.example.pagercontainer; | |
import android.app.Activity; | |
import android.graphics.Color; | |
import android.os.Bundle; | |
import android.support.v4.view.PagerAdapter; | |
import android.support.v4.view.ViewPager; | |
import android.view.Gravity; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
/** | |
* PagerActivity: A Sample Activity for PagerContainer | |
*/ | |
public class PagerActivity extends Activity { | |
PagerContainer mContainer; | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
mContainer = (PagerContainer) findViewById(R.id.pager_container); | |
ViewPager pager = mContainer.getViewPager(); | |
PagerAdapter adapter = new MyPagerAdapter(); | |
pager.setAdapter(adapter); | |
//Necessary or the pager will only have one extra page to show | |
// make this at least however many pages you can see | |
pager.setOffscreenPageLimit(adapter.getCount()); | |
//A little space between pages | |
pager.setPageMargin(15); | |
//If hardware acceleration is enabled, you should also remove | |
// clipping on the pager for its children. | |
pager.setClipChildren(false); | |
} | |
//Nothing special about this adapter, just throwing up colored views for demo | |
private class MyPagerAdapter extends PagerAdapter { | |
@Override | |
public Object instantiateItem(ViewGroup container, int position) { | |
TextView view = new TextView(PagerActivity.this); | |
view.setText("Item "+position); | |
view.setGravity(Gravity.CENTER); | |
view.setBackgroundColor(Color.argb(255, position * 50, position * 10, position * 50)); | |
container.addView(view); | |
return view; | |
} | |
@Override | |
public void destroyItem(ViewGroup container, int position, Object object) { | |
container.removeView((View)object); | |
} | |
@Override | |
public int getCount() { | |
return 5; | |
} | |
@Override | |
public boolean isViewFromObject(View view, Object object) { | |
return (view == object); | |
} | |
} | |
} |
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
/* | |
* Copyright (c) 2012 Wireless Designs, LLC | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining | |
* a copy of this software and associated documentation files (the | |
* "Software"), to deal in the Software without restriction, including | |
* without limitation the rights to use, copy, modify, merge, publish, | |
* distribute, sublicense, and/or sell copies of the Software, and to | |
* permit persons to whom the Software is furnished to do so, subject to | |
* the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be | |
* included in all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
package com.example.pagercontainer; | |
import android.content.Context; | |
import android.graphics.Point; | |
import android.support.v4.view.ViewPager; | |
import android.util.AttributeSet; | |
import android.view.MotionEvent; | |
import android.widget.FrameLayout; | |
/** | |
* PagerContainer: A layout that displays a ViewPager with its children that are outside | |
* the typical pager bounds. | |
*/ | |
public class PagerContainer extends FrameLayout implements ViewPager.OnPageChangeListener { | |
private ViewPager mPager; | |
boolean mNeedsRedraw = false; | |
public PagerContainer(Context context) { | |
super(context); | |
init(); | |
} | |
public PagerContainer(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
public PagerContainer(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
init(); | |
} | |
private void init() { | |
//Disable clipping of children so non-selected pages are visible | |
setClipChildren(false); | |
//Child clipping doesn't work with hardware acceleration in Android 3.x/4.x | |
//You need to set this value here if using hardware acceleration in an | |
// application targeted at these releases. | |
setLayerType(View.LAYER_TYPE_SOFTWARE, null); | |
} | |
@Override | |
protected void onFinishInflate() { | |
try { | |
mPager = (ViewPager) getChildAt(0); | |
mPager.setOnPageChangeListener(this); | |
} catch (Exception e) { | |
throw new IllegalStateException("The root child of PagerContainer must be a ViewPager"); | |
} | |
} | |
public ViewPager getViewPager() { | |
return mPager; | |
} | |
private Point mCenter = new Point(); | |
private Point mInitialTouch = new Point(); | |
@Override | |
protected void onSizeChanged(int w, int h, int oldw, int oldh) { | |
mCenter.x = w / 2; | |
mCenter.y = h / 2; | |
} | |
@Override | |
public boolean onTouchEvent(MotionEvent ev) { | |
//We capture any touches not already handled by the ViewPager | |
// to implement scrolling from a touch outside the pager bounds. | |
switch (ev.getAction()) { | |
case MotionEvent.ACTION_DOWN: | |
mInitialTouch.x = (int)ev.getX(); | |
mInitialTouch.y = (int)ev.getY(); | |
default: | |
ev.offsetLocation(mCenter.x - mInitialTouch.x, mCenter.y - mInitialTouch.y); | |
break; | |
} | |
return mPager.dispatchTouchEvent(ev); | |
} | |
@Override | |
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { | |
//Force the container to redraw on scrolling. | |
//Without this the outer pages render initially and then stay static | |
if (mNeedsRedraw) invalidate(); | |
} | |
@Override | |
public void onPageSelected(int position) { } | |
@Override | |
public void onPageScrollStateChanged(int state) { | |
mNeedsRedraw = (state != ViewPager.SCROLL_STATE_IDLE); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@PrinceRK I am also wondering same. Was checking this code, and surprisingly it is not working for me at all.
@devunwired is there any updates on this code you made. Code seems workable but still it's not working in my scenario. I have
ViewPager
insideRecyclerView
. So I have to usewrap_content
forViewPager
. And have made customization inonMeasure
to get wrapped content. Then I used your piece of code for getting preview of previous and next view. Unfortunately I am not getting it. I don't know what I am missing. I will giveCustomViewPager
code over here. In that there is also vertical swipe related code, but that is what I am not using in the discussed scenario. Just wrapping the height.isWrap
is true in below codeAnd in
PagerAdapter
, I am using this piece of code to get measurement