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的循环滚动以及利用clipToPadding属性解决触及屏幕边缘时不能滑动问题 #8

Merged
merged 1 commit into from
Aug 13, 2017
Merged
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
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".CircleViewPagerActivity"/>
<activity android:name=".StandardViewPagerActivity"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package com.zhy.magicviewpager.sample;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.zhy.magicviewpager.transformer.AlphaPageTransformer;
import com.zhy.magicviewpager.transformer.NonPageTransformer;
import com.zhy.magicviewpager.transformer.RotateDownPageTransformer;
import com.zhy.magicviewpager.transformer.RotateUpPageTransformer;
import com.zhy.magicviewpager.transformer.RotateYTransformer;
import com.zhy.magicviewpager.transformer.ScaleInTransformer;

public class CircleViewPagerActivity extends AppCompatActivity{

private ViewPager mViewPager;
private PagerAdapter mAdapter;

int[] imgRes = {R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d,
R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h, R.drawable.i};

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

mViewPager = (ViewPager) findViewById(R.id.id_viewpager);

mViewPager.setPageMargin(40);
mViewPager.setAdapter(mAdapter = new PagerAdapter()
{
@Override
public Object instantiateItem(ViewGroup container, int position)
{
ImageView view = new ImageView(CircleViewPagerActivity.this);
// ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
// view.setLayoutParams(lp);
// view.setText(position + ":" + view);
view.setScaleType(ImageView.ScaleType.FIT_XY);
// view.setBackgroundColor(Color.parseColor("#44ff0000"));
final int realPosition = getRealPosition(position);
view.setImageResource(imgRes[realPosition]);
container.addView(view);
// view.setAdjustViewBounds(true);

view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(CircleViewPagerActivity.this, "click position= " + realPosition, Toast.LENGTH_SHORT).show();
}
});
return view;
}


@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}

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

@Override
public int getCount()
{
return Integer.MAX_VALUE;
}

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

@Override
public void startUpdate(ViewGroup container) {
super.startUpdate(container);
ViewPager viewPager = (ViewPager) container;
int position = viewPager.getCurrentItem();
if (position == 0) {
position = getFirstItemPosition();
} else if (position == getCount() - 1) {
position = getLastItemPosition();
}
viewPager.setCurrentItem(position, false);

}

private int getRealCount() {
return imgRes.length;
}

private int getRealPosition(int position) {
return position % getRealCount();
}

private int getFirstItemPosition() {
return Integer.MAX_VALUE / getRealCount() / 2 * getRealCount();
}

private int getLastItemPosition() {
return Integer.MAX_VALUE / getRealCount() / 2 * getRealCount() - 1;
}
});

mViewPager.setPageTransformer(true, new AlphaPageTransformer());

}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
String[] effects = this.getResources().getStringArray(R.array.magic_effect);
for (String effect : effects)
menu.add(effect);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
String title = item.getTitle().toString();
mViewPager.setAdapter(mAdapter);

if ("RotateDown".equals(title))
{
mViewPager.setPageTransformer(true, new RotateDownPageTransformer());
} else if ("RotateUp".equals(title))
{
mViewPager.setPageTransformer(true, new RotateUpPageTransformer());
} else if ("RotateY".equals(title))
{
mViewPager.setPageTransformer(true, new RotateYTransformer(45));
} else if ("Standard".equals(title))
{
// mViewPager.setClipChildren(false);
mViewPager.setPageTransformer(true, NonPageTransformer.INSTANCE);
} else if ("Alpha".equals(title))
{
// mViewPager.setClipChildren(false);
mViewPager.setPageTransformer(true, new AlphaPageTransformer());
} else if ("ScaleIn".equals(title))
{
mViewPager.setPageTransformer(true, new ScaleInTransformer());
} else if ("RotateDown and Alpha".equals(title))
{
mViewPager.setPageTransformer(true, new RotateDownPageTransformer(new AlphaPageTransformer()));
}else if ("RotateDown and Alpha And ScaleIn".equals(title))
{
mViewPager.setPageTransformer(true, new RotateDownPageTransformer(new AlphaPageTransformer(new ScaleInTransformer())));
}

setTitle(title);

return true;
}
}
119 changes: 10 additions & 109 deletions app/src/main/java/com/zhy/magicviewpager/sample/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,125 +1,26 @@
package com.zhy.magicviewpager.sample;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.zhy.magicviewpager.transformer.AlphaPageTransformer;
import com.zhy.magicviewpager.transformer.NonPageTransformer;
import com.zhy.magicviewpager.transformer.RotateDownPageTransformer;
import com.zhy.magicviewpager.transformer.RotateUpPageTransformer;
import com.zhy.magicviewpager.transformer.RotateYTransformer;
import com.zhy.magicviewpager.transformer.ScaleInTransformer;

public class MainActivity extends AppCompatActivity
{

private ViewPager mViewPager;
private PagerAdapter mAdapter;

int[] imgRes = {R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d,
R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h, R.drawable.i};
public class MainActivity extends AppCompatActivity {

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

mViewPager = (ViewPager) findViewById(R.id.id_viewpager);

mViewPager.setPageMargin(40);
mViewPager.setOffscreenPageLimit(3);
mViewPager.setAdapter(mAdapter = new PagerAdapter()
{
@Override
public Object instantiateItem(ViewGroup container, int position)
{
ImageView view = new ImageView(MainActivity.this);
// ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
// view.setLayoutParams(lp);
// view.setText(position + ":" + view);
view.setScaleType(ImageView.ScaleType.FIT_XY);
// view.setBackgroundColor(Color.parseColor("#44ff0000"));
view.setImageResource(imgRes[position]);
container.addView(view);
// view.setAdjustViewBounds(true);
return view;
}

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

@Override
public int getCount()
{
return imgRes.length;
}

@Override
public boolean isViewFromObject(View view, Object o)
{
return view == o;
}
});
mViewPager.setPageTransformer(true, new AlphaPageTransformer());

}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
String[] effects = this.getResources().getStringArray(R.array.magic_effect);
for (String effect : effects)
menu.add(effect);
return true;
public void onStandardViewPager(View view) {
Intent intent = new Intent(MainActivity.this, StandardViewPagerActivity.class);
startActivity(intent);
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
String title = item.getTitle().toString();
mViewPager.setAdapter(mAdapter);

if ("RotateDown".equals(title))
{
mViewPager.setPageTransformer(true, new RotateDownPageTransformer());
} else if ("RotateUp".equals(title))
{
mViewPager.setPageTransformer(true, new RotateUpPageTransformer());
} else if ("RotateY".equals(title))
{
mViewPager.setPageTransformer(true, new RotateYTransformer(45));
} else if ("Standard".equals(title))
{
// mViewPager.setClipChildren(false);
mViewPager.setPageTransformer(true, NonPageTransformer.INSTANCE);
} else if ("Alpha".equals(title))
{
// mViewPager.setClipChildren(false);
mViewPager.setPageTransformer(true, new AlphaPageTransformer());
} else if ("ScaleIn".equals(title))
{
mViewPager.setPageTransformer(true, new ScaleInTransformer());
} else if ("RotateDown and Alpha".equals(title))
{
mViewPager.setPageTransformer(true, new RotateDownPageTransformer(new AlphaPageTransformer()));
}else if ("RotateDown and Alpha And ScaleIn".equals(title))
{
mViewPager.setPageTransformer(true, new RotateDownPageTransformer(new AlphaPageTransformer(new ScaleInTransformer())));
}

setTitle(title);

return true;
public void onCircleViewPager(View view) {
Intent intent = new Intent(MainActivity.this, CircleViewPagerActivity.class);
startActivity(intent);
}

}
Loading