在 onCreate 中注册时未调用 SwipeRefreshLayout OnRefreshListener
SwipeRefreshLayout OnRefreshListener not called when registered in onCreate
我正在尝试使用 SwipeRefreshLayout
,但是当我在 activity 的 onCreate()
方法中设置 OnRefreshListener
时,滑动时不会调用它。查看示例代码,这似乎是正确的做事方式。当我稍后注册侦听器时,例如在 onPostCreate()
中或按下按钮时,它确实会在滑动时被调用。我错过了什么?
MessageListActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message_list);
final SwipeRefreshLayout swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_layout);
swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
Log.d(TAG, "Swiped for refresh!");
}
});
}
activity_message_list.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".LoginActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/swipe_layout">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/list"
android:drawSelectorOnTop="false">
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
原来我犯了一个相当愚蠢的错误。我在 onCreate 之后的某处调用了 setContentView(R.layout.activity_message_list),导致资源再次膨胀。
我正在尝试使用 SwipeRefreshLayout
,但是当我在 activity 的 onCreate()
方法中设置 OnRefreshListener
时,滑动时不会调用它。查看示例代码,这似乎是正确的做事方式。当我稍后注册侦听器时,例如在 onPostCreate()
中或按下按钮时,它确实会在滑动时被调用。我错过了什么?
MessageListActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message_list);
final SwipeRefreshLayout swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_layout);
swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
Log.d(TAG, "Swiped for refresh!");
}
});
}
activity_message_list.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".LoginActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/swipe_layout">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/list"
android:drawSelectorOnTop="false">
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
原来我犯了一个相当愚蠢的错误。我在 onCreate 之后的某处调用了 setContentView(R.layout.activity_message_list),导致资源再次膨胀。