Android Pull-to-Refresh with ListView Fragment & Custom ListView Adapter

杀马特。学长 韩版系。学妹 提交于 2020-01-03 18:21:48

问题


I built my project using the Android Studio start options (Automatically builds the action bar with the tabs) so a bit is generated for me. I did however implement a list-view fragment for each one of the tabs that uses custom adapters.

I'm having a trouble adding the pull-to-refresh lib from Chris Bane. Maybe I'm importing it wrong? I'm fairly new at android development and especially with this new Gradle stuff.

I'm Importing the github repository in the build.gradle file here:

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:+'
 } 

Everything minus the minSDKVersion & the last compile in dependencies was pre-generated when my project was created. This build.gradle file is the one in my `/app folder.

Gradle will sync properly and I will see files from the actionbar git in my .idea folder in the project root.

Here is where I am getting confused:

In the github repo "quick start" it says

The first thing you need to do is wrap your refreshable view in a PullToRefreshLayout:

With a code example doing something like this:

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

Is uk.co.senab.actionbarpulltorefresh.extras suppose to be my projects name, so com.lucaapp.app?

Here is my XML for my ListFragment, where u'll see my confusion:

<?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>

This will not give me any errors immediately until I try to implement the code from the quick start guide in my fragment class. I get an Cannot Resolve Symbol error trying to import that github repo methods.

I've tried the following which makes the import red with cannot resolve symbol error:

import com.lucapp.app.PullToRefreshAttacher;
import com.lucapp.app.PullToRefreshLayout;
import uk.co.senab.actionbarpulltorefresh.extras.actionbarcompat.PullToRefreshAttacher;

回答1:


Have you tried to follow this tutorial from Chris Banes: https://github.com/chrisbanes/ActionBar-PullToRefresh/wiki/QuickStart-ABS ?

This tutorial contains all steps, beginning from build.gradle and finishing with code snippets.

The only difference: tutorial is for ScrollView (not ListView as you wish). But don't be scared. Just replace ScrollView with ListView and everything should be OK.

UPDATE: you should see at this block especially:

<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>

you should not use com.lucaapp.app.PullToRefreshLayout instead of uk.co.senab.actionbarpulltorefresh.extras.actionbarsherlock.PullToRefreshLayout




回答2:


Now android has introduced new simple featrure called Swipe to refresh layout. Please refer the site devloper site and a simple example



来源:https://stackoverflow.com/questions/22543047/android-pull-to-refresh-with-listview-fragment-custom-listview-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!