一个very不错的刷新加载框架
先看效果
步骤:
1、导入第三方开源库
下载资源:http://download.csdn.net/download/lanrenxiaowen/10185999
在项目里面找到第三方开源库
这时会在项目里面发现多了这个第三方开源库
然后手动修改项目跟目录下settings.gadle 添加include ‘:App’,’:xrecyclerview’如图所示:
include ':App',':xrecyclerview'
然后在打开App/build.gradle这个文件,添加
dependencies{
compile project(':xrecyclerview')
}
如图:
如果这时候编译还报错的话就在项目xrecyclerview目录下添加一个build.gradle的这个文件,内容如下
apply plugin: 'com.android.library'
version = "1.5.5"
group = "com.jcodecraeer"
def siteUrl = 'https://github.com/jianghejie/XRecyclerView'
def gitUrl = 'https://github.com/jianghejie/XRecyclerView.git'
android {
compileSdkVersion 26
buildToolsVersion '26.0.2'
defaultConfig {
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets.main{
jni.srcDirs = []
jniLibs.srcDir "src/main/libs"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:recyclerview-v7:26.1.0'
compile 'com.android.support:design:26.1.0'
}
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
artifacts {
archives sourcesJar
}
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
最后编译如果还会报错的话
这时候可能需要修改一下xrecyclerview目录下的AndroidManifest.xml文件有可能存在和你项目中文件有冲突或版本跨度太大导致语法的错误修改一下就OK了。
上面就是如果导入第三方开源库文件了,
下面是要进行我们的代码工作》》》》》
在xml布局文件里面引用我们导入的第三方开源库文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:id="@+id/hello"
android:layout_width="match_parent"
android:padding="10dp"
android:text="Hello"
android:textSize="20sp"
android:gravity="center"
android:layout_height="wrap_content" />
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id="@+id/recyclerview"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
记得写item条目布局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">
<LinearLayout
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#999999"
android:orientation="vertical"
android:layout_gravity="center">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_gravity="center"/>
<ImageView
android:layout_width="match_parent"
android:src="@mipmap/ic_launcher"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
写列表的adapter适配器
package com.example.administrator.recyclerviewdemo;
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 void setClickCallBack(ItemClickCallBack clickCallBack) {
this.clickCallBack = clickCallBack;
}
public interface ItemClickCallBack{
void onItemClick(int pos);
}
public ArrayList<String> datas = null;
private ItemClickCallBack clickCallBack;
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);
return new ViewHolder(view);
}
//将数据与界面进行绑定的操作
@Override
public void onBindViewHolder(ViewHolder viewHolder,final int position) {
viewHolder.mTextView.setText(datas.get(position));
viewHolder.mTextView.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
if(clickCallBack != null){
clickCallBack.onItemClick(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);
}
}
}
最后就是Main文件里面的代码工作:
package com.example.administrator.recyclerviewdemo;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
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;
private int times = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (XRecyclerView)this.findViewById(R.id.recyclerview);
GridLayoutManager layoutManager = new GridLayoutManager(this,2);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
mRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);
mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
refreshTime ++;
times = 0;
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
listData.clear();
for(int i = 0; i < 20 ;i++){
listData.add("item" + i + "after " + refreshTime + " times of refresh");
}
mAdapter.notifyDataSetChanged();
mRecyclerView.refreshComplete();
}
}, 1000); //refresh data here
}
@Override
public void onLoadMore() {
if(times < 2){
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
mRecyclerView.loadMoreComplete();
for(int i = 0; i < 20 ;i++){
listData.add("item" + (i + listData.size()) );
}
mRecyclerView.loadMoreComplete();
mAdapter.notifyDataSetChanged();
}
}, 1000);
} else {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
for(int i = 0; i < 9 ;i++){
listData.add("item" + (i + listData.size()) );
}
mAdapter.notifyDataSetChanged();
mRecyclerView.setNoMore(true);
}
}, 1000);
}
times ++;
}
});
listData = new ArrayList<String>();
for(int i = 0; i < 20 ;i++){
listData.add("item" + i);
}
mAdapter = new MyAdapter(listData);
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) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
}
完成,老规矩,源码奉上: