Android实现上拉加载更多以及下拉刷新功能(ListView)

2019-12-10 19:20:15于海丽
易采站长站为您分析Android实现上拉加载更多功能以及下拉刷新功能的相关资料,需要的朋友可以参考下  

首先为大家介绍Andorid5.0原生下拉刷新简单实现。

先上效果图;

相对于上一个19.1.0版本中的横条效果好看了很多。使用起来也很简单。

Android实现上拉加载更多以及下拉刷新功能(ListView)

 <FrameLayout xmlns:android="http://www.easck.com/apk/res/android"
 xmlns:tools="http://www.easck.com/tools"
 android:id="@+id/container"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:ignore="MergeRootFrame" >

 <android.support.v4.widget.SwipeRefreshLayout
  android:id="@+id/swipe_container"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >

  <ListView
   android:id="@+id/list"
   android:layout_width="match_parent"
   android:layout_height="match_parent" >
  </ListView>
 </android.support.v4.widget.SwipeRefreshLayout>

</FrameLayout>
package hi.xiaoyu.swiperefreshlayout;

import hi.xiaoyu.swiperefreshlayout.adapter.TestAdapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.widget.ListView;

public class MainActivity extends Activity implements OnRefreshListener {

 private SwipeRefreshLayout swipeLayout;
 private ListView listView;
 private List<String> listDatas;
 private TestAdapter adapter;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
  listView = (ListView) findViewById(R.id.list);
  swipeLayout.setOnRefreshListener(this);
  swipeLayout.setColorSchemeResources(android.R.color.holo_orange_dark,
    android.R.color.holo_green_light,
    android.R.color.holo_orange_light,
    android.R.color.holo_red_light);
  listDatas = new ArrayList<String>();
  for (int i = 0; i < 10; i++) {
   listDatas.add("item" + i);
  }
  adapter = new TestAdapter(this, listDatas, R.layout.test_item);
  listView.setAdapter(adapter);
 }

 public void onRefresh() {
  new Handler().postDelayed(new Runnable() {
   public void run() {
    swipeLayout.setRefreshing(false);
    listDatas.addAll(listDatas);
    adapter.notifyDataSetChanged();
   }
  }, 3000);
 }

}