viewpager2的setOffscreenPageLimit(3)无效。

huangapple go评论148阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年9月18日 17:19:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63952901.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定