본문 바로가기
프로그래밍 놀이터/안드로이드, Java

[android] RefreshListView source code.

by 돼지왕 왕돼지 2014. 1. 6.
반응형


 android, RefreshListView source code.

 


[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;

}

}

}






반응형

댓글