欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實(shí)現(xiàn)上拉加載更多以及下拉刷新功能(ListView)

 更新時(shí)間:2016年01月13日 17:22:44   投稿:lijiao  
這篇文章主要介紹了Android實(shí)現(xiàn)上拉加載更多功能以及下拉刷新功能的相關(guān)資料,需要的朋友可以參考下

首先為大家介紹Andorid5.0原生下拉刷新簡(jiǎn)單實(shí)現(xiàn)。

先上效果圖;

相對(duì)于上一個(gè)19.1.0版本中的橫條效果好看了很多。使用起來也很簡(jiǎn)單。

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.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);
 }

}

幾行代碼就可以實(shí)現(xiàn)下拉刷新,效果也還不錯(cuò),不用引入第三方j(luò)ar,唯一的缺憾就是沒有上拉加載,不知道谷歌工程師基于什么方面的考慮,希望能在下個(gè)版本看到。不過自己修改下源碼加一個(gè)上拉也比較簡(jiǎn)單,結(jié)合上個(gè)一個(gè)版本的刷新效果做成上拉效果還不錯(cuò)。

二、Android實(shí)現(xiàn)上拉加載更多功能以及下拉刷新功能
采用了目前比較火的PullToRefresh,他是目前實(shí)現(xiàn)比較好的下拉刷新的類庫。
目前他支持的控件有:ListView, ExpandableListView,GridView,WebView等。
首先第一步當(dāng)然是導(dǎo)入libriay到咱們的項(xiàng)目了,具體導(dǎo)入方式,這里不再贅述。
下面是個(gè)例子采用的是ListView,當(dāng)然其余的和這個(gè)類似
1、布局文件activity_main.xml

<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"
 tools:context=".MainActivity" >
 
 <com.handmark.pulltorefresh.library.PullToRefreshListView
 android:id="@+id/pull_refresh_list"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" />
</RelativeLayout>

2、要實(shí)現(xiàn)下拉刷新的功能很簡(jiǎn)單,只需要實(shí)現(xiàn)OnRefreshListener的OnRefresh方法即可。

這里說一下如何實(shí)現(xiàn)上拉和下拉分別執(zhí)行不同的操作。
原理是:根據(jù)下拉和上拉顯示的布局的可見狀態(tài)類區(qū)分上拉還是下拉,然后執(zhí)行相應(yīng)操作。
在PullToRefresh的類庫的com.handmark.pulltorefresh.library包下,打開PullToRefreshBase.java,在這個(gè)類的最后面添加如下代碼:

public boolean isHeaderShown() {

 return getHeaderLayout().isShown();

}

public boolean isFooterShown() {

 return getFooterLayout().isShown();
}

3、在Activity的代碼如下:

public class MainActivity extends ListActivity {
 
 private PullToRefreshListView mPullToRefreshListView;
 
 private LinkedList<String> mItemList;
 private ArrayAdapter<String> adapter;
 private Context context;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 context = this;
 initData();
 adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mItemList);
 //初始化控件
 mPullToRefreshListView = (PullToRefreshListView)findViewById(R.id.pull_refresh_list);
 ListView mListView = mPullToRefreshListView.getRefreshableView();
 mListView.setAdapter(adapter); 
 //設(shè)置pull-to-refresh模式為Mode.Both
 mPullToRefreshListView.setMode(Mode.BOTH);
 //設(shè)置上拉下拉事件
 mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
 
  @Override
  public void onRefresh(PullToRefreshBase<ListView> refreshView) {
  if (refreshView.isHeaderShown()){
   Toast.makeText(context, "下拉刷新",Toast.LENGTH_SHORT).show();
   //下拉刷新 業(yè)務(wù)代碼
  }else {
   Toast.makeText(context, "上拉加載更多",Toast.LENGTH_SHORT).show();
   //上拉加載更多 業(yè)務(wù)代碼

  }   

  }
 });
  
 }
 
 
 private void initData(){
 //初始化數(shù)據(jù)
 mItemList = new LinkedList<String>();
 mItemList.addAll(Arrays.asList(data));
  
 }
 
 private String[] data = new String[]{"data1","data2","data3","data4","data5","data6",
  "data1","data2","data3","data4","data5","data6"};
}

如上代碼所示,在OnRefresh的實(shí)現(xiàn)代碼中,用以區(qū)分上拉還是下拉,關(guān)鍵代碼如下:

if (refreshView.isHeaderShown()){
   Toast.makeText(context, "下拉刷新",Toast.LENGTH_SHORT).show();
   //下拉刷新 業(yè)務(wù)代碼
  }else {
   Toast.makeText(context, "上拉加載更多",Toast.LENGTH_SHORT).show();
   //上拉加載更多 業(yè)務(wù)代碼
  }

至此,運(yùn)行項(xiàng)目,可以得到演示結(jié)果了。

本文已經(jīng)被整理到《Android下拉刷新上拉加載效果》,歡迎大家學(xué)習(xí)研究。

文章內(nèi)容很豐富,希望對(duì)大家學(xué)習(xí)Android實(shí)現(xiàn)上拉加載更多以及下拉刷新功能有所幫助。

相關(guān)文章

最新評(píng)論