Android下拉刷新与ListView Fragment以及自定义ListView适配器

4
我使用Android Studio的启动选项(自动构建带有选项卡的操作栏),因此一些内容已经为我生成。但是,我对每个选项卡都实现了一个使用自定义适配器的列表视图片段。
我在尝试添加Chris Bane的下拉刷新库时遇到了问题。也许我导入方法不正确?我对Android开发和Gradle不太熟悉,特别是这个新的Gradle东西。
我在build.gradle文件中导入了GitHub存储库。
apply plugin: 'android'

android {
compileSdkVersion 19
buildToolsVersion "19.0.1"

defaultConfig {
    minSdkVersion 14
    targetSdkVersion 19
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
  }
}
dependencies {
   compile 'com.android.support:support-v4:19.0.1'
   compile 'com.android.support:appcompat-v7:19.0.1'
   compile 'com.github.chrisbanes.actionbarpulltorefresh:extra-abc:+'
 } 

我的项目创建时,除了minSDKVersion和依赖项中的最后一次编译外,所有内容都是预生成的。这个build.gradle文件是我`/app文件夹中的一个。

Gradle将正确同步,我将在项目根目录的.idea文件夹中看到来自actionbar git的文件。

这里是我困惑的地方:

在github存储库“快速入门”中,它说

你需要做的第一件事是将可刷新的视图包装在PullToRefreshLayout中:

使用代码示例执行以下操作:

<uk.co.senab.actionbarpulltorefresh.extras.actionbarcompat.PullToRefreshLayout>
<listview/>
</uk.co.senab.actionbarpulltorefresh.extras.actionbarcompat.PullToRefreshLayout>

uk.co.senab.actionbarpulltorefresh.extras是我的项目名称吗?还是com.lucaapp.app

这是我ListFragment的XML,您会看到我的困惑:

<?xml version="1.0" encoding="utf-8"?>

<com.lucaapp.app.PullToRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ptr_layout" >


<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="#b5b5b5"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector" />


<TextView
    android:id="@android:id/empty"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="No Events" />

</com.lucaapp.app.PullToRefreshLayout>

这不会立即给我任何错误,直到我尝试在我的片段类中实现来自“快速入门指南”的代码。我尝试导入该 github 存储库方法时,出现“无法解析符号”错误。
我尝试了以下操作,使导入变为红色,并出现“无法解析符号”错误:
import com.lucapp.app.PullToRefreshAttacher;
import com.lucapp.app.PullToRefreshLayout;
import uk.co.senab.actionbarpulltorefresh.extras.actionbarcompat.PullToRefreshAttacher;

当您使用minsdk = 14时,为什么要使用compile 'com.github.chrisbanes.actionbarpulltorefresh:extra-abc:+'?请使用compile 'com.github.chrisbanes.actionbarpulltorefresh:library:+'。 - Gabriele Mariotti
2个回答

1

0

你尝试过跟随 Chris Banes 的教程吗? https://github.com/chrisbanes/ActionBar-PullToRefresh/wiki/QuickStart-ABS

这个教程包含了所有步骤,从 build.gradle 开始到代码片段结束。

唯一的区别是,教程是针对 ScrollView(而不是你想要的 ListView)。但不要害怕。只需将 ScrollView 替换为 ListView,一切就应该没问题了。

更新: 你应该特别注意这个块:

<uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ptr_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Your content, here we're using a ScrollView -->

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ScrollView>

</uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout>

你不应该使用 com.lucaapp.app.PullToRefreshLayout,而应该使用 uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout


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