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

Android開源項(xiàng)目PullToRefresh下拉刷新功能詳解2

 更新時(shí)間:2016年09月22日 09:37:13   作者:Developer_Kale  
這篇文章主要為大家進(jìn)一步的介紹了Android開源項(xiàng)目PullToRefresh下拉刷新功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

先看看效果圖:

這里介紹的是PullToRefreshGridView的使用方法,和之前的PullToRefreshListView方法如出一轍,因?yàn)檫@個(gè)開源項(xiàng)目模塊化很棒,所以很容易實(shí)現(xiàn)。等于說我們可以按照之前使用控件的方式來操作,不用考慮其他的問題。 

思路: 

1.寫布局文件,放入可以下拉刷新的控件 
2.找到下拉刷新的控件,設(shè)置監(jiān)聽器,并且在刷新方法中開啟一個(gè)異步任務(wù)來操作 
3.通過這個(gè)下拉刷新控件的getRefreshableView()方法來得到GridView對(duì)象,按照正常的操作來設(shè)置適配器 
4.在異步任務(wù)中通過LinkedList來給頭部或者是尾部添加新的數(shù)據(jù) 

實(shí)現(xiàn): 

1.布局文件 

我們可以看到,我們?nèi)耘f可以像使用GridView一樣,定義GridView的屬性。當(dāng)然可以通過ptr:命名空間來設(shè)置專屬屬性 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" >

<!-- The PullToRefreshGridView replaces a standard GridView widget. -->
 <com.handmark.pulltorefresh.library.PullToRefreshGridView
  xmlns:ptr="http://schemas.android.com/apk/res-auto"
  android:id="@+id/pull_refresh_grid"
  android:layout_height="fill_parent"
  android:layout_width="fill_parent"
  android:numColumns="auto_fit"
  android:verticalSpacing="1dp"
  android:horizontalSpacing="1dp"
  android:columnWidth="100dp"
  android:stretchMode="columnWidth"
  android:gravity="fill"
  ptr:ptrMode="both"
  ptr:ptrDrawable="@drawable/ic_launcher" />

</LinearLayout>

2.找到這個(gè)可以下拉刷新的控件,并且設(shè)置監(jiān)聽器 

這里的監(jiān)聽器和上篇文章講的不同,是雙向的。所以很方便監(jiān)聽滑動(dòng)操作! 

 /**
  *設(shè)置下拉刷新的view,設(shè)置雙向監(jiān)聽器 
  */
 private void initPTRGrideView() {
  // 得到下拉刷新的GridView
  mPullRefreshGridView = (PullToRefreshGridView) findViewById(R.id.pull_refresh_grid);
  // 設(shè)置監(jiān)聽器,這個(gè)監(jiān)聽器是可以監(jiān)聽雙向滑動(dòng)的,這樣可以觸發(fā)不同的事件
  mPullRefreshGridView.setOnRefreshListener(new OnRefreshListener2<GridView>() {

   @Override
   public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
    Toast.makeText(getApplicationContext(), "下拉", Toast.LENGTH_SHORT).show();
    new GetDataTask(mPullRefreshGridView, mAdapter, mListItems).execute();
   }

   @Override
   public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
    Toast.makeText(getApplicationContext(), "上拉", Toast.LENGTH_SHORT).show();
    new GetDataTask(mPullRefreshGridView, mAdapter, mListItems).execute();
   }

  });
 }

3.找到GridView來進(jìn)行適配器的設(shè)置 

 //鏈表數(shù)組對(duì)象,用來方便添加string對(duì)象
 private LinkedList<String> mListItems;
 //用來下拉刷新的控件
 private PullToRefreshGridView mPullRefreshGridView;
 //真正用到的控件,它被隱含到PullToRefreshGridView中,所以需要找出來才能使用
 private GridView mGridView;
 //定義GridView的適配器
 private ArrayAdapter<String> mAdapter;

這里也可以設(shè)置適配器中無數(shù)據(jù)時(shí)顯示的內(nèi)容,調(diào)用的方法是:setEmptyView() 

 /**
  * 設(shè)置GridView,首先找到它,然后設(shè)置適配器
  */
 private void initGrideView() {
  mGridView = mPullRefreshGridView.getRefreshableView();
  //定義String數(shù)組,然后把它放到LinkedList中,之后只要在異步任務(wù)中用LinkedList就可以添加開頭和結(jié)尾的數(shù)據(jù)了
  String []data = new String[] {"android","ios","wp","java","c++","c#"};
  mListItems = new LinkedList<String>();
  mListItems.addAll(Arrays.asList(data));

  //當(dāng)適配器中沒有數(shù)據(jù)的時(shí)候顯示的東西,這里因?yàn)槲医o適配器中填充了string數(shù)組,所以不會(huì)顯示“這里很空,下拉刷新試試”
  TextView tv = new TextView(this);
  tv.setGravity(Gravity.CENTER);
  tv.setText("這里很空,下拉刷新試試");
  //當(dāng)界面為空的時(shí)候顯示的視圖
  mPullRefreshGridView.setEmptyView(tv);

  //設(shè)置適配器
  mAdapter = new ArrayAdapter<String>(this, 
    android.R.layout.simple_list_item_1, mListItems);
  mGridView.setAdapter(mAdapter);
 }

4.執(zhí)行異步任務(wù),模擬加載數(shù)據(jù),這個(gè)和之前的寫法一樣 

package com.kale.ptrgridview;

import java.util.LinkedList;

import android.os.AsyncTask;
import android.widget.ArrayAdapter;

import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;

/**
 * @author:Jack Tony
 * @tips :通過異步任務(wù)來加載網(wǎng)絡(luò)中的數(shù)據(jù),進(jìn)行更新
 * @date :2014-10-14
 */
public class GetDataTask extends AsyncTask<Void, Void, Void>{

 private PullToRefreshGridView mPullRefreshGridView;
 private ArrayAdapter<String> mAdapter;
 private LinkedList<String> mListItems;
 
 
 public GetDataTask(PullToRefreshGridView gridView,
   ArrayAdapter<String> adapter,LinkedList<String> listItems) {
  // TODO 自動(dòng)生成的構(gòu)造函數(shù)存根
  mPullRefreshGridView = gridView;
  mAdapter = adapter;
  mListItems = listItems;
 }
 
 @Override
 protected Void doInBackground(Void... params) {
  //模擬請(qǐng)求,舒眠2秒鐘
  try {
   Thread.sleep(2000);
  } catch (InterruptedException e) {
  }
  return null;
 }
 
 @Override
 protected void onPostExecute(Void result) {
  // TODO 自動(dòng)生成的方法存根
  super.onPostExecute(result);
  
  //得到當(dāng)前的模式,來判斷數(shù)據(jù)應(yīng)該加載到哪個(gè)位置
  Mode mode = mPullRefreshGridView.getCurrentMode();
  if(mode == Mode.PULL_FROM_START) {
   mListItems.addFirst("這是刷新出來的數(shù)據(jù)");
  }
  else {
   mListItems.addLast("這是刷新出來的數(shù)據(jù)");
  }
  // 通知數(shù)據(jù)改變了
  mAdapter.notifyDataSetChanged();
  // 加載完成后停止刷新
  mPullRefreshGridView.onRefreshComplete();
  
 }
 


}

MainActivity.JAVA中的全部代碼 

package com.kale.ptrgridview;

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

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;

public class MainActivity extends Activity {
 
 //鏈表數(shù)組對(duì)象,用來方便添加string對(duì)象
 private LinkedList<String> mListItems;
 //用來下拉刷新的控件
 private PullToRefreshGridView mPullRefreshGridView;
 //真正用到的控件,它被隱含到PullToRefreshGridView中,所以需要找出來才能使用
 private GridView mGridView;
 //定義GridView的適配器
 private ArrayAdapter<String> mAdapter;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  initView();
 }

 private void initView() {
  // TODO 自動(dòng)生成的方法存根
  initPTRGrideView();
  initGrideView();
 }

 /**
  *設(shè)置下拉刷新的view,設(shè)置雙向監(jiān)聽器 
  */
 private void initPTRGrideView() {
  // 得到下拉刷新的GridView
  mPullRefreshGridView = (PullToRefreshGridView) findViewById(R.id.pull_refresh_grid);
  // 設(shè)置監(jiān)聽器,這個(gè)監(jiān)聽器是可以監(jiān)聽雙向滑動(dòng)的,這樣可以觸發(fā)不同的事件
  mPullRefreshGridView.setOnRefreshListener(new OnRefreshListener2<GridView>() {

   @Override
   public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
    Toast.makeText(getApplicationContext(), "下拉", Toast.LENGTH_SHORT).show();
    new GetDataTask(mPullRefreshGridView, mAdapter, mListItems).execute();
   }

   @Override
   public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
    Toast.makeText(getApplicationContext(), "上拉", Toast.LENGTH_SHORT).show();
    new GetDataTask(mPullRefreshGridView, mAdapter, mListItems).execute();
   }

  });
 }
 
 /**
  * 設(shè)置GridView,首先找到它,然后設(shè)置適配器
  */
 private void initGrideView() {
  mGridView = mPullRefreshGridView.getRefreshableView();
  //定義String數(shù)組,然后把它放到LinkedList中,之后只要在異步任務(wù)中用LinkedList就可以添加開頭和結(jié)尾的數(shù)據(jù)了
  String []data = new String[] {"android","ios","wp","java","c++","c#"};
  mListItems = new LinkedList<String>();
  mListItems.addAll(Arrays.asList(data));

  //當(dāng)適配器中沒有數(shù)據(jù)的時(shí)候顯示的東西,這里因?yàn)槲医o適配器中填充了string數(shù)組,所以不會(huì)顯示“這里很空,下拉刷新試試”
  TextView tv = new TextView(this);
  tv.setGravity(Gravity.CENTER);
  tv.setText("這里很空,下拉刷新試試");
  //當(dāng)界面為空的時(shí)候顯示的視圖
  mPullRefreshGridView.setEmptyView(tv);

  //設(shè)置適配器
  mAdapter = new ArrayAdapter<String>(this, 
    android.R.layout.simple_list_item_1, mListItems);
  mGridView.setAdapter(mAdapter);
 }
 
 
}

源碼下載:http://xiazai.jb51.net/201609/yuanma/AndroidGridView(jb51.net).rar

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論