SwipeRefreshLayout刷新侦听器未调用onRefresh()

时间:2017-03-12 06:44:55

标签: java android swiperefreshlayout

我目前有一个SwipeRefreshLayout,每当我下拉时它都没有调用它的OnRefresh方法。我的代码有问题吗?

public class MainActivity extends AppCompatActivity {
private SwipeRefreshLayout swipe_view;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    swipe_view = (SwipeRefreshLayout) findViewById(R.id.this_will_work);

    swipe_view.setOnRefreshListener(new OnRefreshListener()
    {
        @Override
        public void onRefresh()
        {
            Toast.makeText(getApplicationContext(), "OnRefresh() test", Toast.LENGTH_SHORT).show();
        }
    });

1 个答案:

答案 0 :(得分:2)

您应该拥有一项服务或更新内容,如下所示:

private void refresh() {
    startService(new Intent(this, UpdaterService.class));
}

然后刷新:

mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
    mSwipeRefreshLayout.setOnRefreshListener(this);
    mSwipeRefreshLayout.post(new Runnable() {
                                @Override
                                public void run() {
                                    mSwipeRefreshLayout.setRefreshing(true);
                                    refresh();
                                }});

//here refreshed Item
   //getLoaderManager().initLoader(0, null, this);

    if (savedInstanceState == null) {
        refresh();
    }

并且您的onRefresh()应包含相同的方法refresh()