下拉刷新控件

用了GitHub上的开源库= = https://github.com/chrisbanes/Android-PullToRefresh

        将它下载下来,导入至工程中。一个工程(LauncherActivity)、三个库(PullToRefreshListFragment、PullToRefreshViewPager、library),导入完毕会发现有错误,这是由于它们添加的library的路径发生了改变,properties—>android—>remove错误的库—>重新add进去。

       下面就写一个工程来使用下拉刷新控件吧~

        新建工程,add这三个库,这里会发现有错误,是由于v4 jar包版本不同的原因,将它们统一错误就会消失。

主布局文件中:

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >


android:id="@+id/my_lv"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >




MainActivity.java中:

public class MainActivity extends Activity {


private PullToRefreshListView lv;
private ArrayAdapter myAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (PullToRefreshListView) findViewById(R.id.my_lv);
List list = new ArrayList();
list.add("yi");
list.add("er");
myAdapter = new ArrayAdapter(this, 
android.R.layout.simple_list_item_1, list);
lv.setAdapter(myAdapter);
 lv.setOnRefreshListener(new OnRefreshListener() {


public void onRefresh(PullToRefreshBase refreshView) {
// TODO Auto-generated method stub
new AsyncTask(){


@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
//更新UI

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
myAdapter.addAll("hello","lalala");//适配器里添加
lv.onRefreshComplete();//通知已加载好了
}
}.execute();
}
});

}
}


你可能感兴趣的:(下拉刷新控件)