The following aspects can be learned from this article:

  • 1. What is the MaterialRefreshLayout
  • 2. How to use the MaterialRefreshLayout
  • 3. A simple little demo that takes you through the pull down refresh pull up load more

First take a look at the renderings:

What is # # # # MaterialRefreshLayout

  • The address of the project: https://github.com/android-cjj/Android-MaterialRefreshLayout

  • SwipeRefreshLayout is a drop-down refresh control that is more beautiful and powerful than SwipeRefreshLayout and easier to use. Support Android 3.0 or above. Hope you like it, hehe.

#### AS add a dependency library, so that instead of importing the entire library,Eclipse users import the library. (Because I think if you don’t have an environment that doesn’t allow you to use AS, you should probably give up Eclipse. After all, a lot of great projects on Github use AS.

dependencies {
    compile 'com. CJJ. Materialrefeshlayout: library: 1.3.0'
}
Copy the code

Add the following code to your layout XML.

<com.cjj.MaterialRefreshLayout
    android:id="@+id/refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent"> <! --> > <... ListView or GridView or RecyclerView or ScrollView and more... > </com.cjj.MaterialRefreshLayout>Copy the code

Implement the following code in Java:

materialRefreshLayout = (MaterialRefreshLayout) findViewById(R.id...) ; materialRefreshLayout.setMaterialRefreshListener(newMaterialRefreshListener() {@Override public void onRefresh(Final MaterialRefreshLayout MaterialRefreshLayout) {// Drop refresh... } @override public void onRefreshLoadMore(MaterialRefreshLayout MaterialRefreshLayout) { }} // End drop-down refresh... materialRefreshLayout.finishRefresh(); // End pull-up refresh... materialRefreshLayout.finishRefreshLoadMore();Copy the code

Pull-down effect configuration (the official provides the pull-down effect in 6, which is used in this example. If you want to know more, please go to the official website to check)


    <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"
    >
Copy the code

All right, without further ado, let’s look at the code. (Everything is still based on the code ah, only the code to achieve the function, is king)

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; Private List<String> datas = new ArrayList<>(); private List<String> datas = new ArrayList<>(); Private List<String> mList = new ArrayList<>(); private List<String> mList = new ArrayList<>(); private MyAdapter mAdapter; Private Boolean isLoadMore = /** * if the data load is complete, check whether the data load is on the pull-uptrue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); initView(); initDatas(); } /** * initialize the layout control */ private voidinitView() { mRefreshLayout = (MaterialRefreshLayout) findViewById(R.id.refresh); mRecyclerView = (RecyclerView) findViewById(R.id.recycleview); initRefresh(); } /** * init load */ private voidinitRefresh() { mAdapter = new MyAdapter(datas); mRecyclerView.setAdapter(mAdapter); / / the following can set the default animation mRecyclerView. SetItemAnimator (new DefaultItemAnimator ()); . / / the following layout can set themselves up mRecyclerView setLayoutManager (new LinearLayoutManager (this)); /** * sets whether to pull up to load more, default isfalse, to manually changetrue, otherwise there will not be on tensile load * / mRefreshLayout setLoadMore (isLoadMore); / / set the line mRecyclerView. AddItemDecoration (new DividerItemDecoration (this, DividerItemDecoration. VERTICAL_LIST)); mRefreshLayout.setMaterialRefreshListener(newMaterialRefreshListener* If you want to achieve your own results, you will define a method that gets the latest data, or a method that gets the data before calling it * here. @param materialRefreshLayout public void onRefresh(materialRefreshLayout MaterialRefreshLayout) {// Normally the load data is in the child thread, we used handler new handler (). PostDelayed (new)Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this, "There's no more data.", Toast.LENGTH_SHORT).show(); / * * * refresh after the completion of this method is called, or refresh the effect is not disappear. * / mRefreshLayout finishRefresh (); }}, 3000); } /** * pull up to load more methods, here I just simple simulation load four data * when the real use of the method to define the data, usually pagination, at the data side of the page to increase the number of pages * @param materialRefreshLayout public void onRefreshLoadMore(materialRefreshLayout materialRefreshLayout) { new Handler().postDelayed(newRunnable() {
                    @Override
                    public void run() {
                        isLoadMore = false;
                        for (int i = 0; i <= 3; i++) {
                            mList.add(i, "new City "+ i); } // Notify refresh madapter.addlists (mAdapter.getLists().size(), mList); //mRecyclerView.scrollToPosition(mAdapter.getLists().size()); / * * * after finished loading data, this method is called, it won't disappear or refresh * / mRefreshLayout finishRefreshLoadMore (); }}, 3000); }}); } /** * initializes data */ private voidinitDatas() {

        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"); }}Copy the code

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 Ruan on 2016/1/26. ** * Created by Ruan on 2016/1/26. ** * Created by Ruan on 2016/1/26. Also want to learn * knowledge can help more people, share is learning a pleasure * QQ: 1069584784 * csdn:http://blog.csdn.net/wuyinlei * / public class MyAdapter Extends recyclerView. Adapter< myAdapter. MyViewHolder> {private LayoutInflater mInflater; /** * private List<String> mLists; /** * Custom item click (not used here) */ private OnItemClickListener; /** * mListener get method * @return
     */
    public OnItemClickListener getListener() {
        return mListener;
    }

    /**
     * mListener setMethod * @param listener */ public voidsetListener(OnItemClickListener listener) { mListener = listener; } /** * mLists set get method through which it can get data size(); * @return
     */
    public List<String> getLists() {
        returnmLists; } @param lists */ public void addLists(List<String> lists) {addLists(0, lists); } /** * add data * @param position add data * @param lists add data */ 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. * * Notify item from where to where it has changed */ notifyItemRangeChanged(position, mlists.size ()); }} /** * public MyAdapter(List<String> items) {mLists = items; Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { mInflater = LayoutInflater.from(parent.getContext()); View view = mInflater.inflate(R.layout.list_item, null);returnnew MyViewHolder(view); } @param holder @param position @override public void onBindViewHolder(MyViewHolder, MyViewHolder) int position) { holder.tv_text.setText(mLists.get(position)); } @Override public intgetItemCount() {
        returnmLists.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); /** * If we want to implement item click events, we need to do it here, because recycleView doesn't provide us with direct click and touch events, */ tv_text.setonClickListener (new View)OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mListener ! = null) { mListener.onClick(v, getLayoutPosition(), mLists.get(getLayoutPosition())); }}}); // Create a RecycleView // create a RecycleView // create a RecycleView Interface OnItemClickListener {// define void onClick(View v, int position, String item); }}Copy the code

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>

Copy the code

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>
Copy the code

DividerItemDecoration. Java (the online) :

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 inThe 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 toin 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);
        }
    }

}

Copy the code

RecycleView+SwipeRefreshLayout: SwipeRefreshLayout: SwipeRefreshLayout: SwipeRefreshLayout: SwipeRefreshLayout: SwipeRefreshLayout: SwipeRefreshLayout: SwipeRefreshLayout

  • http://blog.csdn.net/wuyinlei/article/details/49805567

MaterialRefreshLayout implements a drop-down refresh drop-down to load more items

  • https://github.com/wuyinlei/MaterialRefreshLayoutDemo