上图
没有什么注释 没有demo 但是 思路清晰 好好理解看一下
xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swipe_refresh_widget"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.jsu.whr.recyclerview.Main3Activity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycle_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
主activity
package com.jsu.whr.recyclerview;
import android.os.Handler;
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 com.jsu.whr.myapplication.R;
import java.util.ArrayList;
import java.util.List;
public class Main3Activity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
SwipeRefreshLayout mSwipeRefreshLayout;
RecyclerView mRecyclerView;
MyAdapter adapter;
List<String> list = new ArrayList<String>();
LinearLayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
mSwipeRefreshLayout = (SwipeRefreshLayout) this.findViewById(R.id.swipe_refresh_widget);
mSwipeRefreshLayout.setColorSchemeResources(
R.color.black,
R.color.read,
R.color.green,
R.color.blue);
mSwipeRefreshLayout.setOnRefreshListener(this);//下拉监听
mRecyclerView = (RecyclerView) this.findViewById(R.id.recycle_view);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setOnScrollListener(mOnScrollListener);
adapter = new MyAdapter(this);
mRecyclerView.setAdapter(adapter);
}
private RecyclerView.OnScrollListener mOnScrollListener = new RecyclerView.OnScrollListener() {
private int lastVisibleItem;//窗体可见最后一个item
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE//滑动状态 SCROLL_STATE_IDLE表示停止
&& lastVisibleItem + 1 == adapter.getItemCount()
&& adapter.isShowFooter()) {//条件表示 滑到最底端了
//加载更多
handler.postDelayed(new Runnable() {
@Override
public void run() {
list.addAll(loadNewData());
adapter.notifyDataSetChanged();
}
},1000);
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();
}
};
@Override
public void onRefresh() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
if(list!=null){
list.clear();
}
list.addAll(loadData());
adapter.setDatalist(list);
mSwipeRefreshLayout.setRefreshing(false);
}
},4000);
}
public List<String> loadData(){
List<String> listdata = new ArrayList<String>();
for(int i=0;i<20;i++){
list.add("---->"+i);
}
return listdata;
}
public List<String> loadNewData(){
List<String> listdata = new ArrayList<String>();
for(int i=0;i<20;i++){
list.add("---xin->"+i);
}
return listdata;
}
Handler handler = new Handler(){
};
}
adapter
package com.jsu.whr.recyclerview;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.jsu.whr.myapplication.R;
import java.util.ArrayList;
import java.util.List;
/**
* Created by k90 on 2016-09-02.
*/
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
ItemViewHolder mItemViewHolder;
List<String>Datalist = new ArrayList<String>();
Context context;
private boolean mShowFooter = true;
private static final int TYPE_ITEM = 0;
private static final int TYPE_FOOTER = 1;
public void setDatalist(List<String> datalist) {
Datalist = datalist;
this.notifyDataSetChanged();
}
public MyAdapter(Context context) {
this.context = context;
}
@Override
public int getItemViewType(int position) {
// 最后一个item设置为footerView
if(!mShowFooter) {
return TYPE_ITEM;
}
if (position + 1 == getItemCount()) {
return TYPE_FOOTER;
} else {
return TYPE_ITEM;
}
}
public boolean isShowFooter() {
return this.mShowFooter;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
if(viewType == TYPE_ITEM) {
View view = LayoutInflater.from(context).inflate(R.layout.items, viewGroup, false);
mItemViewHolder = new ItemViewHolder(view);
return mItemViewHolder;
}
else{
View view = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.footer, null);
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
return new FooterViewHolder(view);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
if(viewHolder instanceof ItemViewHolder) {
String s = Datalist.get(i);
if (s == null) {
return;
}
((ItemViewHolder) viewHolder).tv.setText(s);
}
}
@Override
public int getItemCount() {
return Datalist.size();
}
public class FooterViewHolder extends RecyclerView.ViewHolder {
public FooterViewHolder(View view) {
super(view);
}
}
public void isShowFooter(boolean showFooter) {
this.mShowFooter = showFooter;
}
class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView tv;
public ItemViewHolder(View itemView) {
super(itemView);
tv = (TextView) itemView.findViewById(R.id.tv);
tv.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(mOnItemClickListtenet!=null){
mOnItemClickListtenet.onItemClick(v,this.getPosition());
}
}
}
OnItemClickListtenet mOnItemClickListtenet;
public interface OnItemClickListtenet{
void onItemClick(View v ,int position);
}
public void setmOnItemClickListtenet(OnItemClickListtenet mOnItemClickListtenet) {
this.mOnItemClickListtenet = mOnItemClickListtenet;
}
}
底部layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/load_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:paddingBottom="12dip"
android:paddingTop="12dip" >
<ProgressBar
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/more_data_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_marginLeft="10dp"
android:text="正在加载..." />
</LinearLayout>