android - 在 onCreate 中注册时未调用 SwipeRefreshLayout OnRefreshListener

标签 android swiperefreshlayout

我正在尝试使用 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),导致资源再次膨胀。

关于android - 在 onCreate 中注册时未调用 SwipeRefreshLayout OnRefreshListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34642423/

相关文章:

android - 带 fragment 的 RecyclerView

android - 在 ViewAnimator 中的按钮上执行(单击())后,Espresso 测试卡住/不活动

Android:Viewpager 内的 SwipeRefreshLayout 内的搜索栏

java - SwipeRefreshLayout 阻止 android.widget.switch 拖动

java - 禁用单击 SwipeRefreshLayout 内的 RecyclerView

android - gradle mustRunAfter 似乎不起作用

android - 如何解决 java.net.MalformedURLException?

android - 设置背景图像不起作用

android - 如何向 SwipeRefreshLayout 内的 listView 添加标题?

具有反向布局的 RecyclerView 上的 Android SwipeRefreshLayout