1. 程式人生 > >Android簡單的下拉重新整理,上拉載入

Android簡單的下拉重新整理,上拉載入

先匯入第三方的東西 下載地址

匯入後,就和你的專案聯絡起來 這裡寫圖片描述

佈局程式碼(activity_pull_to_refresh_action.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".PullToRefreshAction">
<com.handmark.pulltorefresh.library.PullToRefreshListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/pull_to_refresh_id" >
</com.handmark.pulltorefresh.library.PullToRefreshListView> </LinearLayout>

Java檔案(PullToRefreshAction.java)

package com.example.android_06;

import android.os.AsyncTask;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import
android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshListView; import java.util.ArrayList; import java.util.List; public class PullToRefreshAction extends AppCompatActivity { private List<String> list; private PullToRefreshListView listView; private ArrayAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pull_to_refresh_action); listView = findViewById(R.id.pull_to_refresh_id); list = new ArrayList<>(); for (int i = 1; i <=10 ; i++) { list.add("峰峰"+i); } //設定介面卡 adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list); listView.setAdapter(adapter); //給下拉重新整理的ListView新增下拉事件 listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() { @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) { //Toast.makeText(PullToRefreshAction.this, "重新整理了", Toast.LENGTH_SHORT).show(); new MyTask().execute();//建立任務類物件開始執行 } }); listView.setMode(PullToRefreshBase.Mode.BOTH);//允許下拉上拉 //includeStart:true:上拉 includeEnd:true:下拉 listView.getLoadingLayoutProxy(true,true);//可有可無 } //建立非同步任務類 class MyTask extends AsyncTask{ @Override protected Object doInBackground(Object[] objects) { for (int i =1; i <=5 ; i++) { list.add(0,"微微"+i); SystemClock.sleep(1000);//載入一次休息一秒 } return null; } @Override protected void onPostExecute(Object o) { super.onPostExecute(o); //通知下拉控制元件重新整理完成 listView.onRefreshComplete(); //通知介面卡資料發生改變 adapter.notifyDataSetChanged(); } } }