public class ViewSwitcherExample extends ListActivity
 implements OnClickListener {
    
//sample list items
static final String[] ITEMS = new String[]
          { "List Item 1", "List Item 2", 
            "List Item 3", "List Item 4", 
            "List Item 5", "List Item 6", 
            "List Item 7", "List Item 8", 
            "List Item 9", "List Item 10" };
//the ViewSwitcher
private ViewSwitcher switcher;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  
  //no window title
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  
  //create the ViewSwitcher in the current context
  switcher = new ViewSwitcher(this);
  
  //footer Button: see XML1
  Button footer = (Button)View.inflate(this, R.layout.btn_loadmore, null);
  
  //progress View: see XML2
  View progress = View.inflate(this, R.layout.loading_footer, null);
  
  //add the views (first added will show first)
  switcher.addView(footer);
  switcher.addView(progress);
  
  //add the ViewSwitcher to the footer
  getListView().addFooterView(switcher);
  
  //add items to the ListView
  setListAdapter(new ArrayAdapter(this,
          android.R.layout.simple_list_item_1, ITEMS));
}

@Override /* Load More Button Was Clicked */
public void onClick(View arg0) {
//first view is showing, show the second progress view
switcher.showNext();
//and start background work
new getMoreItems().execute();
}
/** Background Task To Get More Items**/
private class getMoreItems extends AsyncTask {
@Override
protected Object doInBackground(Void… params) {
//code to add more items
//...
try {
Thread.sleep(3000); //only to demonstrate
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}

@Override /* Background Task is Done */
protected void onPostExecute(Object result) {
//go back to the first view
switcher.showPrevious();
                        //update the ListView
}
}
}

'안드로이드' 카테고리의 다른 글

AndroidManifest  (0) 2011.01.26
Permission List  (0) 2011.01.26
안드로이드 날짜 포맷  (0) 2011.01.26
List View 구현시 유의할 점.  (0) 2011.01.26
화면 회전  (0) 2011.01.26

+ Recent posts