欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 仿硅谷新闻下拉刷新/上拉加载更多

程序员文章站 2024-03-02 23:30:46
1.添加加载更多布局 1_初始化和隐藏代码 在refreshlistview构造方法中调用 private void initfooterview(con...

1.添加加载更多布局

1_初始化和隐藏代码

在refreshlistview构造方法中调用

private void initfooterview(context context) {
view footerview = view.inflate(context, r.layout.refresh_listview_footer, null);
//隐藏代码
footerview.measure(0, 0);
int footerviewheight = footerview.getmeasuredheight();
footerview.setpadding(0, -footerviewheight, 0, 0);
this.addfooterview(footerview);
}

2_布局文件refresh_listview_footer.xml

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<progressbar
android:layout_margin="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminatedrawable="@drawable/custom_progressbar" />
<textview
android:layout_marginleft="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加载更多中..."
android:textcolor="#ff0000"
android:textsize="25sp" />
</linearlayout>

2.拖动到底部的时候

/**
* 菜单页面对应的新闻页签页面
* 总共有12个
* @author administrator
*
*/
public class tabmenudetailpager extends menudetailbasepagerimplements onpagechangelistener {
/**
* 新闻中心-新闻菜单对应的标签对应的数据
*/
private newcentertag newcentertag;
.......................
/**
* 加载更多数据的url
*/
private string moreurl;
/**
* 是否加载更多数据中
*/
protected boolean isloadingmore = false;
...................
@override
public view initview() {
view view = view.inflate(mactivity, r.layout.tab_detail, null);
//把view注入到xutils框架中
viewutils.inject(this, view); 
.......................... 
//设置监听下拉刷新
mlistview.setonrefreshlistener(new onrefreshlistener() {
@override
public void onpulldownrefresh() {
ispulldownrefreshing = true;
getdatafromnet();
}
@override
public void onloadingmore() {
if(textutils.isempty(moreurl)){
toast.maketext(mactivity, "没有更多数据了", 1).show();
mlistview.onrefreshfinish(false);
}else{
//有更多数据,要加载更多数据了
getmoredatafromnet();
}
}
});
return view;
}
/**
* 加载更多数据
*/
protected void getmoredatafromnet() {
httputils httputils = new httputils();
httputils.send(httpmethod.get, moreurl, new requestcallback<string>() {
@override
public void onsuccess(responseinfo<string> responseinfo) {
system.out.println("加载更多数据成功:"+responseinfo.result);
mlistview.onrefreshfinish(false);
isloadingmore = true;
processdata(responseinfo.result);
}
@override
public void onfailure(httpexception error, string msg) {
mlistview.onrefreshfinish(false);
system.out.println("加载更多数据失败:"+ msg);
}
}); 
}
/**
* 处理和解析json数据
* @param json
*/
protected void processdata(string json) {
tabdetailbean bean = parserjson(json);
if(!isloadingmore){
system.out.println(bean.data.news.get(0).title);
topnews = bean.data.topnews;
//给viewpager设置适配器
tabdetailadapter adapter = new tabdetailadapter();
mviewpager.setadapter(adapter);
// 把所有的view清除
ll_point_group.removeallviews();
for(int i=0;i<topnews.size();i++){
view point = new view(mactivity);
layoutparams params = new layoutparams(5, 5) ;
point.setbackgroundresource(r.drawable.tab_detail_point_bg);
if(i!=0){
params.leftmargin = 10;
}
point.setenabled(false);
point.setlayoutparams(params);
ll_point_group.addview(point);
}
previouspointposition = 0;
//设置默认的图片描述和指示点
mtv_title_description.settext(topnews.get(previouspointposition).title);
ll_point_group.getchildat(previouspointposition).setenabled(true);
//设置页面改变的监听
mviewpager.setonpagechangelistener(this);
//设置适配器和对应的数据
newslists = bean.data.news;
listviewadapter = new listviewadapter();
mlistview.setadapter(listviewadapter);
// mlistview.addheaderview(v) ;//把一个视图一头的方式添加到listview中 
}else{
//把列表新闻取出来,在加载到以前的集合中,在刷新数据
isloadingmore = false;
list<news>moredatanews = bean.data.news;
newslists.addall(moredatanews);
listviewadapter.notifydatasetchanged();//刷新数据
}
}
................
/**
* 用gson开源项目解析json
* @param json
*/
private tabdetailbean parserjson(string json) {
gson gson = new gson();
tabdetailbean bean = gson.fromjson(json, tabdetailbean.class);
moreurl = bean.data.more;
if(textutils.isempty(moreurl)){
moreurl = null;
}else{
moreurl = constantutils.server_url+moreurl;
}
return bean;
}
@override
public void onpagescrollstatechanged(int arg0) {
// todo auto-generated method stub
}
@override
public void onpagescrolled(int arg0, float arg1, int arg2) {
// todo auto-generated method stub
}
...............
}

3.完整代码

package com.atguigu.refreshlistview;
import android.content.context;
import android.util.attributeset;
import android.view.motionevent;
import android.view.view;
import android.view.animation.animation;
import android.view.animation.rotateanimation;
import android.widget.abslistview;
import android.widget.imageview;
import android.widget.linearlayout;
import android.widget.listview;
import android.widget.progressbar;
import android.widget.textview;
import java.text.simpledateformat;
import java.util.date;
/**
* 作用:自定义下拉刷新的listview
*/
public class refreshlistview extends listview {
/**
* 下拉刷新和顶部轮播图
*/
private linearlayout headerview;
/**
* 下拉刷新控件
*/
private view ll_pull_down_refresh;
private imageview iv_arrow;
private progressbar pb_status;
private textview tv_status;
private textview tv_time;
/**
* 下拉刷新控件的高
*/
private int pulldownrefreshheight;
/**
* 下拉刷新
*/
public static final int pull_down_refresh = 0;
/**
* 手松刷新
*/
public static final int release_refresh = 1;
/**
* 正在刷新
*/
public static final int refreshing = 2;
/**
* 当前状态
*/
private int currentstatus = pull_down_refresh;
private animation upanimation;
private animation downanimation;
/**
* 加载更多的控件
*/
private view footerview;
/**
* 加载更多控件高
*/
private int footerviewheight;
/**
* 是否已经加载更多
*/
private boolean isloadmore = false;
/**
* 顶部轮播图部分
*/
private view topnewsview;
/**
* listview在y轴上的坐标
*/
private int listviewonscreeny = -1;
public refreshlistview(context context) {
this(context, null);
}
public refreshlistview(context context, attributeset attrs) {
this(context, attrs, 0);
}
public refreshlistview(context context, attributeset attrs, int defstyleattr) {
super(context, attrs, defstyleattr);
initheaderview(context);
initanimation();
initfooterview(context);
}
private void initfooterview(context context) {
footerview = view.inflate(context, r.layout.refresh_footer, null);
footerview.measure(0, 0);
footerviewheight = footerview.getmeasuredheight();
footerview.setpadding(0, -footerviewheight, 0, 0);
//listview添加footer
addfooterview(footerview);
//监听listview的滚动
setonscrolllistener(new myonscrolllistener());
}
/**
* 添加顶部轮播图
* @param topnewsview
*/
public void addtopnewsview(view topnewsview) {
if(topnewsview != null){
this.topnewsview =topnewsview;
headerview.addview(topnewsview);
}
}
class myonscrolllistener implements onscrolllistener{
@override
public void onscrollstatechanged(abslistview view, int scrollstate) {
//当静止或者惯性滚动的时候
if(scrollstate ==onscrolllistener.scroll_state_idle||scrollstate ==onscrolllistener.scroll_state_fling){
//并且是最后一条可见
if(getlastvisibleposition()>=getcount()-1){
//1.显示加载更多布局
footerview.setpadding(8,8,8,8);
//2.状态改变
isloadmore = true;
//3.回调接口
if(monrefreshlistener != null){
monrefreshlistener.onloadmore();
}
}
}
}
@override
public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) {
}
}
private void initanimation() {
upanimation = new rotateanimation(0, -180, rotateanimation.relative_to_self, 0.5f, rotateanimation.relative_to_self, 0.5f);
upanimation.setduration(500);
upanimation.setfillafter(true);
downanimation = new rotateanimation(-180, -360, rotateanimation.relative_to_self, 0.5f, rotateanimation.relative_to_self, 0.5f);
downanimation.setduration(500);
downanimation.setfillafter(true);
}
private void initheaderview(context context) {
headerview = (linearlayout) view.inflate(context, r.layout.refresh_header, null);
//下拉刷新控件
ll_pull_down_refresh = headerview.findviewbyid(r.id.ll_pull_down_refresh);
iv_arrow = (imageview) headerview.findviewbyid(r.id.iv_arrow);
pb_status = (progressbar) headerview.findviewbyid(r.id.pb_status);
tv_status = (textview) headerview.findviewbyid(r.id.tv_status);
tv_time = (textview) headerview.findviewbyid(r.id.tv_time);
//测量
ll_pull_down_refresh.measure(0, 0);
pulldownrefreshheight = ll_pull_down_refresh.getmeasuredheight();
//默认隐藏下拉刷新控件
// view.setpadding(0,-控件高,0,0);//完全隐藏
//view.setpadding(0, 0,0,0);//完全显示
ll_pull_down_refresh.setpadding(0, -pulldownrefreshheight, 0, 0);
//添加listview的头
addheaderview(headerview);
}
private float starty = -1;
@override
public boolean ontouchevent(motionevent ev) {
switch (ev.getaction()) {
case motionevent.action_down:
//1.记录起始坐标
starty = ev.gety();
break;
case motionevent.action_move:
if (starty == -1) {
starty = ev.gety();
}
//判断顶部轮播图是否完全显示,只有完全显示才会有下拉刷新
boolean isdisplaytopnews = isdisplaytopnews();
if(!isdisplaytopnews){
//加载更多
break;
}
//如果是正在刷新,就不让再刷新了
if (currentstatus == refreshing) {
break;
}
//2.来到新的坐标
float endy = ev.gety();
//3.记录滑动的距离
float distancey = endy - starty;
if (distancey > 0) {//下拉
//int paddingtop = -控件高 + distancey;
int paddingtop = (int) (-pulldownrefreshheight + distancey);
if (paddingtop < 0 && currentstatus != pull_down_refresh) {
//下拉刷新状态
currentstatus = pull_down_refresh;
//更新状态
refreshviewstate();
} else if (paddingtop > 0 && currentstatus != release_refresh) {
//手松刷新状态
currentstatus = release_refresh;
//更新状态
refreshviewstate();
}
ll_pull_down_refresh.setpadding(0, paddingtop, 0, 0);
//view.setpadding(0,paddingtop,0,0);//动态的显示下拉刷新控件
}
break;
case motionevent.action_up:
starty = -1;
if (currentstatus == pull_down_refresh) {
// view.setpadding(0,-控件高,0,0);//完全隐藏
ll_pull_down_refresh.setpadding(0, -pulldownrefreshheight, 0, 0);
} else if (currentstatus == release_refresh) {
//设置状态为正在刷新
currentstatus = refreshing;
refreshviewstate();
// view.setpadding(0,0,0,0);//完全显示
ll_pull_down_refresh.setpadding(0, 0, 0, 0);
//回调接口
if (monrefreshlistener != null) {
monrefreshlistener.onpulldownrefresh();
}
}
break;
}
return super.ontouchevent(ev);
}
/**
* 判断是否完全显示顶部轮播图
* 当listview在屏幕上的y轴坐标小于或者等于顶部轮播图在y轴的坐标的时候,顶部轮播图完全显示
* @return
*/
private boolean isdisplaytopnews() {
if(topnewsview != null){
//1.得到listview在屏幕上的坐标
int[] location = new int[2];
if(listviewonscreeny == -1){
getlocationonscreen(location);
listviewonscreeny = location[1];
}
//2.得到顶部轮播图在屏幕上的坐标
topnewsview.getlocationonscreen(location);
int topnewsviewonscreeny = location[1];
// if(listviewonscreeny <= topnewsviewonscreeny){
// return true;
// }else{
// return false;
// }
return listviewonscreeny <= topnewsviewonscreeny;
}else{
return true;
}
}
private void refreshviewstate() {
switch (currentstatus) {
case pull_down_refresh://下拉刷新状态
iv_arrow.startanimation(downanimation);
tv_status.settext("下拉刷新...");
break;
case release_refresh://手松刷新状态
iv_arrow.startanimation(upanimation);
tv_status.settext("手松刷新...");
break;
case refreshing://正在刷新状态
tv_status.settext("正在刷新...");
pb_status.setvisibility(visible);
iv_arrow.clearanimation();
iv_arrow.setvisibility(gone);
break;
}
}
/**
* 当联网成功和失败的时候回调该方法
* 用户刷新状态的还原
*
* @param sucess
*/
public void onrefreshfinish(boolean sucess) {
if(isloadmore){
//加载更多
isloadmore = false;
//隐藏加载更多布局
footerview.setpadding(0,-footerviewheight,0,0);
}else{
//下拉刷新
tv_status.settext("下拉刷新...");
currentstatus = pull_down_refresh;
iv_arrow.clearanimation();
pb_status.setvisibility(gone);
iv_arrow.setvisibility(visible);
//隐藏下拉刷新控件
ll_pull_down_refresh.setpadding(0, -pulldownrefreshheight, 0, 0);
if (sucess) {
//设置最新更新时间
tv_time.settext("上次更新时间:" + getsystemtime());
}
}
}
/**
* 得到当前android系统的时间
*
* @return
*/
private string getsystemtime() {
simpledateformat format = new simpledateformat("yyyy-mm-dd hh:mm:ss");
return format.format(new date());
}
/**
* 监听控件的刷新
*/
public interface onrefreshlistener {
/**
* 当下拉刷新的时候回调这个方法
*/
public void onpulldownrefresh();
/**
当加载更多的时候回调这个方法
*/
public void onloadmore();
}
private onrefreshlistener monrefreshlistener;
/**
* 设置监听刷新,由外界设置
*/
public void setonrefreshlistener(onrefreshlistener l) {
this.monrefreshlistener = l;
}
}

以上所述是小编给大家介绍的android 仿硅谷新闻下拉刷新/上拉加载更多,希望对大家有所帮助