Irecycleview 的初次使用简单介绍(irecycleview 下拉刷新上拉加载)

Posted xuexidememeda

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Irecycleview 的初次使用简单介绍(irecycleview 下拉刷新上拉加载)相关的知识,希望对你有一定的参考价值。

导包 还得加一个mavn地址自己也看一下作者git把有详细解释(自己也要导入recycleview的包)

我的例子下载地址  https://www.lanzous.com/i32yzaj

implementation com.github.Aspsine:IRecyclerView:0.0.7

其实简单用法很简单 导包后 activity 和fragment 都可以  implements OnRefreshListener,OnLoadMoreListener  实现其中的方法loadmore refrash就行是我想复杂了【但是对于上拉刷新要setRefreshEnabled(true);和设置刷新的头部view 我以为作者在包里面封装了,后来发现并没封装 这个头部也是要自己写的吧 尾部也是......不知到是不是我理解错了 作者例子里也不是很详细 没有默认的封装好的 】

我的简单例子图片

技术图片技术图片

我掉的坑就是设置头部和尾部view,以为不设置会有默认的其实没有  ,第二个 copy作者的xml文件和那个头部和尾部view类 xml文件出错

layout_irecyclerview_load_more_footer.xml

<com.stdu.LoadMoreFooterView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="48dp"/>

com.stdu.LoadMoreFooterView 这个要是你的类路径 直接copy作者的弄了一会

  其他的就很简单

上Mainactivity代码

技术图片
public class MainActivity extends AppCompatActivity implements  OnRefreshListener,OnLoadMoreListener{
    List<Teacher> list;
    IRecyclerView view;
    TeacherAdapter1 adapter;
    LoadMoreFooterView loadMoreFooterView;
    int num=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initList();
        view = findViewById(R.id.view);
        loadMoreFooterView= (LoadMoreFooterView) view.getLoadMoreFooterView();
        view.setLayoutManager(new LinearLayoutManager(this));
        initRefreshView();//初始化刷新
        initLoadMoreView();//初始化加载更多
        adapter = new TeacherAdapter1(R.layout.item,list);//TeacherAdapter(this,list);
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(MainActivity.this,"哈哈"+position,Toast.LENGTH_SHORT).show();
            }
        });
        view.setIAdapter(adapter);
//注意setIAdapter 中间有个I


    }


    private void initRefreshView() {
        view.setRefreshEnabled(true);
       // System.out.println("initRefreshView------------------");
        //这两个类是直接从作者哪里八下来的 我发现作者没提供默认样式..........是我没找到错了码?
        ClassicRefreshHeaderView classicRefreshHeaderView = new ClassicRefreshHeaderView(this);
        classicRefreshHeaderView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,80 ));
        view.setRefreshHeaderView(classicRefreshHeaderView);
        view.setOnRefreshListener(this);
    }


    private void initLoadMoreView()
    {
        view.setOnLoadMoreListener(this);
        view.setLoadMoreEnabled(true);
        view.setOnLoadMoreListener(this);
    }


    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                view.setRefreshing(false);
            }
        },2000);
    }

//初始化载入的list
    private void initList() {
        list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(new Teacher(R.mipmap.ic_launcher,"teacher "+i));
        }
    }



    @Override
    public void onLoadMore() {
        num++; //模拟假数据如果num<3则请求到了数据
        loadMoreFooterView.setStatus(LoadMoreFooterView.Status.LOADING);//向上滑动就显示loading 界面
        if(num<=3) {

            new Handler().postDelayed(new Runnable() {
                @Override//延迟两秒执行
                public void run() {
                    //模拟网上得到数据封装成list
                    for (int i = 0; i < 10; i++) {
                        list.add(new Teacher(R.mipmap.ic_launcher, "第二次加载 " + i));
                    }
                    //System.out.println("载入其他数据");
                    loadMoreFooterView.setStatus(LoadMoreFooterView.Status.GONE);//得到数据loading界面消失

                }
            },2000);

        }else
            {
            loadMoreFooterView.setStatus(LoadMoreFooterView.Status.THE_END);//数据加载完
        }

    }



    }
View Code
TeacherAdapter1 代码 (这个老师的例子是看的网上一个例子睡了一晚百度了一下没找着,这个TeacherAdapter1 是我自己写的哈不存在抄袭)
技术图片
public class TeacherAdapter1 extends BaseQuickAdapter<Teacher, BaseViewHolder> {
    List<Teacher> list;
    Context context;

    public TeacherAdapter1(int layoutResId, @Nullable List<Teacher> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, Teacher item) {
     helper.addOnClickListener(R.id.image);
     helper.setText(R.id.text,item.getName());
     helper.setImageResource(R.id.image,item.getId());


    }
}
View Code
ClassicRefreshHeaderView  作者的直接copy
技术图片
package com.stdu;

import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aspsine.irecyclerview.RefreshTrigger;
public class ClassicRefreshHeaderView extends RelativeLayout implements RefreshTrigger {
    private ImageView ivArrow;

    private ImageView ivSuccess;

    private TextView tvRefresh;

    private ProgressBar progressBar;

    private Animation rotateUp;

    private Animation rotateDown;

    private boolean rotated = false;

    private int mHeight;

    public ClassicRefreshHeaderView(Context context) {
        this(context, null);
    }

    public ClassicRefreshHeaderView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ClassicRefreshHeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        inflate(context, R.layout.layout_irecyclerview_classic_refresh_header_view, this);

        tvRefresh = (TextView) findViewById(R.id.tvRefresh);

        ivArrow = (ImageView) findViewById(R.id.ivArrow);

        ivSuccess = (ImageView) findViewById(R.id.ivSuccess);

        progressBar = (ProgressBar) findViewById(R.id.progressbar);

        rotateUp = AnimationUtils.loadAnimation(context, R.anim.rotate_up);

        rotateDown = AnimationUtils.loadAnimation(context, R.anim.rotate_down);
    }

    @Override
    public void onStart(boolean automatic, int headerHeight, int finalHeight) {
        this.mHeight = headerHeight;
        progressBar.setIndeterminate(false);
    }

    @Override
    public void onMove(boolean isComplete, boolean automatic, int moved) {
        if (!isComplete) {
            ivArrow.setVisibility(VISIBLE);
            progressBar.setVisibility(GONE);
            ivSuccess.setVisibility(GONE);
            if (moved <= mHeight) {
                if (rotated) {
                    ivArrow.clearAnimation();
                    ivArrow.startAnimation(rotateDown);
                    rotated = false;
                }

                tvRefresh.setText("下拉刷新");
            } else {
                tvRefresh.setText("释放刷新");
                if (!rotated) {
                    ivArrow.clearAnimation();
                    ivArrow.startAnimation(rotateUp);
                    rotated = true;
                }
            }
        }
    }

    @Override
    public void onRefresh() {
        ivSuccess.setVisibility(GONE);
        ivArrow.clearAnimation();
        ivArrow.setVisibility(GONE);
        progressBar.setVisibility(VISIBLE);
        tvRefresh.setText("刷新中..");
    }

    @Override
    public void onRelease() {

    }

    @Override
    public void onComplete() {
        rotated = false;
        ivSuccess.setVisibility(VISIBLE);
        ivArrow.clearAnimation();
        ivArrow.setVisibility(GONE);
        progressBar.setVisibility(GONE);
        tvRefresh.setText("刷新成功");
    }

    @Override
    public void onReset() {
        rotated = false;
        ivSuccess.setVisibility(GONE);
        ivArrow.clearAnimation();
        ivArrow.setVisibility(GONE);
        progressBar.setVisibility(GONE);
    }
}
View Code
LoadMoreFooterView 作者的直接copy
技术图片
package com.stdu;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

public class LoadMoreFooterView extends FrameLayout {

    private Status mStatus;

    private View mLoadingView;

    private View mErrorView;

    private View mTheEndView;

    private OnRetryListener mOnRetryListener;

    public LoadMoreFooterView(Context context) {
        this(context, null);
    }

    public LoadMoreFooterView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadMoreFooterView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.layout_irecyclerview_load_more_footer_view, this, true);

        mLoadingView = findViewById(R.id.loadingView);
        mErrorView = findViewById(R.id.errorView);
        mTheEndView = findViewById(R.id.theEndView);

        mErrorView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnRetryListener != null) {
                    mOnRetryListener.onRetry(LoadMoreFooterView.this);
                }
            }
        });

        setStatus(Status.GONE);
    }

    public void setOnRetryListener(OnRetryListener listener) {
        this.mOnRetryListener = listener;
    }

    public Status getStatus() {
        return mStatus;
    }

    public void setStatus(Status status) {
        this.mStatus = status;
        change();
    }

    public boolean canLoadMore() {
        return mStatus == Status.GONE || mStatus == Status.ERROR;
    }

    private void change() {
        switch (mStatus) {
            case GONE:
                mLoadingView.setVisibility(GONE);
                mErrorView.setVisibility(GONE);
                mTheEndView.setVisibility(GONE);
                break;

            case LOADING:
                mLoadingView.setVisibility(VISIBLE);
                mErrorView.setVisibility(GONE);
                mTheEndView.setVisibility(GONE);
                break;

            case ERROR:
                mLoadingView.setVisibility(GONE);
                mErrorView.setVisibility(VISIBLE);
                mTheEndView.setVisibility(GONE);
                break;

            case THE_END:
                mLoadingView.setVisibility(GONE);
                mErrorView.setVisibility(GONE);
                mTheEndView.setVisibility(VISIBLE);
                break;
        }
    }

    public enum Status {
        GONE, LOADING, ERROR, THE_END
    }

    public interface OnRetryListener {
        void onRetry(LoadMoreFooterView view);
    }

}
View Code

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<com.aspsine.irecyclerview.IRecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:loadMoreEnabled="true"
    app:loadMoreFooterLayout="@layout/layout_irecyclerview_load_more_footer"
    app:refreshEnabled="true"

     />

 

 

不想贴代码了大家直接下载例子 吧也是我自己学习的记录  

 



 


以上是关于Irecycleview 的初次使用简单介绍(irecycleview 下拉刷新上拉加载)的主要内容,如果未能解决你的问题,请参考以下文章

pyspider初次使用pyspider遇到的问题

虹软人脸识别 - faceId及IR活体检测的介绍

markdown 继电器IR介绍

LLVM 之 IR 篇:如何使用 LLVM IR 优化器

LLVM 之 IR 篇:如何编写内联 Pass

LLVM 之 IR 篇:如何编写内联 Pass