Scroll effect between fragments without actually allowing the user to scroll?

问题: I'm trying to make a single activity with a modular section of its screen that houses a variety of non-sequential fragments. However, using ViewPager allows the user to...

问题:

I'm trying to make a single activity with a modular section of its screen that houses a variety of non-sequential fragments.

However, using ViewPager allows the user to scroll through all of these fragments at will. I'd like to set it up so that the fragments can only be seen and accessed if the user clicks a button in the fixed section of the activity, or in another one of the fragments. Is this possible?

What would be the best way to get this effect?


回答1:

Yes, you can subclass ViewPager to intercept user inputs to disable the scroll. Then you can control the ViewPager with your buttons directly.

public class CustomViewPager extends ViewPager {

    private boolean isPagingEnabled = true;

    public CustomViewPager(Context context) {
        super(context);
    }

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return this.isPagingEnabled && super.onTouchEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return this.isPagingEnabled && super.onInterceptTouchEvent(event);
    }

    public void setPagingEnabled(boolean b) {
        this.isPagingEnabled = b;
    }
}

Taken from https://stackoverflow.com/a/7814054/3106174

  • 发表于 2019-04-01 17:26
  • 阅读 ( 194 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除