添加RecycleView依赖
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
activity_main.xml布局中:
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<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>
定义footerview 的布局
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正在加载更多..."
android:textSize="25sp"
android:gravity="center"
android:padding="10dp"/>
定义headerVIew 的布局
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is header"
android:textSize="25sp"
android:gravity="center"
android:background="@color/colorAccent"
android:padding="10dp"/>
多条目中布局1
<TextView
android:id="@+id/tvContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:textSize="25sp"
android:background="#ff0"
android:text="哈哈哈"/>
多条目中布局2
<TextView
android:id="@+id/tvContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:textSize="25sp"
android:background="#00ff00"
android:text="哈哈哈"/>
MyAdapter中的代码
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<String> list;
/**
* 添加HEADER类型
*/
private static final int TYPE_HEADER = 0;
//Footer RecyclerView底部的一个View
private static final int TYPE_FOOTER = 1;
//RecyclerView 的条目
private static final int TYPE_ITEM = 2;
/**
* 多种条目加载的第二种
*/
private static final int TYPE_ITEM2 = 3;
public MyAdapter(Context context, List<String> list){
this.context = context;
this.list = list;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//viewType = 0 Header viewType = 1 footer
if(viewType == TYPE_HEADER){//HEADER
View headerView = LayoutInflater.from(context).inflate(R.layout.header,parent,false);
return new VHHeader(headerView);
}else if(viewType == TYPE_FOOTER){//Footer
View footerView = LayoutInflater.from(context).inflate(R.layout.footer,parent,false);
return new VHFooter(footerView);
}else if(viewType == TYPE_ITEM) {//ITEM 条目
View itemView = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
return new VHItem(itemView);
}else if(viewType == TYPE_ITEM2){
View itemView2 = LayoutInflater.from(context).inflate(R.layout.item2, parent, false);
return new VHItem(itemView2);
}
return null;
}
//定义条目点击事件的回调接口
public interface OnItemClickListener{
//提供一个条目点击事件的方法
void onItemClick(View view,int position);
//提供一个条目长按事件
void onItemLongClick(View view,int position);
}
private OnItemClickListener onItemClickListener;
//设置条目监听事件
public void setOnItemClick(OnItemClickListener onItemClickListener){
this.onItemClickListener = onItemClickListener;
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
if(holder instanceof VHHeader){
return;
}else if(holder instanceof VHFooter){
return;
}else if(holder instanceof VHItem) {
//将数据绑定到控件上
VHItem itemHolder = (VHItem) holder;
//设置文本
//0 ,1,2
itemHolder.tvContent.setText("嘻嘻 : "+list.get(position - 1));
}else if(holder instanceof VHItem2){
VHItem2 itemHolder2 = (VHItem2) holder;
itemHolder2.tvContent.setText("呵呵 - > "+list.get(position - 1));
}
//设置条目的点击事件与长按事件
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//得到条目位置
int position = holder.getLayoutPosition();
//将点击事件信息回调回去
onItemClickListener.onItemClick(holder.itemView,position);
}
});
//长按
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//得到条目位置
int position = holder.getLayoutPosition();
//将点击事件信息回调回去
onItemClickListener.onItemLongClick(holder.itemView,position);
return true;//长按事件优先于点击事件
}
});
}
/**
* 加载更多
*/
public void loadMore(List<String> dataList){
for (String str : dataList){
list.add(str);
}
//更新界面
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return list.size() + 2;
}
@Override
public int getItemViewType(int position) {
//定义每个条目展示的是什么类型的View
if(position == 0){
return TYPE_HEADER;
}else if(position == getItemCount() - 1){
return TYPE_FOOTER;
}else if(position %2 == 0){
return TYPE_ITEM2;
}else return TYPE_ITEM;
}
//Header 的ViewHolder
private class VHHeader extends RecyclerView.ViewHolder{
public VHHeader(View itemView) {
super(itemView);
}
}
//Footer VIEW 的 Holder
private class VHFooter extends RecyclerView.ViewHolder{
public VHFooter(View itemView) {
super(itemView);
}
}
//条目Holder
private class VHItem extends RecyclerView.ViewHolder{
TextView tvContent;
public VHItem(View itemView) {
super(itemView);
tvContent = (TextView) itemView.findViewById(R.id.tvContent);
}
}
//条目Holder
private class VHItem2 extends RecyclerView.ViewHolder{
TextView tvContent;
public VHItem2(View itemView) {
super(itemView);
tvContent = (TextView) itemView.findViewById(R.id.tvContent);
}
}
下面就看看MainActivity的代码
public class MainActivity extends AppCompatActivity implements MyAdapter.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener {
private RecyclerView recyclerView;
private List<String> lists;
private MyAdapter adapter;
private SwipeRefreshLayout swipeRefreshLayout;
private LinearLayoutManager linearLayoutManager;
/**
* 是否拉到底部了
*/
private boolean isLast;
private ProgressBar progressBar;
private Handler handler = new Handler();
private int lastVisibleItemPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
//找到实现下拉这个控件
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefresh);
//找到进度条控件
//progressBar = (ProgressBar) findViewById(R.id.progressBar);
//创建一个线性的管理器
linearLayoutManager = new LinearLayoutManager(this);
//设置布局管理器
recyclerView.setLayoutManager(linearLayoutManager);
adapter = new MyAdapter(this,lists);
//设置适配器
recyclerView.setAdapter(adapter);
//设置条目分割线
recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
//设置条目监听事件
adapter.setOnItemClick(this);
//实现下拉监听
swipeRefreshLayout.setOnRefreshListener(this);
//更改背景的颜色
swipeRefreshLayout.setProgressBackgroundColorSchemeColor
(getResources().getColor(android.R.color.holo_red_dark));
//设置旋转箭头颜色变更
swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_dark,android.R.color.holo_green_light,
android.R.color.holo_orange_dark);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
//监听滚动状态 IDEL 静止 手指滑动 惯性滑动
if(lastVisibleItemPosition + 1 ==
adapter.getItemCount() && newState == RecyclerView.SCROLL_STATE_IDLE){
handler.postDelayed(new Runnable() {
@Override
public void run() {
List<String> myList = new ArrayList<String>();
for (int i = 0;i < 10;++i){
myList.add("JAVA ,你去死"+i);
}
//将网络请求过来的数据更新到界面上
adapter.loadMore(myList);
}
},3000);
}
/* switch (newState){
case RecyclerView.SCROLL_STATE_IDLE:
{
Log.d("MainActivity", "静止");
if(isLast){
//模拟的我网络请求的数据
handler.postDelayed(new Runnable() {
@Override
public void run() {
List<String> myList = new ArrayList<String>();
for (int i = 0;i < 10;++i){
myList.add("JAVA ,你去死"+i);
}
//将网络请求过来的数据更新到界面上
adapter.loadMore(myList);
}
},3000);
}*/
//处理数据
/*if(!isLast){
//加载更多
//显示ProgressBar
Log.d("MainActivity", "静止----");
progressBar.setVisibility(View.VISIBLE);
handler.postDelayed(new Runnable() {
@Override
public void run() {
//隐藏进度条
progressBar.setVisibility(View.GONE);
}
},3000);
}
}
case RecyclerView.SCROLL_STATE_DRAGGING://用户手指滑动
{
}
break;
case RecyclerView.SCROLL_STATE_SETTLING://惯性滑动
{
}
break;
}*/
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
//最后一个可见条目
lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition();
//得到屏幕中可见的第一个条目
//int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
/*
//应该得到recyclerView的总条目数
int totalItemCount = linearLayoutManager.getItemCount();
Log.d("MainActivity", "第一个可见条目 : " + firstVisibleItemPosition +
"最后一个可见条目 : " + lastVisibleItemPosition+" 总条目数 : "
+totalItemCount);
if((firstVisibleItemPosition + lastVisibleItemPosition) == totalItemCount){
//滑动到了底部
Log.d("MainActivity", "滑动到底部了");
isLast = true;
}else{
//没有滑动到底部
Log.d("MainActivity", "没有滑动到底部");
isLast = false;
}*/
}
});
}
/**
* 初始化数据
*/
private void initData() {
//JDK 8 语法 菱形
lists = new ArrayList<>();
for (int i = 0; i < 10; ++i){
lists.add("Hello World - "+i);
}
}
@Override
public void onItemClick(View view, int position) {
Toast.makeText(this, "条目点击位置 : " + position, Toast.LENGTH_SHORT).show();
}
@Override
public void onItemLongClick(View view, int position) {
Toast.makeText(this, "条目长按事件 -> " + position, Toast.LENGTH_SHORT).show();
}
@Override
public void onRefresh() {
//当下拉的时候会被调用
Log.d("MainActivity", "下拉了");
}
}
}