zoukankan      html  css  js  c++  java
  • 下拉刷新+Recycle+上拉刷新

    下拉刷新:

    activity中:

    public class DemoBActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener{

    @BindView(R.id.demo_b_recyclerview)RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)SwipeRefreshLayout swipeRefreshLayout;
    private SimpleAdapter simpleAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_demo_b);
    ButterKnife.bind(this);
    //设置布局
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    simpleAdapter = new SimpleAdapter();
    recyclerView.setAdapter(simpleAdapter);

    //下拉刷新监听
    swipeRefreshLayout.setOnRefreshListener(this);
    }

    @Override
    public void onRefresh() {
    //刷新3秒后,展示20条新数据
    new Thread(new Runnable() {
    @Override
    public void run() {
    //沉睡3秒
    try {
    Thread.sleep(3000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    simpleAdapter.clear();//刷新数据,清空旧数据
    for (int i = 0; i < 20; i++) {
    simpleAdapter.addItem("我是新刷新到的第" + i + "条数据");
    }
    //更新UI(需要在UI线程)
    runOnUiThread(new Runnable() {
    @Override
    public void run() {
    simpleAdapter.notifyDataSetChanged();
    //刷新完成,关闭progressbar
    swipeRefreshLayout.setRefreshing(false);
    }
    });
    }
    }).start();
    }
    }

    adapter中实现对recycleView的setAdapter(●ˇ∀ˇ●)

    package com.fuicuiedu.xc.resourceviewdemo_20170321;

    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;

    import butterknife.BindView;
    import butterknife.ButterKnife;

    /**
    * Created by Administrator on 2017/3/21 0021.
    */

    public class SimpleAdapter extends RecyclerView.Adapter<SimpleAdapter.DemoViewHolder>{

    private ArrayList<String> datas = new ArrayList<>();

    //添加数据
    public void addItem(String data){
    datas.add(data);
    }
    //清空数据
    public void clear(){
    datas.clear();
    }

    //创建
    @Override
    public DemoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    //把item视图注入进来
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_text,parent,false);
    return new DemoViewHolder(view);
    }

    //绑定数据
    @Override
    public void onBindViewHolder(DemoViewHolder holder, int position) {
    //拿到数据
    String data = datas.get(position);
    //拿到当前项的视图
    TextView textView = holder.textView;
    //绑定数据
    textView.setText(data);
    }

    //获取item数量
    @Override
    public int getItemCount() {
    return datas.size();
    }


    //先实现ViewHolder
    static class DemoViewHolder extends RecyclerView.ViewHolder{
    @BindView(R.id.textView)TextView textView;

    public DemoViewHolder(View itemView) {
    super(itemView);
    ButterKnife.bind(this,itemView);
    }
    }
    }

    布局:

    =======================上拉刷新=使用第三方控件需要依赖===============

    mugen的机制:监视recycleView是否滑动到底部,滑到底部,自动加载更多数据(onLoadMore方法)

     

    package com.fuicuiedu.xc.resourceviewdemo_20170321.democ;

    import android.support.v4.widget.SwipeRefreshLayout;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.View;
    import android.widget.ProgressBar;

    import com.fuicuiedu.xc.resourceviewdemo_20170321.R;
    import com.fuicuiedu.xc.resourceviewdemo_20170321.SimpleAdapter;
    import com.mugen.Mugen;
    import com.mugen.MugenCallbacks;

    import butterknife.BindView;
    import butterknife.ButterKnife;

    public class DemoCActivity extends AppCompatActivity implements
    SwipeRefreshLayout.OnRefreshListener,
    MugenCallbacks{

    @BindView(R.id.demo_c_rv)RecyclerView recyclerView;
    @BindView(R.id.demo_c_srl)SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.demo_c_prb)ProgressBar progressBar;
    private SimpleAdapter simpleAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_demo_c);
    ButterKnife.bind(this);

    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    simpleAdapter = new SimpleAdapter();
    recyclerView.setAdapter(simpleAdapter);

    //下拉刷新
    swipeRefreshLayout.setOnRefreshListener(this);

    //上拉加载(mugen),(会检测到列表是否滑动到底部,如果到了底部执行“onLoadMore”)
    Mugen.with(recyclerView,this).start();
    }

    @Override
    public void onRefresh() {
    //刷新3秒后,展示20条新数据
    new Thread(new Runnable() {
    @Override
    public void run() {
    //沉睡3秒
    try {
    Thread.sleep(3000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    simpleAdapter.clear();//刷新数据,清空旧数据
    for (int i = 0; i < 20; i++) {
    simpleAdapter.addItem("我是新刷新到的第" + i + "条数据");
    }
    //更新UI(需要在UI线程)
    runOnUiThread(new Runnable() {
    @Override
    public void run() {
    simpleAdapter.notifyDataSetChanged();
    //刷新完成,关闭progressbar
    swipeRefreshLayout.setRefreshing(false);
    }
    });
    }
    }).start();
    }


    // ------------------- Mugen回调 ------------------
    //加载数据时的逻辑
    @Override
    public void onLoadMore() {
    //显示加载布局
    progressBar.setVisibility(View.VISIBLE);
    //上拉1.5秒后,加载10条数据
    new Thread(new Runnable() {
    @Override
    public void run() {
    //沉睡1.5秒
    try {
    Thread.sleep(1500);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    //加载10条新数据
    for (int i = 0; i < 10; i++) {
    simpleAdapter.addItem("我是新加载到的第" + i + "条数据");
    }
    //加载完成,更新UI
    runOnUiThread(new Runnable() {
    @Override
    public void run() {
    simpleAdapter.notifyDataSetChanged();
    //隐藏加载prb
    progressBar.setVisibility(View.GONE);
    }
    });

    }
    }).start();
    }

    //加载状态
    @Override
    public boolean isLoading() {
    //判断prb显示状态
    return progressBar.getVisibility() == View.VISIBLE;
    }

    //是否加载完所有数据
    // (如果返回true,则列表滑动到底部时,不再执行onLoadMore)
    // 如果返回false,列表滑动到底部时,执行onLoadMore
    @Override
    public boolean hasLoadedAllItems() {
    return false;
    }
    }

    =================下拉刷新,上拉加载,====封装=================

    TODO:待实现

  • 相关阅读:
    Python面向对象:杂七杂八的知识点
    初学Python常见异常错误,总有一处你会遇到!
    Python GUI开发,效率提升10倍的方法!
    Python里三个最高逼格的调试神器
    你见过的最全面的 Python 重点
    Python使用数字与字符串的技巧
    python3的eval和exec的区别与联系
    Python规范:提高可读性
    mysql使用mysqldump和crontab定时备份
    spring cloud stream集成rabbitmq
  • 原文地址:https://www.cnblogs.com/ll-ouyang/p/6638160.html
Copyright © 2011-2022 走看看