如何使用NestedScrollView进行分页?

时间:2018-01-18 11:44:51

标签: android android-recyclerview pagination android-nestedscrollview recyclerview-layout

当我使用嵌套滚动视图进行分页时,需要花费太多时间,有时我的应用会挂起?请告诉我使用嵌套的scrollview

实现分页的正确方法

2 个答案:

答案 0 :(得分:1)

1。。设置嵌套滚动启用回收站视图的false。

recyclerView.setNestedScrollingEnabled(false);

2。。将滚动列表器添加到嵌套的滚动视图中。

  return (
      <Container style={styles.container}>
      <View>
      {this.state.section.map(article =>
      <View>

      <WebView
        source={{uri:article.data.description}}
        style={{marginTop: 20}}
      />
      </View>

)}
        </View>

      </Container>
    );

答案 1 :(得分:0)

在您的包中添加此类

    import android.content.Context;
import android.support.v4.widget.NestedScrollView;
import android.util.AttributeSet;
import android.view.View;

public class TouchDetectableScrollView extends NestedScrollView {

    OnMyScrollChangeListener myScrollChangeListener;

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

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

    public TouchDetectableScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);

        if (myScrollChangeListener!=null)
        {
            if (t>oldt)
            {
                myScrollChangeListener.onScrollDown();
            }
            else if (t<oldt){
                myScrollChangeListener.onScrollUp();
            }
            View view = (View) getChildAt(getChildCount()-1);
            int diff = (view.getBottom()-(getHeight()+getScrollY()));
            if (diff == 0 ) {
                myScrollChangeListener.onBottomReached();
            }
        }
    }

    public OnMyScrollChangeListener getMyScrollChangeListener() {
        return myScrollChangeListener;
    }

    public void setMyScrollChangeListener(OnMyScrollChangeListener myScrollChangeListener) {
        this.myScrollChangeListener = myScrollChangeListener;
    }

    public interface OnMyScrollChangeListener
    {
            public void onScrollUp();
            public void onScrollDown();
            public void onBottomReached();
    }
}

现在在xml和java代码中使用TouchDetectableScrollView代替NestedScrollView。 并将Listener设置为:

TouchDetectableScrollView nestedScrollView=findViewById(R.id.nestedScrollView);
        nestedScrollView.setMyScrollChangeListener(new TouchDetectableScrollView.OnMyScrollChangeListener() {
            @Override
            public void onScrollUp() {

            }

            @Override
            public void onScrollDown() {

            }

            @Override
            public void onBottomReached() {
                // api call for pagination
            }
        });

onBottomReached方法

中执行您的分页任务