android, RefreshListView source code. |
public class RefreshListView extends ListView implements OnScrollListener{
private boolean isLoading = false;
private View footer;
private RefreshListener listener;
private RefreshAdapter adapter;
public RefreshListView(Context context) {
super(context);
setOnScrollListener( this );
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
Adapter adapter = getAdapter();
if ( adapter == null ) return;
if ( adapter.getCount() == 0 ) return;
int lastItemIndex = visibleItemCount + firstVisibleItem;
if ( lastItemIndex >= totalItemCount && !isLoading ){
this.addFooterView( footer );;
isLoading = true;
listener.loadData();
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
public void setLoadingView( int resId ){
LayoutInflater inflater = (LayoutInflater) super.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
footer = (View) inflater.inflate(resId, null);
addFooterView( footer );
}
public void setRefreshListener( RefreshListener l ){
listener = l;
}
public void setAdapter(RefreshAdapter adapter) {
super.setAdapter(adapter);
this.adapter = adapter;
}
public void addNewData( List<String> data ){
this.removeFooterView( footer );
adapter.addAll( data );
adapter.notifyDataSetChanged();
isLoading = false;
}
public static interface RefreshListener{
public void loadData();
}
public static class RefreshAdapter extends BaseAdapter{
private List<String> list = new ArrayList<String>();
public void addAll( List<String> list ){
this.list.addAll( list );
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get( position );
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) convertView;
if ( textView == null ){
textView = new TextView( parent.getContext() );
textView.setPadding( 0, 50, 0, 50 );
}
textView.setText( list.get( position ) );
return textView;
}
}
}
'프로그래밍 놀이터 > 안드로이드, Java' 카테고리의 다른 글
[android] ActionBarSherlock vs. ActionBar Compatibility (0) | 2014.01.07 |
---|---|
[Java] Generic Type Tutorial. (0) | 2014.01.06 |
[android] multiple layers of defense. (0) | 2014.01.03 |
[Java] Reflection Tutorial - Dynamic Proxies (0) | 2014.01.03 |
[android] how to add comma in the EditText. (0) | 2013.12.30 |
댓글