android 下拉刷新菜单库 johannilsson/android-pulltorefresh

本文介绍了一个使用PullToRefreshListView实现下拉刷新功能的简单示例应用。通过MainActivity的源代码展示如何设置布局、初始化组件及监听刷新事件,并在刷新时通过异步任务更新列表数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
>
  </com.handmark.pulltorefresh.library.PullToRefreshListView> 
</RelativeLayout>


MainActivity.java

package com.example.pulltorefreshlistviewjike;



import java.util.ArrayList;
import java.util.List;


import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;


import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(PullToRefreshListView) findViewById(R.id.listView);
List<String> arr=new ArrayList<String>();
arr.add("hello,word");
arr.add("im coming");
adpter=new  ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arr);
lv.setAdapter(adpter);
lv.setOnRefreshListener(new OnRefreshListener<ListView>(){


@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
// TODO Auto-generated method stub
new AsyncTask<Void,Void,Void>(){


@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try{
Thread.sleep(3000);
}catch(InterruptedException e){
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result){
adpter.addAll("helo","大家好");
lv.onRefreshComplete();
};
}.execute();
}

});
}

private PullToRefreshListView lv;
private ArrayAdapter<String> adpter;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}

github托管地址:https://github.com/johannilsson/android-pulltorefresh


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值