英文:
viewpager2 setoffscreenpagelimit(3) not working
问题
抱歉,我理解您只需要对代码部分进行翻译。以下是您的Java代码的翻译:
public class ExplorerFragment extends Fragment {
ViewPager2 usersAvatarSlider;
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.explorer_fragment, container, false);
Find(v);
initUsersAvatarSlider();
return v;
}
void Find(View v) {
usersAvatarSlider = v.findViewById(R.id.UserViewPager2);
}
void initUsersAvatarSlider() {
UserAvatarSliderAdapter userAvatarSliderAdapter = new UserAvatarSliderAdapter();
usersAvatarSlider.setAdapter(userAvatarSliderAdapter);
usersAvatarSlider.setOffscreenPageLimit(3);
}
}
这是您提供的Java代码的中文翻译。如果您有任何其他问题或需要进一步的帮助,请随时提出。
英文:
Hi guys I want to make a slider with Viewpager2 that shows 3 element at the moment
and I want it to look like this
how I want to my slider look like
but my slider is look like this
how my slider look like
and here is my java code
public class ExplorerFragment extends Fragment {
ViewPager2 usersAvatarSlider;
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.explorer_fragment, container, false);
Find(v);
initUsersAvatarSlider();
return v;
}
void Find(View v) {
usersAvatarSlider = v.findViewById(R.id.UserViewPager2);
}
void initUsersAvatarSlider() {
UserAvatarSliderAdapter userAvatarSliderAdapter = new UserAvatarSliderAdapter();
usersAvatarSlider.setAdapter(userAvatarSliderAdapter);
usersAvatarSlider.setOffscreenPageLimit(3);
}
}
as u see I'm using usersAvatarSlider.setOffscreenPageLimit(3);
but I don't know why it's not working
答案1
得分: 1
setOffscreenPageLimit()
不会设置元素数量。根据文档:
open fun setOffscreenPageLimit(limit: Int): Unit
在视图层次结构处于空闲状态时,设置应在当前页面两侧保留的页面数量。超出此限制的页面将在需要时从适配器中重新创建。
要设置元素数量,请在您的适配器实现UserAvatarSliderAdapter
中覆盖getItemCount()
。
英文:
setOffscreenPageLimit()
does not set the element count. As per the docs:
> open fun setOffscreenPageLimit(limit: Int): Unit
>
> Set the number of pages that should be retained to either side of the
> current page in the view hierarchy in an idle state. Pages beyond this
> limit will be recreated from the adapter when needed.
To set the element count, override getItemCount()
in your adapter implementation UserAvatarSliderAdapter
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论