Day06

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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值