下拉刷新+Recycle+上拉刷新

下拉刷新:java

activity中:android

public class DemoBActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener{app

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

@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);
}ui

@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();
}
}this

adapter中實現對recycleView的setAdapter(●ˇ∀ˇ●)線程

package com.fuicuiedu.xc.resourceviewdemo_20170321;blog

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;ip

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:待實現

相關文章
相關標籤/搜索