Android Studio:无法解析符号'SwipeRefreshLayout'

4

我现在正在使用Android Studio 3.1.1,build.gradle文件引入了以下内容:

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:recyclerview-v7:27.1.1'
    implementation 'com.android.support:support-vector-drawable:27.1.1'
    implementation 'com.android.support:cardview-v7:27.1.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation 'com.android.support:support-v4:27.1.1'
}

但是,当我尝试使用import android.support.v4.widget.SwipeRefreshLayout;来使用SwipeRefreshLayout时,它会显示无法解析符号 'SwipeRefreshLayout'

我尝试了以下解决方案:

  • 重新安装Android SDK
  • 删除.gradle文件夹并重新构建
  • 从Android Studio的文件菜单中选择"无效的缓存并重启"
  • 清理并重新构建项目

但仍然有相同的问题。

我正在使用以下代码:

import android.support.v4.widget.SwipeRefreshLayout;

public class HomeFragment extends Fragment {

static SwipeRefreshLayout swipeRefreshLayout;

public HomeFragment() {
}


@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    swipeRefreshLayout = view.findViewById(R.id.swipe_refresh);
    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            refresh();
        }
    });

    refresh();

    return view;
    }

}

分享你的代码在它被使用的地方。 - Android Geek
https://www.appsinfoway.com/implementing-swipe-to-refresh-android/ - IntelliJ Amiya
分享你的代码,展示你是如何使用它的! - Nabeel Nazir
对于androidx,请阅读:https://dev59.com/smEh5IYBdhLWcg3wKgqg#58453899 - user158
请标记我的回答为答案,谢谢。 - Nishant Shah
3个回答

23

将此依赖项添加到您的 gradle 文件中:

implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0'

5

对我来说,这个问题可以通过使用androidx.swiperefreshlayout.widget.SwipeRefreshLayout解决。

像这样:

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swiperefresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

带有依赖项:

implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"

请看这里的官方指南


0
首先,如果您已经添加了appcompat-v7,则无需添加support-v4。该库依赖于v4支持库,因此不必担心无法解析符号,只需转到文件->使缓存无效/重新启动->使缓存无效并重新启动或通过菜单中的Buld->清除项目来清除项目。

请查看官方文档https://developer.android.com/topic/libraries/support-library/packages.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接