网上有些人写的东西,我照着写,一直不能左右滑动,原来是少了一个方法:
@Override PRotected void attachBaseContext(Context newBase) { super.attachBaseContext(new ParallaxContextWrapper(newBase)); }至于原因,现在不清楚,也没时间去深究了。谢谢github的源码:https://github.com/prolificinteractive/ParallaxPagerParallaxPager实现当页面滚动切换时产生视差效果。
Download
Use Gradle to grab the dependency from Maven Central:
compile 'com.prolificinteractive:parallaxpager:2.2.1'Usage
There are 4 important steps:
Use a
ParallaxContainerin layout xmlCreate a layout XML file for each page
Wrap the Activity Context
Add the attachment code to
onCreateof your Activity oronCreateViewof your Fragment1. Use a
ParallaxContainerin layout XMLUse the class
com.prolificinteractive.parallaxpager.ParallaxContainerin your layout XML, sizing it however you like.Ex:
<com.prolificinteractive.parallaxpager.ParallaxContainer android:id="@+id/parallax_container_1" android:layout_width="match_parent" android:layout_height="match_parent"/>2. Create a layout XML file for each page
Each page must have its own layout XML file. Use whichever Layouts or Views you like, as usual.
Ensure this line is added to the Root Element:
xmlns:app="http://schemas.android.com/apk/res-auto"
Assign any combination of the following attributes (all floats):
x_in: as the View enters the screen, it will translate in the horizontal direction along with user swiping, at a rate multiplied by this value. Default is0.
x_out: as the View leaves the screen, it will translate in the horizontal direction along with user swiping, at a rate multiplied by this value. Default is0.
y_in: as the View enters the screen, it will translate downward as the user swipes right to left, at a rate multiplied by this value. Default is0.
y_out: as the View leaves the screen, it will translate upward as the user swipes right to left, at a rate multiplied by this value. Default is0.
a_in: as the View enters the screen, it will fade in as the user swipes right to left, at a rate multiplied by this value. Default is0.
a_out: as the View leaves the screen, it will fade out as the user swipes right to left, at a rate multiplied by this value. Default is0.Ex:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" app:x_in="@dimen/parallax_speed_medium" app:x_out="@dimen/parallax_speed_fast" app:y_in="@dimen/parallax_speed_medium_rev" app:y_out="@dimen/parallax_speed_fast" app:a_in="@dimen/parallax_speed_very_fast" app:a_out="@dimen/parallax_speed_very_fast" android:text="@string/text_1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" app:x_in="@dimen/parallax_speed_medium_rev" app:x_out="@dimen/parallax_speed_fast" app:y_in="@dimen/parallax_speed_medium" app:y_out="@dimen/parallax_speed_fast_rev" app:a_in="@dimen/parallax_speed_very_fast" app:a_out="@dimen/parallax_speed_very_fast" android:text="@string/text_2" /></LinearLayout>Keep in mind that negative values mean a change in direction for translation effects, and have no effect for alpha. For translation effects, values between
0and1will result in a high level of funkiness.3. Wrap the Activity Context
Wrap the activity context using
com.prolificinteractive.parallaxpager.ParallaxContextWrapperin your activity.Ex:
@Override protected void attachBaseContext(Context newBase) { super.attachBaseContext(new ParallaxContextWrapper(newBase)); }Note: If you are using this in conjunction with another library that wraps Context, it doesn't appear to like chaining them together. Instead, we've added the ability to hook into the View creation process to use with other libraries. The sample project shows how to hook into Calligraphy.
Ex:
@Override protected void attachBaseContext(Context newBase) { super.attachBaseContext( new ParallaxContextWrapper(newBase, new OnViewCreatedListener() { @Override public View onViewCreated(View view, Context context, AttributeSet attrs) { //Setup view as needed return view; //Return the view passed in } }) ); }4. Add the attachment code to
onCreateof your Activity oronCreateViewof your FragmentImportant steps in
onCreateof an Activity (oronCreateViewof a Fragment):Find the parallax container by ID
Specify whether the pager should loop (
truemeans it will loop)Submit a Layout Inflater and list the layouts for each page (in order). Currently there must be at least 2 in this list (repeats allowed).
Ex:
// find the parallax containerParallaxContainer parallaxContainer = (ParallaxContainer) findViewById(R.id.parallax_container);// specify whether pager will loopparallaxContainer.setLooping(true);// wrap the inflater and inflate children with custom attributesparallaxContainer.setupChildren(getLayoutInflater(), R.layout.parallax_view_1, R.layout.parallax_view_2, R.layout.parallax_view_3, R.layout.parallax_view_4);Extras
Extra 1. Setting a
ViewPager.OnPageChangeListenerYou can set a
ViewPager.OnPageChangeListenerafter the attachment code in step 4.// optionally set a ViewPager.OnPageChangeListenerparallaxContainer.setOnPageChangeListener(this);Extra 2.
ViewPageraccessYou have access to the
ViewPagerby calling:parallaxContainer.getViewPager();This is exposed for use with existing code which requires a
ViewPagerinstance. Please make sure that if you call methods likesetAdapterorsetOnPageChangeListeneron the instance returned, that you do so with forethought and good reason.Extra 3. Overriding parallax visibility
Parallax views will be
VISIBLEwhen onscreen, andGONEwhen offscreen. If you need to override this behavior, set this attribute on your View in XML:app:override_visibility="true"
新闻热点
疑难解答