Skip to content

Commit

Permalink
add LoopRecyclerViewPager demo
Browse files Browse the repository at this point in the history
  • Loading branch information
lsjwzh committed Dec 12, 2015
1 parent 3a72ab7 commit 93c3b3f
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 9 deletions.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<activity
android:name=".VerticalPagerActivity" >
</activity>
<activity
android:name=".LoopPagerActivity" >
</activity>
<activity
android:name=".MaterialDemoActivity"
android:theme="@style/Theme.DesignDemo">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.lsjwzh.widget.recyclerviewpagerdeomo;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.lsjwzh.widget.recyclerviewpager.LoopRecyclerViewPager;

public class LoopPagerActivity extends Activity {
protected LoopRecyclerViewPager mRecyclerViewPager;
private EditText mTargetPosition;
private Button mScrollToPosition;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo_loopviewpager);
initViewPager();
}

protected void initViewPager() {
mRecyclerViewPager = (LoopRecyclerViewPager) findViewById(R.id.viewpager);
mTargetPosition = (EditText) findViewById(R.id.target_position);
mScrollToPosition = (Button) findViewById(R.id.scroll_to_target_position);
mScrollToPosition.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = mTargetPosition.getText() != null ? Integer.valueOf(mTargetPosition
.getText()
.toString()) : 0;
mRecyclerViewPager.smoothScrollToPosition(position);
}
});

LinearLayoutManager layout = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,
false);
mRecyclerViewPager.setTriggerOffset(0.15f);
mRecyclerViewPager.setFlingFactor(0.25f);
mRecyclerViewPager.setLayoutManager(layout);
mRecyclerViewPager.setAdapter(new LayoutAdapter(this, mRecyclerViewPager));
mRecyclerViewPager.setHasFixedSize(true);
mRecyclerViewPager.setLongClickable(true);

mRecyclerViewPager.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int scrollState) {

}

@Override
public void onScrolled(RecyclerView recyclerView, int i, int i2) {
// mPositionText.setText("First: " + mRecyclerViewPager.getFirstVisiblePosition());
int childCount = mRecyclerViewPager.getChildCount();
int width = mRecyclerViewPager.getChildAt(0).getWidth();
int padding = (mRecyclerViewPager.getWidth() - width) / 2;

for (int j = 0; j < childCount; j++) {
View v = recyclerView.getChildAt(j);
//往左 从 padding 到 -(v.getWidth()-padding) 的过程中,由大到小
float rate = 0;
if (v.getLeft() <= padding) {
if (v.getLeft() >= padding - v.getWidth()) {
rate = (padding - v.getLeft()) * 1f / v.getWidth();
} else {
rate = 1;
}
v.setScaleY(1 - rate * 0.1f);
} else {
//往右 从 padding 到 recyclerView.getWidth()-padding 的过程中,由大到小
if (v.getLeft() <= recyclerView.getWidth() - padding) {
rate = (recyclerView.getWidth() - padding - v.getLeft()) * 1f / v.getWidth();
}
v.setScaleY(0.9f + rate * 0.1f);
}
}
}
});

mRecyclerViewPager.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
if (mRecyclerViewPager.getChildCount() < 3) {
if (mRecyclerViewPager.getChildAt(1) != null) {
View v1 = mRecyclerViewPager.getChildAt(1);
v1.setScaleY(0.9f);
}
} else {
if (mRecyclerViewPager.getChildAt(0) != null) {
View v0 = mRecyclerViewPager.getChildAt(0);
v0.setScaleY(0.9f);
}
if (mRecyclerViewPager.getChildAt(2) != null) {
View v2 = mRecyclerViewPager.getChildAt(2);
v2.setScaleY(0.9f);
}
}

}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,18 @@ void onClick() {
startActivity(new Intent(MainActivity.this, MaterialDemoActivity.class));
}
});
mDemoListAdapter.add(new DemoItem("Vertical ViewPager Demo(TODO)") {
mDemoListAdapter.add(new DemoItem("Vertical ViewPager Demo") {
@Override
void onClick() {
startActivity(new Intent(MainActivity.this, VerticalPagerActivity.class));
}
});
mDemoListAdapter.add(new DemoItem("Loop ViewPager Demo") {
@Override
void onClick() {
startActivity(new Intent(MainActivity.this, LoopPagerActivity.class));
}
});
mDemoListAdapter.add(new DemoItem("3D effect Demo(TODO)") {
@Override
void onClick() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void onScrollStateChanged(RecyclerView recyclerView, int scrollState) {

@Override
public void onScrolled(RecyclerView recyclerView, int i, int i2) {
// mPositionText.setText("First: " + mRecyclerView.getFirstVisiblePosition());
// mPositionText.setText("First: " + mRecyclerViewPager.getFirstVisiblePosition());
int childCount = mRecyclerView.getChildCount();
int width = mRecyclerView.getChildAt(0).getWidth();
int padding = (mRecyclerView.getWidth() - width) / 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void onViewCreated(View view, Bundle savedInstanceState) {
mToast = Toast.makeText(activity, "", Toast.LENGTH_SHORT);
mToast.setGravity(Gravity.CENTER, 0, 0);

mRecyclerView = (RecyclerViewPager) view.findViewById(R.id.list);
mRecyclerView = (RecyclerViewPager) view.findViewById(R.id.viewpager);

LinearLayoutManager layout = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
mRecyclerView.setTriggerOffset(0.15f);
Expand All @@ -81,7 +81,7 @@ public void onScrollStateChanged(RecyclerView recyclerView, int scrollState) {

@Override
public void onScrolled(RecyclerView recyclerView, int i, int i2) {
// mPositionText.setText("First: " + mRecyclerView.getFirstVisiblePosition());
// mPositionText.setText("First: " + mRecyclerViewPager.getFirstVisiblePosition());
int childCount = mRecyclerView.getChildCount();
int width = mRecyclerView.getChildAt(0).getWidth();
int padding = (mRecyclerView.getWidth() - width) / 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected void onCreate(Bundle savedInstanceState) {
}

protected void initViewPager() {
mRecyclerView = (RecyclerViewPager) findViewById(R.id.list);
mRecyclerView = (RecyclerViewPager) findViewById(R.id.viewpager);

LinearLayoutManager layout = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,
false);
Expand All @@ -38,7 +38,7 @@ public void onScrollStateChanged(RecyclerView recyclerView, int scrollState) {

@Override
public void onScrolled(RecyclerView recyclerView, int i, int i2) {
// mPositionText.setText("First: " + mRecyclerView.getFirstVisiblePosition());
// mPositionText.setText("First: " + mRecyclerViewPager.getFirstVisiblePosition());
int childCount = mRecyclerView.getChildCount();
int width = mRecyclerView.getChildAt(0).getWidth();
int padding = (mRecyclerView.getWidth() - width) / 2;
Expand Down
28 changes: 28 additions & 0 deletions app/src/main/res/layout/demo_loopviewpager.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/target_position"
android:hint="Target Position"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="40dp"/>

<Button
android:id="@+id/scroll_to_target_position"
android:text="Scroll To Target Position"
android:layout_width="match_parent"
android:layout_height="50dp"/>

<com.lsjwzh.widget.recyclerviewpager.LoopRecyclerViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:clipToPadding="false"
android:paddingTop="15dp"
android:paddingBottom="15dp"/>
</LinearLayout>
2 changes: 1 addition & 1 deletion app/src/main/res/layout/demo_vertical_viewpager.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
android:layout_height="match_parent">

<com.lsjwzh.widget.recyclerviewpager.RecyclerViewPager
android:id="@+id/list"
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/layout_horizontal.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
android:layout_height="match_parent">
<com.lsjwzh.widget.recyclerviewpager.RecyclerViewPager
android:layout_marginTop="20dp"
android:id="@+id/list"
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="30dp"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/layout_viewpager.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
android:layout_height="match_parent">

<android.support.v4.view.ViewPager
android:id="@+id/list"
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
Expand Down

0 comments on commit 93c3b3f

Please sign in to comment.