-
If you're planning to use {@link androidx.fragment.app.Fragment Fragments} as pages,
-
implement {@link androidx.viewpager2.adapter.FragmentStateAdapter FragmentStateAdapter}. If
-
your pages are Views, implement {@link RecyclerView.Adapter} as usual.
-
If your pages contain LayoutTransitions, then those LayoutTransitions must have
-
{@code animateParentHierarchy} set to {@code false}. Note that if you have a ViewGroup with
-
{@code animateLayoutChanges=“true”} in your layout xml file, a LayoutTransition is added
-
automatically to that ViewGroup. You will need to manually call {@link
-
android.animation.LayoutTransition#setAnimateParentHierarchy(boolean)
-
getLayoutTransition().setAnimateParentHierarchy(false)} on that ViewGroup after you inflated
-
the xml layout, like this:
-
View view = layoutInflater.inflate(R.layout.page, parent, false);
-
ViewGroup viewGroup = view.findViewById(R.id.animated_viewgroup);
-
viewGroup.getLayoutTransition().setAnimateParentHierarchy(false);
-
@param adapter The adapter to use, or {@code null} to remove the current adapter
-
@see androidx.viewpager2.adapter.FragmentStateAdapter
-
@see RecyclerView#setAdapter(Adapter)
*/
public void setAdapter(@Nullable @SuppressWarnings(“rawtypes”) Adapter adapter) {
final Adapter<?> currentAdapter = mRecyclerView.getAdapter();
mAccessibilityProvider.onDetachAdapter(currentAdapter);
unregisterCurrentItemDataSetTracker(currentAdapter);
mRecyclerView.setAdapter(adapter);
mCurrentItem = 0;
restorePendingState();
mAccessibilityProvider.onAttachAdapter(adapter);
registerCurrentItemDataSetTracker(adapter);
}
描述的意思 大概是使用RecyclerView 的 dapter,在了解了使用方法以后,于是就开始体验一下吧!
@FindView(R.id.splash_viewpage2)
ViewPager2 viewPager2;
设置滑动方向 —> 左右或者上下
viewPager2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
ViewPager2的Adapter
public class SplashAdapter extends RecyclerView.Adapter<SplashAdapter.SplashVH> {
private List list;
public SplashAdapter(List list) {
this.list = list;
}
@NonNull
@Override
public SplashVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = ((LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.item_splash_wellcome,parent,false);
return new SplashVH(view);
}
@Over
ride
public void onBindViewHolder(@NonNull SplashVH holder, int position) {
holder.iv_splash_image_center.setBackgroundResource(list.get(position).getIcon());
// holder.imageView.startCountDown();
}
@Override
public int getItemCount() {
return list == null ? 0 : list.size();
}
static class SplashVH extends RecyclerView.ViewHolder{
private ImageView iv_splash_image_center;
private CountDownView countDownView;
public SplashVH(@NonNull View itemView) {
super(itemView);
this.iv_splash_image_center = itemView.findViewById(R.id.iv_splash_image_center);
countDownView = itemView.findViewById(R.id.cdv_splash_number);
}
}
那么 跟之前的 ViewPager有些API变更 比如 addOnPageChange 现已经更换为:
registerOnPageChangeCallback
Code:
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
if (position == list.size()-1){
btn_splash_start.setVisibility(View.VISIBLE);
btn_splash_start.startAnimation(animation);
}else{
btn_splash_start.setVisibility(View.GONE);
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
}
@Override
public void onPageScrollStateChanged(int state) {
最后
小编这些年深知大多数初中级Android工程师,想要提升自己,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、电子书籍、讲解视频,并且后续会持续更新
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人
都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
如果你需要这些资料, ⬅ 专栏获取
71916780)]
[外链图片转存中…(img-aBrhirJS-1719171916781)]
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、电子书籍、讲解视频,并且后续会持续更新
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人
都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
如果你需要这些资料, ⬅ 专栏获取