PullToRefresh(刷新加載)

/////////////////////////////////////////////  MainActivity    ///////////////////////////////////////////////////////

import java.util.ArrayList;

import java.util.List;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.widget.ArrayAdapter;
import android.widget.GridView;


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


public class MainActivity extends Activity {
private Context context;
private PullToRefreshGridView gridView;
private List<String> list;
private ArrayAdapter<String> adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;

init();
}


private void init() {
gridView = (PullToRefreshGridView) findViewById(R.id.myGridView);

list = setData("商品--");

adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1,list);

gridView.setAdapter(adapter);

gridView.setOnRefreshListener(new OnRefreshListener2<GridView>() {


@Override
public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
// label 標籤,標記
String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),  
                       DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);  


List<String> list2 = setData("刷新--");
list.addAll(0,list2);
ArrayAdapter<String> A = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1,list);
gridView.setAdapter(A);
gridView.onRefreshComplete();
}


@Override
public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
List<String> list3 = setData("加載--");
list.addAll(list3);
ArrayAdapter<String> A = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1,list);
gridView.setAdapter(A);
gridView.onRefreshComplete();
}
});


}



private List<String> setData(String str) {
List<String> list = new ArrayList<String>();
for(int i=0;i<30;i++){
list.add(str+i);
}
return list;
}

}

/////////////////////////////////////////////  main_ctivity    ///////////////////////////////////////////////////////

<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.PullToRefreshGridView
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/myGridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="3"
        ptr:ptrDrawable="@drawable/ic_launcher"  
        ptr:ptrMode="both"
          />


     
</RelativeLayout>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章