標籤:android 下拉重新整理 控制項
本次使用的第三方下拉重新整理控制項是:Android-Pull-Refresh,:https://github.com/chrisbanes/Android-PullToRefresh
該控制項適用於:
- ExpandableListView
- ListFragment
從github上下載解壓後,將library,PullToRefreshListFragment,PullToRefreshViewPager匯入項目後,右鍵自己建立的項目,選擇Properties,進入如的頁面後點擊Add添加即可:
添加好後即可開始編寫Demo,具體代碼如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <!-- The PullToRefreshListView replaces a standard ListView widget. --> <com.handmark.pulltorefresh.library.PullToRefreshListView xmlns:ptr="http://schemas.android.com/apk/res-auto" android:id="@+id/pull_refresh_list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:cacheColorHint="#00000000" android:divider="#19000000" android:dividerHeight="4dp" android:fadingEdge="none" android:fastScrollEnabled="false" android:footerDividersEnabled="false" android:headerDividersEnabled="false" android:smoothScrollbar="true" ptr:ptrDrawable="@drawable/ic_launcher" ptr:ptrMode="both" /></LinearLayout>
import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.os.AsyncTask;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.PullToRefreshBase.OnLastItemVisibleListener;import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;import com.handmark.pulltorefresh.library.PullToRefreshListView;/** * MainActivity---利用第三方下拉重新整理控制項實現 * @author seabear * */public class MainActivity extends Activity {private PullToRefreshListView mPullToRefreshListView;private List<String> mArrayList = new ArrayList<String>();private ArrayAdapter<String> mArrayAdapter;private int mNums = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mPullToRefreshListView = (PullToRefreshListView)this.findViewById(R.id.pull_refresh_list);mArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mArrayList);mPullToRefreshListView.setAdapter(mArrayAdapter);mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {@Overridepublic void onRefresh(PullToRefreshBase<ListView> refreshView) {// TODO Auto-generated method stubnew GetDataTask().execute();}});}private class GetDataTask extends AsyncTask<Void, Void, String>{@Overrideprotected String doInBackground(Void... params) {// Simulates a background job.mNums++;try {Thread.sleep(2000);} catch (InterruptedException e) {}return "第" + String.valueOf(mNums) + "行";}@Overrideprotected void onPostExecute(String result) {mArrayList.add(result);mArrayAdapter.notifyDataSetChanged();// Call onRefreshComplete when the list has been refreshed.mPullToRefreshListView.onRefreshComplete();super.onPostExecute(result);}}}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
android下拉重新整理控制項之第三方開原始檔控制的使用實現