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

XRecyclerView实现下拉刷新、滚动到底部加载更多等功能

程序员文章站 2023-01-03 10:10:04
介绍: 一个实现了下拉刷新,滚动到底部加载更多以及添加header功能的的recyclerview。使用方式和recyclerview完全一致,不需要额外的layout,...

介绍:
一个实现了下拉刷新,滚动到底部加载更多以及添加header功能的的recyclerview。使用方式和recyclerview完全一致,不需要额外的layout,不需要写特殊的adater。 加载效果内置了avloadingindicatorview上的所有效果,可以根据需要指定。

项目地址:https://github.com/jianghejie/xrecyclerview

效果:

XRecyclerView实现下拉刷新、滚动到底部加载更多等功能

使用:

xml

<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"
tools:context=".mainactivity">


<com.jcodecraeer.xrecyclerview.xrecyclerview
android:id="@+id/recyclerview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

</relativelayout>

2.mainactivity

package com.example.xrecyclerview;
 
import android.os.bundle;
import android.os.handler;
import android.support.v7.app.appcompatactivity;
import android.support.v7.widget.linearlayoutmanager;
import android.view.layoutinflater;
import android.view.menu;
import android.view.menuitem;
import android.view.view;
import android.view.viewgroup;
import com.jcodecraeer.xrecyclerview.progressstyle;
import com.jcodecraeer.xrecyclerview.xrecyclerview;
import java.util.arraylist;
 

public class mainactivity extends appcompatactivity {
private xrecyclerview mrecyclerview;
private myadapter madapter;
private arraylist<string> listdata;
private int refreshtime = 0;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
mrecyclerview = (xrecyclerview)this.findviewbyid(r.id.recyclerview);
linearlayoutmanager layoutmanager = new linearlayoutmanager(this);
layoutmanager.setorientation(linearlayoutmanager.vertical);
mrecyclerview.setlayoutmanager(layoutmanager);

mrecyclerview.setrefreshprogressstyle(progressstyle.ballspinfadeloader);
mrecyclerview.setlaodingmoreprogressstyle(progressstyle.pacman);
mrecyclerview.setarrowimageview(r.drawable.iconfont_downgrey);


view header = layoutinflater.from(this).inflate(r.layout.recyclerview_header, (viewgroup)findviewbyid(android.r.id.content),false);
mrecyclerview.addheaderview(header);


mrecyclerview.setloadinglistener(new xrecyclerview.loadinglistener() {
@override
public void onrefresh() {
refreshtime ++;
new handler().postdelayed(new runnable(){
public void run() {
listdata.clear();
for(int i = 0; i < 15 ;i++){
listdata.add("item" + i + "after " + refreshtime + " times of refresh");
}
madapter.notifydatasetchanged();
mrecyclerview.refreshcomplete();
}
}, 3000);   //refresh data here
}
 
@override
public void onloadmore() {
new handler().postdelayed(new runnable(){
public void run() {
for(int i = 0; i < 15 ;i++){
listdata.add("item" + (i + listdata.size()) );
}
madapter.notifydatasetchanged();
mrecyclerview.loadmorecomplete();
}
}, 3000);
 
}

});
 
listdata = new arraylist<string>();
madapter = new myadapter(listdata);
for(int i = 0; i < 15 ;i++){
listdata.add("item" + i);
}
mrecyclerview.setadapter(madapter);
}


@override
public boolean oncreateoptionsmenu(menu menu) {
// inflate the menu; this adds items to the action bar if it is present.
getmenuinflater().inflate(r.menu.menu_main, menu);
return true;
}
 
@override
public boolean onoptionsitemselected(menuitem item) {
// handle action bar item clicks here. the action bar will
// automatically handle clicks on the home/up button, so long
// as you specify a parent activity in androidmanifest.xml.
int id = item.getitemid();




//noinspection simplifiableifstatement

if (id == r.id.action_settings) {
return true;
}
 
return super.onoptionsitemselected(item);
}
 
 
}

3. adapter

package com.example.xrecyclerview;


import android.support.v7.widget.recyclerview;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.textview;
import java.util.arraylist;

/**
* created by jianghejie on 15/11/26.
*/
public class myadapter extends recyclerview.adapter<myadapter.viewholder> {
public arraylist<string> datas = null;
public myadapter(arraylist<string> datas) {
this.datas = datas;
}
//创建新view,被layoutmanager所调用
@override
public viewholder oncreateviewholder(viewgroup viewgroup, int viewtype) {
view view = layoutinflater.from(viewgroup.getcontext()).inflate(r.layout.item,viewgroup,false);
viewholder vh = new viewholder(view);
return vh;
}
//将数据与界面进行绑定的操作
@override
public void onbindviewholder(viewholder viewholder, int position) {
viewholder.mtextview.settext(datas.get(position));
}
//获取数据的数量
@override
public int getitemcount() {
return datas.size();
}
//自定义的viewholder,持有每个item的的所有界面元素
public static class viewholder extends recyclerview.viewholder {
public textview mtextview;
public viewholder(view view){
super(view);

mtextview = (textview) view.findviewbyid(r.id.text);
}


}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。