pulltorefreshdemo 上拉下拉

MainActivity 类

package com.example.pulltore;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.Arrays;
import java.util.LinkedList;

public class MainActivity extends AppCompatActivity {

    private LinkedList<String> mListItems;
    private PullToRefreshListView mPullRefreshListView;
    private String[] mStrings = {"邵延禹和赵丽颖", "邵延禹和赵丽颖", "邵延禹和赵丽颖", "邵延禹和赵丽颖", "邵延禹和赵丽颖",
            "邵延禹和赵丽颖", "邵延禹和赵丽颖", "邵延禹和赵丽颖", "邵延禹和赵丽颖", "邵延禹和赵丽颖", "邵延禹和赵丽颖", "邵延禹和赵丽颖",
            "邵延禹和赵丽颖", "邵延禹和赵丽颖", "邵延禹和赵丽颖", "邵延禹和赵丽颖", "邵延禹和赵丽颖", "邵延禹和赵丽颖",
            "邵延禹和赵丽颖", "邵延禹和赵丽颖", "邵延禹和赵丽颖", "邵延禹和赵丽颖", "邵延禹和赵丽颖", "邵延禹和赵丽颖", "邵延禹和赵丽颖",
            "邵延禹和赵丽颖"};
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPullRefreshListView = (PullToRefreshListView) findViewById(
                R.id.pull_refresh_list);
        mPullRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        mListItems = new LinkedList<>();
        mListItems.addAll(Arrays.asList(mStrings));
        adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, mListItems);
        mPullRefreshListView.setAdapter(adapter);
        mPullRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
                new MAsyncTack().execute();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
                new MAsyncTask2().execute();
            }
        });

        }

        class MAsyncTack extends AsyncTask<Void,Void,String[]>{
            @Override
            protected String[] doInBackground(Void... voids) {
                try {
                    Thread.sleep( 2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return new String[0];
            }

            @Override
            protected void onPostExecute(String[] strings) {
                super.onPostExecute(strings);
                mListItems.addFirst("老王和爽姐在一起");
                adapter.notifyDataSetChanged();
                mPullRefreshListView.onRefreshComplete();
            }
        }
        class MAsyncTask2 extends AsyncTask<Void,Void,String[]>{

            @Override
            protected String[] doInBackground(Void... voids) {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return new String[0];
            }

            @Override
            protected void onPostExecute(String[] strings) {
                super.onPostExecute(strings);
                mListItems.addLast("小禹和颖宝");
                adapter.notifyDataSetChanged();
                //让刷新UI隐藏;
                mPullRefreshListView.onRefreshComplete();
            }
        }
    }
 
布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="com.example.pulltore.MainActivity">

    <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/pull_refresh_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000"
        android:divider="#19000000"
        android:dividerHeight="4dp"
        android:fadingEdge="none"
        android:fastScrollEnabled="false"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false"
        android:smoothScrollbar="true" />

</android.support.constraint.ConstraintLayout>

 
//清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.pulltore">

    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true" android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
//依赖
apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"

    defaultConfig {
        applicationId "com.example.pulltore"
        minSdkVersion 18
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile 'com.github.userswlwork:pull-to-refresh:1.0.0'
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值