通過本文可以了解以下幾方面:
- 1、MaterialRefreshLayout是什么
- 2、MaterialRefreshLayout怎么使用
- 3、一個簡單的小小demo帶你實現(xiàn)下拉刷新上拉加載更多
首先看下效果圖:
這里寫圖片描述
MaterialRefreshLayout是什么
項目地址: https://github.com/android-cjj/Android-MaterialRefreshLayout
簡介:這是一個下拉刷新控件,它比SwipeRefreshLayout更漂亮和強大,使用也比較簡單。支持android 3.0 以上。希望你喜歡,呵呵。
用法介紹(摘自官方文檔)
AS添加依賴庫,這樣就不用導入整個library庫,Eclipse的用戶導入library庫,慢慢折騰吧!(因為我覺得如果不是環(huán)境不允許你用as的話,還是放棄eclipse吧,畢竟github上很多優(yōu)秀的項目都是用as的,好吧我多嘴了...)
dependencies {
compile 'com.cjj.materialrefeshlayout:library:1.3.0'
}
在你的layout xml.添加下面的代碼:
<com.cjj.MaterialRefreshLayout
android:id="@+id/refresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!--在這里我們可以添加ListView OR GridView OR RecycleView OR ScrollView OR 更多-->
<...ListView or GridView or RecyclerView or ScrollView and more...>
</com.cjj.MaterialRefreshLayout>
在java中實現(xiàn)以下代碼:
materialRefreshLayout = (MaterialRefreshLayout) findViewById(R.id...);
materialRefreshLayout.setMaterialRefreshListener(new MaterialRefreshListener() {
@Override
public void onRefresh(final MaterialRefreshLayout materialRefreshLayout) {
//下拉刷新...
}
@Override
public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
//上拉加載更多...
}
}
// 結束下拉刷新...
materialRefreshLayout.finishRefresh();
// 結束上拉刷新...
materialRefreshLayout.finishRefreshLoadMore();
下拉效果配置(官方提供了6中下拉效果,這里提供本例子中使用的,如果想要了解更多,請去官方查看)
<com.cjj.MaterialRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/refresh"
app:overlay="false"
app:wave_show="true"
app:wave_color="@color/material_green"
app:wave_height_type="higher"
>
好了,不多說,看代碼吧。(一切還是以代碼為主啊,只有代碼實現(xiàn)了功能,才是王道)
MainActivity.java:
package com.example.materialrefreshlayoutdemo;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;
import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private MaterialRefreshLayout mRefreshLayout;
private RecyclerView mRecyclerView;
/**
* 剛初始化的數(shù)據(jù)
*/
private List<String> datas = new ArrayList<>();
/**
* 一個承接數(shù)據(jù)的數(shù)組
*/
private List<String> mList = new ArrayList<>();
private MyAdapter mAdapter;
/**
* 在上拉刷新的時候,判斷,是否處于上拉刷新,如果是的話,就禁止在一次刷新,保障在數(shù)據(jù)加載完成之前
* 避免重復和多次加載
*/
private boolean isLoadMore = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initDatas();
}
/**
* 初始化布局控件
*/
private void initView() {
mRefreshLayout = (MaterialRefreshLayout) findViewById(R.id.refresh);
mRecyclerView = (RecyclerView) findViewById(R.id.recycleview);
initRefresh();
}
/**
* 初始化加載
*/
private void initRefresh() {
mAdapter = new MyAdapter(datas);
mRecyclerView.setAdapter(mAdapter);
//下面可以自己設置默認動畫
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
//下面可以自己設置排版方式
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
/**
* 設置是否上拉加載更多,默認是false,要手動改為true,要不然不會出現(xiàn)上拉加載
*/
mRefreshLayout.setLoadMore(isLoadMore);
//設置分割線
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
mRefreshLayout.setMaterialRefreshListener(new MaterialRefreshListener() {
/**
* 刷新的方法,我這里演示的是下拉刷新,因為沒有數(shù)據(jù),我這里也就只是toast一下
* 如果想要實現(xiàn)你們自己要的結果,就會在定義一個方法,獲取最新數(shù)據(jù),或者是在次
* 在這里調用之前獲取數(shù)據(jù)的方法,以達到刷新數(shù)據(jù)的功能
* @param materialRefreshLayout
*/
@Override
public void onRefresh(MaterialRefreshLayout materialRefreshLayout) {
//一般加載數(shù)據(jù)都是在子線程中,這里我用到了handler
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "已經沒有更多數(shù)據(jù)了", Toast.LENGTH_SHORT).show();
/**
* 刷新完成后調用此方法,要不然刷新效果不消失
*/
mRefreshLayout.finishRefresh();
}
}, 3000);
}
/**
* 上拉加載更多的方法,在這里我只是簡單的模擬了加載四條數(shù)據(jù)
* 真正用的時候,就會去定義方法,獲取數(shù)據(jù),一般都是分頁,在數(shù)據(jù)端獲取的時候
* 把頁數(shù)去增加一,然后在去服務端去獲取數(shù)據(jù)
* @param materialRefreshLayout
*/
@Override
public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
isLoadMore = false;
for (int i = 0; i <= 3; i++) {
mList.add(i, "new City " + i);
}
//通知刷新
mAdapter.addLists(mAdapter.getLists().size(), mList);
//mRecyclerView.scrollToPosition(mAdapter.getLists().size());
/**
* 完成加載數(shù)據(jù)后,調用此方法,要不然刷新的效果不會消失
*/
mRefreshLayout.finishRefreshLoadMore();
}
}, 3000);
}
});
}
/**
* 初始化數(shù)據(jù)
*/
private void initDatas() {
datas.add("New York");
datas.add("Bei Jing");
datas.add("Boston");
datas.add("London");
datas.add("San Francisco");
datas.add("Chicago");
datas.add("Shang Hai");
datas.add("Tian Jin");
datas.add("Zheng Zhou");
datas.add("Hang Zhou");
datas.add("Guang Zhou");
datas.add("Fu Gou");
datas.add("Zhou Kou");
}
}
MyAdapter.java:
package com.example.materialrefreshlayoutdemo;
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.List;
/**
* Created by 若蘭 on 2016/1/26.
* 一個懂得了編程樂趣的小白,希望自己
* 能夠在這個道路上走的很遠,也希望自己學習到的
* 知識可以幫助更多的人,分享就是學習的一種樂趣
* QQ:1069584784
* csdn:http://blog.csdn.net/wuyinlei
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
/**
* 加載布局的
*/
private LayoutInflater mInflater;
/**
* 用來存放數(shù)據(jù)的集合
*/
private List<String> mLists;
/**
* 自定義的item點擊(在這里沒有用到)
*/
private OnItemClickListener mListener;
/**
* mListener get方法
* @return
*/
public OnItemClickListener getListener() {
return mListener;
}
/**
* mListener set方法
* @param listener
*/
public void setListener(OnItemClickListener listener) {
mListener = listener;
}
/**
* mLists集合的get方法 通過他可以取得數(shù)據(jù)的size();
* @return
*/
public List<String> getLists() {
return mLists;
}
/**
* 對以下方法的復用
* @param lists
*/
public void addLists(List<String> lists) {
addLists(0, lists);
}
/**
* 添加數(shù)據(jù)
* @param position 添加的位置
* @param lists 添加的數(shù)據(jù)
*/
public void addLists(int position, List<String> lists) {
//mLists = lists;
if (lists != null && lists.size() > 0) {
mLists.addAll(lists);
/**
* Notify any registered observers that the <code>itemCount</code> items starting at
* position <code>positionStart</code> have changed.
*
* 通知item是從哪個地方到哪個地方已經改變了
*/
notifyItemRangeChanged(position, mLists.size());
}
}
/**
* 構造方法,通過new對象的時候傳入數(shù)據(jù)
* @param items
*/
public MyAdapter(List<String> items) {
mLists = items;
}
// 創(chuàng)建ViewHolder ,相當于ListVie Adapter 的getView 方法
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
mInflater = LayoutInflater.from(parent.getContext());
View view = mInflater.inflate(R.layout.list_item, null);
return new MyViewHolder(view);
}
/**
* 綁定數(shù)據(jù)
* @param holder
* @param position
*/
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.tv_text.setText(mLists.get(position));
}
@Override
public int getItemCount() {
return mLists.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
private TextView tv_text;
public MyViewHolder(View itemView) {
super(itemView);
tv_text = (TextView) itemView.findViewById(R.id.tv_item);
/**
* 如果我們想要實現(xiàn)item的點擊事件,就要在這處理,因為recycleview
* 并沒有給我們提供直接的點擊觸摸事件,這個也是他的好處(也可以理解為不好的地方)
*/
tv_text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.onClick(v, getLayoutPosition(), mLists.get(getLayoutPosition()));
}
}
});
}
}
//RecycleView的事件監(jiān)聽,這個要自己去寫,里面的參數(shù)也是根據(jù)自己的需要去定義
//不像listview中,已經有了item的點擊監(jiān)聽事件
interface OnItemClickListener {
//自己定義
void onClick(View v, int position, String item);
}
}
activity_main.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="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<com.cjj.MaterialRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/refresh"
app:overlay="false"
app:wave_show="true"
app:wave_color="@color/material_green"
app:wave_height_type="higher"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:id="@+id/recycleview"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</com.cjj.MaterialRefreshLayout>
</LinearLayout>
item_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:text="1"
android:gravity="center"
android:textColor="#000"
android:textSize="20dp"/>
</RelativeLayout>
DividerItemDecoration.java(這個網上找的):
package com.example.materialrefreshlayoutdemo;
/**
* Created by ruolan on 2015/11/11.
*/
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
/**
* This class is from the v7 samples of the Android SDK. It's not by me!
* <p/>
* See the license above for details.
*/
public class DividerItemDecoration extends RecyclerView.ItemDecoration
{
private static final int[] ATTRS = new int[] { android.R.attr.listDivider };
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
private Drawable mDivider;
private int mOrientation;
public DividerItemDecoration(Context context, int orientation)
{
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(orientation);
}
public void setOrientation(int orientation)
{
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST)
{
throw new IllegalArgumentException("invalid orientation");
}
mOrientation = orientation;
}
@Override
public void onDraw(Canvas c, RecyclerView parent)
{
if (mOrientation == VERTICAL_LIST) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
public void drawVertical(Canvas c, RecyclerView parent)
{
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++)
{
final View child = parent.getChildAt(i);
RecyclerView v = new RecyclerView(
parent.getContext());
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
public void drawHorizontal(Canvas c, RecyclerView parent)
{
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++)
{
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int left = child.getRight() + params.rightMargin;
final int right = left + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
@Override
public void getItemOffsets(Rect outRect, int itemPosition,
RecyclerView parent)
{
if (mOrientation == VERTICAL_LIST)
{
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else
{
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
}
好了,一個簡單的上拉加載更多和下拉刷新就完成了,如果有興趣,可以參考一下這個文章
RecycleView+SwipeRefreshLayout 實現(xiàn)下拉刷新地址:
MaterialRefreshLayout實現(xiàn)下拉刷新上拉加載更多項目的github地址: