当前位置:   article > 正文

下拉刷新 上拉加载_hui 上拉加载

hui 上拉加载

//依赖
compile 'com.androidkun:pulltorefreshrecyclerview:1.0.9'

package com.example.administrator.xiala;


import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;


import com.androidkun.PullToRefreshRecyclerView;
import com.androidkun.adapter.BaseAdapter;
import com.androidkun.adapter.ViewHolder;
import com.androidkun.callback.PullToRefreshListener;


import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity implements PullToRefreshListener{    //注意继承监听
    private PullToRefreshRecyclerView pullToRefreshRV;
    private ArrayList<String> arrayList = new ArrayList<>();
    private ModeAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); //main方法


//id


        pullToRefreshRV = (PullToRefreshRecyclerView) findViewById(R.id.pullToRefreshRV);


//布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL)
        pullToRefreshRV.setLayoutManager(layoutManager);


//适配器——调全局
        adapter = new ModeAdapter(this, R.layout.item,arrayList);
        pullToRefreshRV.setAdapter(adapter);
//是否开启下拉刷新功能
        pullToRefreshRV.setPullRefreshEnabled(true);
//是否开启上拉加载功能
        pullToRefreshRV.setLoadingMoreEnabled(true);
//设置是否显示上次刷新的时间
        pullToRefreshRV.displayLastRefreshTime(true);
//设置刷新回调
        pullToRefreshRV.setPullToRefreshListener(this);
//主动触发下拉刷新操作
//pullToRefreshRV.onRefresh();


    }
    public void onRefresh(){
        pullToRefreshRV.postDelayed(new Runnable() {
            @Override
            public void run() {
                pullToRefreshRV.setRefreshComplete();
                //模拟没有数据的情况
                arrayList.clear();
                adapter.notifyDataSetChanged();
            }
        },3000);




    }
    public void onLoadMore() {
        pullToRefreshRV.postDelayed(new Runnable() {
            @Override
            public void run() {
                pullToRefreshRV.setLoadMoreComplete();
                //模拟加载数据的情况
//arrayLisr是集合
                int size = arrayList.size();
                for (int i = size; i < size + 4; i++) {
                    arrayList.add( i + i + i + i+"");
                }
// 适配器刷新
                adapter.notifyDataSetChanged();
            }
        },3000);
    }

//适配器
    public class ModeAdapter extends BaseAdapter {


        public ModeAdapter(Context context, int layoutId, ArrayList<String> datas) {
            super(context, layoutId, datas);
        }


        @Override
        public void convert(ViewHolder holder, Object o) {
            holder.setText(R.id.Text, (String) o);
        }
    }
}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/1009019
推荐阅读
相关标签
  

闽ICP备14008679号