Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

更改viewpager滑动时间。完成翻页动画。 #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
package com.eajy.materialdesigndemo.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Interpolator;

import com.eajy.materialdesigndemo.R;
import com.eajy.materialdesigndemo.view.BottomNavigationViewHelper;
import com.eajy.materialdesigndemo.view.BottomNavigationPageTransformer;
import com.eajy.materialdesigndemo.view.BottomNavigationViewHelper;
import com.eajy.materialdesigndemo.view.FixedScroller;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class BottomNavigationActivity extends AppCompatActivity {
public class BottomNavigationActivity extends AppCompatActivity
{

private ViewPager viewPager;
private BottomNavigationView navigation;
private List<View> viewList;

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

Expand All @@ -40,7 +46,8 @@ protected void onCreate(Bundle savedInstanceState) {
initView();
}

private void initView() {
private void initView()
{
View view1 = getLayoutInflater().inflate(R.layout.item_view_pager_1, null);
View view2 = getLayoutInflater().inflate(R.layout.item_view_pager_2, null);
View view3 = getLayoutInflater().inflate(R.layout.item_view_pager_3, null);
Expand All @@ -53,6 +60,7 @@ private void initView() {
viewList.add(view4);

viewPager = findViewById(R.id.view_pager_bottom_navigation);
changViewpagerTime();
viewPager.setAdapter(pagerAdapter);
viewPager.addOnPageChangeListener(pageChangeListener);
viewPager.setPageTransformer(true, new BottomNavigationPageTransformer());
Expand All @@ -64,15 +72,41 @@ private void initView() {
BottomNavigationViewHelper.disableShiftMode(navigation);
}

private ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
/**
* 改变viewpager自动播放的滑动时间为500ms 默认是250ms
*/
private void changViewpagerTime()
{
try
{
Field mScroller;
mScroller = ViewPager.class.getDeclaredField("mScroller");
mScroller.setAccessible(true);
Interpolator sInterpolator = new AccelerateDecelerateInterpolator();
FixedScroller scroller = new FixedScroller(viewPager.getContext(), sInterpolator);
mScroller.set(viewPager, scroller);
} catch (NoSuchFieldException e)
{
} catch (IllegalArgumentException e)
{
} catch (IllegalAccessException e)
{
}
}

private ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener()
{
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{

}

@Override
public void onPageSelected(int position) {
switch (position) {
public void onPageSelected(int position)
{
switch (position)
{
case 0:
navigation.setSelectedItemId(R.id.bottom_navigation_blue);
break;
Expand All @@ -89,15 +123,19 @@ public void onPageSelected(int position) {
}

@Override
public void onPageScrollStateChanged(int state) {
public void onPageScrollStateChanged(int state)
{

}
};

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener()
{
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
public boolean onNavigationItemSelected(@NonNull MenuItem item)
{
switch (item.getItemId())
{
case R.id.bottom_navigation_blue:
viewPager.setCurrentItem(0);
return true;
Expand All @@ -115,24 +153,29 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) {
}
};

private PagerAdapter pagerAdapter = new PagerAdapter() {
private PagerAdapter pagerAdapter = new PagerAdapter()
{
@Override
public int getCount() {
public int getCount()
{
return viewList.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
public boolean isViewFromObject(View view, Object object)
{
return view == object;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
public void destroyItem(ViewGroup container, int position, Object object)
{
container.removeView(viewList.get(position));
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
public Object instantiateItem(ViewGroup container, int position)
{
container.addView(viewList.get(position));
return viewList.get(position);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@
* Created by ANTVR-24 on 17/04/06.
*/

public class BottomNavigationPageTransformer implements ViewPager.PageTransformer {
public class BottomNavigationPageTransformer implements ViewPager.PageTransformer
{
@Override
public void transformPage(View view, float position) {

if (position < 0) {
public void transformPage(View view, float position)
{
if (position < 0)
{
// view.setPivotX(view.getMeasuredWidth());
// view.setPivotY(view.getMeasuredHeight() * 0.5f);
// view.setScaleY(1 + (float) (position * 0.2));
// view.setRotationY(20f * position);
view.setAlpha(position + 1);

} else if (position < 1) {
} else if (position < 1)
{
view.setTranslationX(view.getMeasuredWidth() * -position);
// view.setPivotX(0);
// view.setPivotY(view.getMeasuredHeight() * 0.5f);
// view.setRotationY(20f * position);
// view.setAlpha(1 - position);
view.setPivotX(0);
view.setPivotY(view.getMeasuredHeight() * 0.5f);
view.setRotationY(20f * position);
view.setAlpha(1 - position);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.eajy.materialdesigndemo.view;

import android.content.Context;
import android.view.animation.Interpolator;
import android.widget.Scroller;

/**
* @author xiaokun
* @date 2017/12/6
*/

public class FixedScroller extends Scroller
{
private int mDuration = 500;

public FixedScroller(Context context)
{
super(context);
}

public FixedScroller(Context context, Interpolator interpolator)
{
super(context, interpolator);
}

@Override
public void startScroll(int startX, int startY, int dx, int dy, int duration)
{
// Ignore received duration, use fixed one instead
super.startScroll(startX, startY, dx, dy, mDuration);
}

@Override
public void startScroll(int startX, int startY, int dx, int dy)
{
// Ignore received duration, use fixed one instead
super.startScroll(startX, startY, dx, dy, mDuration);
}
}