Day06
#1
private Context context;
private List<String> mData;
public ViewPagerAdapter(Context context, List<String> mData) {
this.context = context;
this.mData = mData;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view == o;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
//加载vp的布局
View inflate = View.inflate(context, R.layout.vp_layout, null);
//给布局中的控件赋值
TextView textView = inflate.findViewById(R.id.vp_tv_id);
textView.setText(mData.get(position));
//添加一个布局
container.addView(inflate);
return inflate;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
}
public class ViewPagerActivity extends AppCompatActivity {
private ViewPager vpId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去掉自带的bar
//supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_veiw_pager);
vpId = findViewById(R.id.vp_id);
//初始化数据
List<String> list = new ArrayList<>();
for (int i = 0; i < 4; i++) {
list.add("第"+i+"个View");
}
//设置adapter
vpId.setAdapter(new ViewPagerAdapter(this,list));
}
}
public class DepthPageTransformer implements ViewPager.PageTransformer {
private static final float MIN_SCALE = 0.75f;
public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);
} else if (position <= 0) { // [-1,0]
// Use the default slide transition when moving to the left page
view.setAlpha(1);
view.setTranslationX(0);
view.setScaleX(1);
view.setScaleY(1);
} else if (position <= 1) { // (0,1]
// Fade the page out.
view.setAlpha(1 - position);
// Counteract the default slide transition
view.setTranslationX(pageWidth * -position);
// Scale the page down (between MIN_SCALE and 1)
float scaleFactor = MIN_SCALE
+ (1 - MIN_SCALE) * (1 - Math.abs(position));
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
}
public class ViewPagerActivity extends AppCompatActivity {
private ViewPager vpId;
private CountDownTimer timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去掉自带的bar
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_veiw_pager);
//找到vp
vpId = findViewById(R.id.vp_id);
//初始化数据
final List<String> list = new ArrayList<>();
for (int i = 0; i < 4; i++) {
list.add("第"+i+"个View");
}
//设置adapter
vpId.setPageTransformer(false,new DepthPageTransformer());
vpId.setAdapter(new ViewPagerAdapter(this,list));
//安卓自带的定时器方法
timer = new CountDownTimer(100000, 2000) {
int index = 0;
@Override
public void onTick(long millisUntilFinished) {
//设置当前viewpager显示那个页面
vpId.setCurrentItem(index);
++index;
if (index == list.size()){
index = 0;
}
}
@Override
public void onFinish() {
}
};
timer.start();
}
}
vpId.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Log.e("vp","滑动中==position:"+ position + " positionOffset:"+ positionOffset + " positionOffsetPixels:"+positionOffsetPixels);
}
@Override
public void onPageSelected(int position) {
Log.e("vp","显示页改变==postion:"+ position);
}
@Override
public void onPageScrollStateChanged(int state) {
switch (state) {
case ViewPager.SCROLL_STATE_IDLE:
Log.e("vp","状态改变==SCROLL_STATE_IDLE==静止状态");
break;
case ViewPager.SCROLL_STATE_DRAGGING:
Log.e("vp","状态改变==SCROLL_STATE_DRAGGING==滑动状态");
break;
case ViewPager.SCROLL_STATE_SETTLING:
Log.e("vp","状态改变==SCROLL_STATE_SETTLING==滑翔状态");
break;
}
}
});
public class PagerFragment extends Fragment {
String mContent;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mContent = (String) getArguments().get("content");
View view = inflater.inflate(R.layout.fragment_pager, container, false) ;
TextView textView = (TextView) view.findViewById(R.id.tv);
textView.setText(mContent);
return view;
}
}
来源:CSDN
作者:zhulin_yuexia
链接:https://blog.csdn.net/zhulin_yuexia/article/details/104522119