Android中ListView上拉刷新的功能实现(总结)
程序员文章站
2024-03-21 18:07:28
...
效果图如下:
1、自定义一个LoadMoreListView继承自ListVIew,同时提供加载接口
(原理就是通过对FooterView的控制实现上划加载的效果)
代码如下:
package com.crazyboy.glideshowimagedemo;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
public class LoadMoreListView extends ListView {
private Context mContext;
private View mLoadMoreFooter;
private OnLoadMoreListener mLoadMoreListener;
private boolean mAddedFooter;
private boolean mLoadMore;
public interface OnLoadMoreListener extends OnScrollListener {
// 加载更多数据
void onLoadMoreData();
}
public LoadMoreListView(Context context) {
super(context);
mContext = context;
init();
}
public LoadMoreListView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
init();
}
public LoadMoreListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
init();
}
private void init() {
mLoadMoreFooter = LayoutInflater.from(mContext).inflate(R.layout.load_more_view, null);
this.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (mLoadMoreListener != null) {
mLoadMoreListener.onScrollStateChanged(view, scrollState);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (mLoadMoreListener != null) {
mLoadMoreListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
// 滑动到底部显示LoadingMore
if (firstVisibleItem + visibleItemCount == totalItemCount && mLoadMore) {
showLoadMoreView();
// 加载耗时操作
post(new Runnable() {
@Override
public void run() {
mLoadMoreListener.onLoadMoreData();
mLoadMore = false;
}
});
}
}
}
});
}
// 加载数据完成
public void loadMoreComplete() {
deferNotifyDataSetChanged();
setLoadMore(false);
}
public void setLoadMore(boolean loadMore) {
mLoadMore = loadMore;
if (!loadMore) {
removeFooterView(mLoadMoreFooter);
}
}
private void showLoadMoreView() {
if (!mAddedFooter) {
addFooterView(mLoadMoreFooter);
mAddedFooter = true;
}
}
public void setLoadMoreListener(OnLoadMoreListener loadMoreListener) {
this.mLoadMoreListener = loadMoreListener;
}
}
2、在相应的Activity中调用并实现加载接口。代码如下:
package com.crazyboy.glideshowimagedemo;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.AbsListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Context mContext;
private List<Tb_NewsInfo> tbNewsInfoList;
private Tb_NewsInfo tb_newsInfo;
private NewsListAdapter newsListAdapter;
private LoadMoreListView LMLVNewsList;
private String[] urlStrs = new String[]{ "http://pic14.photophoto.cn/20100127/0036036848818577_b.jpg"
, "http://img.pconline.com.cn/images/upload/upc/tx/wallpaper/1209/11/c0/13783009_1347330674323.jpg"
, "http://pic9.photophoto.cn/20081105/0034034891195212_b.jpg"
, "http://pic9.photophoto.cn/20081105/0034034891195212_b.jpg", "http://pic9.photophoto.cn/20081105/0034034891195212_b.jpg"
, "http://pic9.photophoto.cn/20081105/0034034891195212_b.jpg", "http://pic9.photophoto.cn/20081105/0034034891195212_b.jpg"
, "http://pic9.photophoto.cn/20081105/0034034891195212_b.jpg", "http://pic9.photophoto.cn/20081105/0034034891195212_b.jpg"
, "http://pic9.photophoto.cn/20081105/0034034891195212_b.jpg", "http://pic9.photophoto.cn/20081105/0034034891195212_b.jpg"
, "http://pic9.photophoto.cn/20081105/0034034891195212_b.jpg", "http://pic9.photophoto.cn/20081105/0034034891195212_b.jpg"
, "http://nba.sports.qq.com/media/img/players/head/260x190/201935.png"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//判断android版本号,弹出申请权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
showConfirmAppPermissions();
}
bindBaseInfoInit();
bindView();
setViewBaseState();
}
private void bindBaseInfoInit() {
mContext = this;
tbNewsInfoList = new ArrayList<>();
}
private void bindView() {
LMLVNewsList = findViewById(R.id.main_LMLVNewsList);
}
private void setViewBaseState() {
for (int i = 0; i< urlStrs.length; i++) {
String title = "这是一条新闻的标题内容";
tb_newsInfo = new Tb_NewsInfo(urlStrs[i], title);
tbNewsInfoList.add(tb_newsInfo);
}
newsListAdapter = new NewsListAdapter(mContext, tbNewsInfoList);
LMLVNewsList.setAdapter(newsListAdapter);
LMLVNewsList.setLoadMore(true);
LMLVNewsList.setLoadMoreListener(new LoadMoreListView.OnLoadMoreListener() {
@Override
public void onLoadMoreData() {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i< urlStrs.length; i++) {
String title = "这是一条新闻的标题内容";
String url1 = "http://pic14.photophoto.cn/20100127/0036036848818577_b.jpg";
tb_newsInfo = new Tb_NewsInfo(url1, title);
tbNewsInfoList.add(tb_newsInfo);
}
LMLVNewsList.post(new Runnable() {
@Override
public void run() {
LMLVNewsList.loadMoreComplete();
}
});
}
}).start();
}
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {
}
@Override
public void onScroll(AbsListView absListView, int i, int i1, int i2) {
}
});
}
// 7.0动态申请权限
public void showConfirmAppPermissions() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);
}
}
}
}
上一篇: Centos7安装docker
推荐阅读
-
Android中ListView上拉刷新的功能实现(总结)
-
3年以上勿进!最简单的Android自定义ListView下拉刷新与上拉加载,代码直接拿去用~
-
Android RecyclerView 上拉加载更多及下拉刷新功能的实现方法
-
Android使用PullToRefresh实现上拉加载和下拉刷新效果的代码
-
Android RecyclerView 上拉加载更多及下拉刷新功能的实现方法
-
Android开发ListView中下拉刷新上拉加载及带列的横向滚动实现方法
-
Android App使用RecyclerView实现上拉和下拉刷新的方法
-
Android使用PullToRefresh实现上拉加载和下拉刷新效果的代码
-
Android开发ListView中下拉刷新上拉加载及带列的横向滚动实现方法
-
Android App使用RecyclerView实现上拉和下拉刷新的方法