Android下拉/上拉刷新ListView之Android-PullToRefresh
发布时间:2020-05-24 20:19:43 所属栏目:Java 来源:互联网
导读:Android下拉/上拉刷新ListView之Android-PullToRefresh
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 import java.util.LinkedList;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import android.support.v7.app.ActionBarActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.os.Bundle;
import android.os.Handler;
public class MainActivity extends ActionBarActivity {
private PullToRefreshListView mPullRefreshListView;
private LinkedList<String> mListItems;
private ArrayAdapter<String> mAdapter;
// 数据
private int DATA = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
// Mode.BOTH:支持下拉和上拉刷新。
mPullRefreshListView.setMode(Mode.BOTH);
mPullRefreshListView
.setOnRefreshListener(new OnRefreshListener2<ListView>() {
// 下拉
@Override
public void onPullDownToRefresh(
PullToRefreshBase<ListView> refreshView) {
Toast.makeText(getApplicationContext(),"下拉刷新",Toast.LENGTH_SHORT).show();
addItem();
}
// 上拉
@Override
public void onPullUpToRefresh(
PullToRefreshBase<ListView> refreshView) {
Toast.makeText(getApplicationContext(),"上拉刷新",Toast.LENGTH_SHORT).show();
addItem();
}
});
// 列表到底,即看到最后一个元素。
mPullRefreshListView
.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
@Override
public void onLastItemVisible() {
Toast.makeText(getApplication(),"已经到底!",Toast.LENGTH_SHORT).show();
}
});
ListView actualListView = mPullRefreshListView.getRefreshableView();
mListItems = new LinkedList<String>();
mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mListItems);
actualListView.setAdapter(mAdapter);
}
// 添加数据
private void addItem() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mListItems.add((DATA++) + "");
mAdapter.notifyDataSetChanged();
mPullRefreshListView.onRefreshComplete();
}
},1000);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/pull_refresh_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@android:color/black"
android:dividerHeight="1dip"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:smoothScrollbar="true" />
</RelativeLayout>
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
