安卓高级3 RecyclerView结合SwipeRefreshLayout并添加上拉
目录结构:
效果图:
MainActivity.java
package qianfeng.com.pullrecyclerviewdemo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private SwipeRefreshLayout swipeLayout;
private RecyclerView recyclerView;
private List<String> total = new ArrayList<>();
private MyAdapter myAdapter;
boolean isReferencing;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 100:
isReferencing = false;
swipeLayout.setRefreshing(false);
myAdapter.notifyDataSetChanged();
break;
case 200:
isReferencing = false;
// 添加数据结束后 将 空的那条数据 移除 在刷新
total.remove("");
myAdapter.notifyDataSetChanged();
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipeLayout);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
final LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(manager);
initData();
myAdapter = new MyAdapter(total, this);
recyclerView.setAdapter(myAdapter);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int lastVisibleItemPosition = manager.findLastVisibleItemPosition();
if (lastVisibleItemPosition == myAdapter.getItemCount() - 1) {
isReferencing = swipeLayout.isRefreshing();
if (!isReferencing) {
isReferencing = true;
initData();
handler.sendEmptyMessageDelayed(200, 3000);
Toast.makeText(MainActivity.this, "最后拉 ,不要在向下了", Toast.LENGTH_SHORT).show();
}
}
}
});
swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Thread() {
@Override
public void run() {
super.run();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
total.clear();
initData();
handler.sendEmptyMessage(100);
}
}.start();
}
});
}
private void initData() {
for (int i = 0; i < 50; i++) {
total.add("都别睡觉啊~ 逮住" + i);
}
// 每次加载数据的时候添加一条空数据 作为 footerView 展示的 itemView
total.add("");
}
public void onClick(View view) {
// myAdapter.delete(2);
}
}
MyAdapter.java
package qianfeng.com.pullrecyclerviewdemo;
import android.content.Context;
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.List;
/**
* Created by ${Mr.Zhao} on 2016/10/19.
*/
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<String> list;
LayoutInflater inflater;
//1. 定义两种类型
private final int TYPE_FOOTER_VIEW = 0;
private final int TYPE_ITEM_VIEW = 1;
public MyAdapter(List<String> list, Context context) {
this.list = list;
inflater = LayoutInflater.from(context);
}
@Override
public int getItemViewType(int position) {
if (position == getItemCount() - 1)
return TYPE_FOOTER_VIEW;
return TYPE_ITEM_VIEW;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_ITEM_VIEW) {
View view = inflater.inflate(R.layout.item_layout, parent, false);
return new MyViewHolder(view);
} else {
View view = inflater.inflate(R.layout.footer_layout, parent, false);
return new FooterViewHolder(view);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof MyViewHolder) {
((MyViewHolder) holder).item_tv.setText(list.get(position));
}
}
@Override
public int getItemCount() {
return list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView item_tv;
public MyViewHolder(View itemView) {
super(itemView);
item_tv = (TextView) itemView.findViewById(R.id.item_tv);
}
}
class FooterViewHolder extends RecyclerView.ViewHolder {
public FooterViewHolder(View itemView) {
super(itemView);
}
}
// 待验证
// public void delete(int position) {
// notifyItemRemoved(position);
// }
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="qianfeng.com.pullrecyclerviewdemo.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="删除" />
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
footer_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="加载更多"
android:textColor="@android:color/black"
android:textSize="20sp" />
</LinearLayout>
item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:foreground="?android:attr/selectableItemBackground"
android:orientation="vertical"
app:cardBackgroundColor="@android:color/holo_blue_light"
app:cardCornerRadius="10dp"
app:cardElevation="10dp">
<TextView
android:id="@+id/item_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="30dp"
android:layout_marginTop="30dp"
android:textColor="@android:color/white"
android:textSize="20sp" />
</android.support.v7.widget.CardView>