标签:android listview swiperefreshlayout 下拉刷新上拉加载
开发项目过程中基本都会用到listView的下拉刷新和上滑加载更多,之前大家最常用的应该是pull to refresh或它的变种版吧,google官方在最新的android.support.v4包中增加了一个新类SwipeRefreshLayout,地址 这个类的作用就是提供官方的下拉刷新,并且效果相当不错,而上拉加载更多则用我们自定义的listview,也是相当简单。
简单的介绍下:
首先它是一个viewgroup,但是它只允许有一个子控件,子控件能是任何view,使用的时候,所在类实现OnRefreshListener接口,在onRefresh()方法中实现所要完成的任务,
findviewbyid得到SwipeRefreshLayout后,显示刷新动画用SwipeRefreshLayout.setRefreshing(true);取消刷新动画用SwipeRefreshLayout.setRefreshing(false);相当简单。
另外有一些其他的常用方法比如判断时候正在显示刷新动画,用SwipeRefreshLayout.isShown()。
布局:
<android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swip_index" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.listviewloadmore.LoadMoreListView android:id="@+id/listView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" > </com.example.listviewloadmore.LoadMoreListView> </android.support.v4.widget.SwipeRefreshLayout>
package com.example.listviewloadmore; import android.annotation.SuppressLint; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.ListView; public class LoadMoreListView extends ListView implements OnScrollListener{ private View footer; private int totalItem; private int lastItem; private boolean isLoading; private OnLoadMore onLoadMore; private LayoutInflater inflater; public LoadMoreListView(Context context) { super(context); init(context); } public LoadMoreListView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public LoadMoreListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context); } @SuppressLint("InflateParams") private void init(Context context) { inflater = LayoutInflater.from(context); footer = inflater.inflate(R.layout.load_more_footer,null ,false); footer.setVisibility(View.GONE); this.addFooterView(footer); this.setOnScrollListener(this); } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { this.lastItem = firstVisibleItem+visibleItemCount; this.totalItem = totalItemCount; } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if(this.totalItem == lastItem&&scrollState == SCROLL_STATE_IDLE){ Log.v("isLoading", "yes"); if(!isLoading){ isLoading=true; footer.setVisibility(View.VISIBLE); onLoadMore.loadMore(); } } } public void setLoadMoreListen(OnLoadMore onLoadMore){ this.onLoadMore = onLoadMore; } /** * 加载完成调用此方法 */ public void onLoadComplete(){ footer.setVisibility(View.GONE); isLoading = false; } public interface OnLoadMore{ public void loadMore(); } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/load_more_footer" android:layout_width="fill_parent" android:layout_height="wrap_content" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" > <ProgressBar android:id="@+id/load_more_progressBar" android:layout_width="36dp" android:layout_height="36dp" android:padding="3dp" android:visibility="visible" /> <TextView android:id="@+id/no_more_textView" android:layout_width="wrap_content" android:layout_height="36dp" android:gravity="center" android:text="正在加载" android:textColor="#666666" android:textSize="16sp" android:visibility="visible" /> </LinearLayout> </RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.listviewloadmore.MainActivity" > <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swip_index" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.listviewloadmore.LoadMoreListView android:id="@+id/listView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" > </com.example.listviewloadmore.LoadMoreListView> </android.support.v4.widget.SwipeRefreshLayout> </RelativeLayout>
package com.example.listviewloadmore; import java.util.ArrayList; 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.ArrayAdapter; import android.widget.Toast; import com.example.listviewloadmore.LoadMoreListView.OnLoadMore; public class MainActivity extends Activity implements OnRefreshListener, OnLoadMore { private LoadMoreListView listView; private SwipeRefreshLayout swip; private int page = 1; ArrayList<String> data1 = new ArrayList<String>(); private ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(1); } private void initView() { listView = (LoadMoreListView) findViewById(R.id.listView); listView.setLoadMoreListen(this); swip = (SwipeRefreshLayout) findViewById(R.id.swip_index); swip.setOnRefreshListener(this); swip.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data1); } /** * 加载数据 * @param page2 页数 */ private void initData(int page2) { // TODO Auto-generated method stub if(page2==1){ for (int i = 0; i < 10; i++) { data1.add((i+1)+""); listView.setAdapter(adapter); } }else{ data1.add("新加的第"+(9+page2)+"个"); } } // 下拉刷新的方法 @Override public void onRefresh() { Toast.makeText(this, "开始刷新啦", Toast.LENGTH_SHORT).show(); new Handler().postDelayed(new Runnable() { @Override public void run() { if(swip.isShown()){ swip.setRefreshing(false); } Toast.makeText(MainActivity.this, "刷新完成了", Toast.LENGTH_SHORT).show(); } }, 3000); } // 加载更多的方法 @Override public void loadMore() { page++; new Handler().postDelayed(new Runnable() { @Override public void run() { initData(page); listView.onLoadComplete(); Toast.makeText(MainActivity.this, "加载完成", Toast.LENGTH_SHORT).show(); adapter.notifyDataSetChanged(); } }, 3000); } }
标签:android listview swiperefreshlayout 下拉刷新上拉加载
原文地址:http://blog.csdn.net/u012782626/article/details/45701203