PullToRefreshScrollView的使用

以下亲测可行,希望能帮到你


布局文件ScrollView嵌套ListView,使用PullToRefresScrollView代替原本的ScrollView,自定义的ListView解决嵌套时高度计算问题 




    
        
            
        
    

自定义的ListView

/**
 * Created by Administrator on 2016/9/14.
 */
public class MyListView extends ListView {
    public MyListView(Context context) {
        super(context);
    }

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

    public MyListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}

调用的代码

public class MainActivity extends AppCompatActivity {
    ScrollView mScrollView;
    private PullToRefreshScrollView mPullRefreshScrollView;
    private List ls;
    private List ls2;
    private MyListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //查找到控件
        mPullRefreshScrollView = (PullToRefreshScrollView) findViewById(R.id.pulltoscroll);


        //设置刷新或者加载时候显示的内容
        mPullRefreshScrollView.getLoadingLayoutProxy().setLastUpdatedLabel("lastUpdateLabel");
        mPullRefreshScrollView.getLoadingLayoutProxy().setPullLabel("PULLLABLE");
        mPullRefreshScrollView.getLoadingLayoutProxy().setRefreshingLabel("refreshingLabel");
        mPullRefreshScrollView.getLoadingLayoutProxy().setReleaseLabel("releaseLabel");
        // 上拉、下拉或者全部允许
        mPullRefreshScrollView.setMode(PullToRefreshBase.Mode.PULL_FROM_START); //设置允许下拉

        mPullRefreshScrollView.setMode(PullToRefreshBase.Mode.PULL_FROM_END); //设置允许上拉
        mPullRefreshScrollView.setMode(PullToRefreshBase.Mode.BOTH); //设置上下拉均允许


        //这个被注释掉的方法在刷新完成时被调用,但加载完成也会走这个方法,我们自己进行判断容易考虑不周,所以使用OnRefreshListener2进行回调监听

        mPullRefreshScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshBase refreshView) {
                Toast.makeText(MainActivity.this, "刷新完成", Toast.LENGTH_SHORT).show();
                mPullRefreshScrollView.onRefreshComplete();
            }

            OnRefreshListener2
        });


        //这是OnRefreshListener2,里面有分别对应的加载和刷新执行完之后分别执行的回调

        mPullRefreshScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase refreshView) {
                Toast.makeText(MainActivity.this, "刷新完成", Toast.LENGTH_SHORT).show();
                mPullRefreshScrollView.onRefreshComplete();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase refreshView) {
                Toast.makeText(MainActivity.this, "加载完成", Toast.LENGTH_SHORT).show();
                mPullRefreshScrollView.onRefreshComplete();
            }
        });


    }
}
public class MainActivity extends AppCompatActivity {
    ScrollView mScrollView;
    private PullToRefreshScrollView mPullRefreshScrollView;
    private List ls;
    private List ls2;
    private MyListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPullRefreshScrollView = (PullToRefreshScrollView) findViewById(R.id.pulltoscroll);
        //这几个刷新Label的设置
        mPullRefreshScrollView.getLoadingLayoutProxy().setLastUpdatedLabel("lastUpdateLabel");
        mPullRefreshScrollView.getLoadingLayoutProxy().setPullLabel("PULLLABLE");
        mPullRefreshScrollView.getLoadingLayoutProxy().setRefreshingLabel("refreshingLabel");
        mPullRefreshScrollView.getLoadingLayoutProxy().setReleaseLabel("releaseLabel");
        上拉、下拉设定 // mPullRefreshScrollView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        mPullRefreshScrollView.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        mPullRefreshScrollView.setMode(PullToRefreshBase.Mode.BOTH);
        lv = (MyListView) findViewById(R.id.lv);
        ls = new ArrayList<>();
        ls2 = new ArrayList<>();
        add();
        ls2.addAll(ls);
        seta();
        mScrollView = mPullRefreshScrollView.getRefreshableView();
        PullToRefreshScrollView
        mPullRefreshScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshBase refreshView) {
                Toast.makeText(MainActivity.this, "刷新完成", Toast.LENGTH_SHORT).show();
                mPullRefreshScrollView.onRefreshComplete();
            }
        });
        mPullRefreshScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase refreshView) {
                Toast.makeText(MainActivity.this, "刷新完成", Toast.LENGTH_SHORT).show();
                add();
                ls2.addAll(ls);
                seta();
                mPullRefreshScrollView.onRefreshComplete();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase refreshView) {
                Toast.makeText(MainActivity.this, "加载完成", Toast.LENGTH_SHORT).show();
                add();
                ls2.addAll(ls);
                seta();
                mPullRefreshScrollView.onRefreshComplete();
            }
        });
    }

    private void seta() {
        lv.setAdapter(new MyAdapter(ls2, MainActivity.this));
    }

    private void add() {
        for (int i = 0; i < 100; i++) {
            ls.add("条目" + i);
        }
    }
}


 

 

你可能感兴趣的:(android基础进阶)