您的位置:首页 > 其它

使用ViewSwitcher实现ListView的数据动态加载[学习]

2013-09-05 11:30 651 查看
ViewSwitcher

译者署名: ivanlee

版本:Android 2.3 r1

 

结构

继承关系

public class ViewSwitcher extends ViewAnimator

 

java.lang.Object

android.view.View

android.view.ViewGroup

android.widget.FrameLayout

android.widget.ViewAnimator

android.widget.ViewSwitcher

 

已知直接子类 ImageSwitcher, TextSwitcher

 

类概述

在两个视图间转换时显示动画,有一个可以创建这些视图的工厂类。你可以用工厂来创建这些视图,也可以自己创建。一个ViewSwitcher只允许包含两个子视图,且一次仅能显示一个。 (译者注:与ViewFlipper类相似,但该类不常用,常用其两个子类ImageSwitcher:转换图片时增加动画效果; TextSwitcher: 转换文字时增加动画效果; 其实例见apidemos中ImageSwitcher实例和TextSwitcher实例)

 

内部类 interface ViewSwitcher.ViewFactory

在一个ViewSwitcher里创建视图

 

构造函数

public ViewSwitcher (Context context)

构造一个新的空的视图转换器(ViewSwitcher)。

参数

context 应用环境(译者注:应用程序上下文)

 

public ViewSwitcher (Context context, AttributeSet attrs)

构造一个指定上下文、属性集合的空的视图转换器(ViewSwitcher)。

参数

context 应用环境(译者注:应用程序上下文)

attrs 属性集合

 

公共方法

public void addView(View child, int index, ViewGroup.LayoutParams params)

添加一个指定布局参数的子视图

参数

child 添加的子视图

index 添加的子视图的索引

params 子视图的布局参数

异常

IllegalStateException 如果切换器中已经包含了两个视图时。

 

public View getNextView ()

返回下一个要显示的视图

返回

视图切换之后将要显示出的下一个视图

 

public void reset ()

重置视图转换器(ViewSwitcher)来隐藏所有存在的视图,并使转换器达到一次动画都还没有播放的状态。

 

public void setFactory (ViewSwitcher.ViewFactory factory)

设置用来生成将在视图转换器中切换的两个视图的工厂。也可以调用两次 addView(android.view.View, int, android.view.ViewGroup.LayoutParams)来替代使用工厂的方法。

参数

factory 用来生成转换器内容的视图工厂

 

实现ListView的数据动态加载DEMO:

代码:

load_more_items.xml:

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/btn_loadmorecontacts"
android:text="Load More Items"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textColor="#FFFFFF"
android:background="@android:drawable/list_selector_background"
android:clickable="true"
android:onClick="onClick" />


 

progress_bar.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:minHeight="?android:attr/listPreferredItemHeight">

<ProgressBar
android:id="@+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true" />

<TextView
android:text="Loading…"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="@+id/progressbar"
android:layout_centerVertical="true"
android:gravity="center"
android:padding="10dip"
android:textColor="#FFFFFF" />
</RelativeLayout>

 

ViewSwitcherExample.java:

/**
* ViewSwitcher 字面意思理解为视图转换开关.
* 在两个视图间转换时显示动画,有一个可以创建这些视图的工厂类。你可以用工厂来创建这些视图,也可以自己创建。
* 一个ViewSwitcher只允许包含两个子视图,且一次仅能显示一个。
*/
public class ViewSwitcherExample extends ListActivity implements OnClickListener {

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"};

private static final String TAG = "ViewSwitcherExample";

private ViewSwitcher switcher;

@Override
protected 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
View footer = View.inflate(this, R.layout.load_more_items, null);
//progress View: see XML2
View processBar = View.inflate(this, R.layout.progress_bar, null);
//add the views (first added will show first)
switcher.addView(footer);
switcher.addView(processBar);
//add the ViewSwitcher to the footer
getListView().addFooterView(switcher);
//add items to the ListView
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ITEMS));

}

@Override
public void onClick(View v) {
//first view is showing, show the second progress view
switcher.showNext();  //显示下一个视图
//and start background work
new getMoreItems().execute();
Log.i(TAG, Thread.currentThread().getName());
}

/** Background Task To Get More Items**/
private class getMoreItems extends AsyncTask{

@Override
protected Object doInBackground(Object... params) {
//code to add more items
try {
Thread.sleep(3000); //only to demonstrate
Log.i(TAG, Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Object result) {
//返回上一个视图
switcher.showPrevious();
//update the ListView
}

}

}


 

 

效果图:模拟点击LoadMoreItems按钮或得更多数据。

 



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: