如何根据需要刷新SwipeRefreshLayout?

时间:2016-02-19 14:04:33

标签: android layout

我有一些开源项目(CySmart)的SwipeRefreshLayout。

我想添加一个刷新布局的按钮,好像它现在被刷掉一样(按需)。

如何实现这种效果

1 个答案:

答案 0 :(得分:1)

你可以做;

@Override
protected void onCreate(Bundle savedInstanceState){
    final SwipeRefreshLayout layout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh);
    Button button = (Button) findViewById(R.id.button_refresh);

    layout.setOnRefreshListener(refreshListener);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dispatchRefresh(); //Refresh
        }
    });
}
private SwipeRefreshLayout.OnRefreshListener refreshListener = new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        dispachRefresh();
    }
};

private void dispatchRefresh(){

    layout.setRefreshing(true);
    //code to refesh data, etc.
}

然后;

layout.setRefreshing(false);
刷新完成后