框架地址:https://github.com/XRecyclerView/XRecyclerView
本文只是针对这个框架的使用
效果图:
1、引入依赖:
compile 'com.jcodecraeer:xrecyclerview:1.3.2'
2、布局使用XRecycler:
3、在MainActivity获取对象,并添加各种设置,扩展性很好:
package com.booyue.recycler;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.booyue.recycler.adapter.BaseRecyclerAdapter;
import com.booyue.recycler.adapter.MainAdapter;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {private XRecyclerView xRecyclerView;
private BaseRecyclerAdapter mAdapter;
private int page;
private View empty;
private List list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//设置布局方式
xRecyclerView = (XRecyclerView) findViewById(R.id.xRecyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
xRecyclerView.setLayoutManager(linearLayoutManager);
list = new ArrayList<>();
List list1 = loadData();
if(list1 != null){
list.addAll(list1);
}
empty = LayoutInflater.from(this).inflate(R.layout.layout_empty,null);
//设置适配器
mAdapter = new MainAdapter(this, list);
xRecyclerView.setAdapter(mAdapter);
xRecyclerView.setEmptyView(empty);
//设置空布局
mAdapter.setOnItemClickLinster(new BaseRecyclerAdapter.OnItemClickListener() {
@Override
public void onItemClickListener(View view, int pos,Object object) {
if(object instanceof String){
String data = https://www.it610.com/article/(String) object;
Toast.makeText(MainActivity.this,pos +"---data = "https://www.it610.com/article/+ data,Toast.LENGTH_SHORT).show();
}
}
});
//设置加载的样式
xRecyclerView.setRefreshProgressStyle(ProgressStyle.BallBeat);
xRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallBeat);
//or xRecyclerView.setRefreshProgressStyle(ProgressStyle.BallClipRotateMultiple);
//如果布标不满意,可以另外设置
//xRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);
//设置是否开启,如果想禁用传false就可以了
xRecyclerView.setPullRefreshEnabled(true);
xRecyclerView.setLoadingMoreEnabled(true);
//添加头布局
View header =LayoutInflater.from(this).inflate(R.layout.recyclerview_header,
(ViewGroup)findViewById(android.R.id.content),false);
View header1 =LayoutInflater.from(this).inflate(R.layout.recyclerview_header1,
(ViewGroup)findViewById(android.R.id.content),false);
xRecyclerView.addHeaderView(header);
MainAdapter.HEADER_NUMBER ++;
xRecyclerView.addHeaderView(header1);
MainAdapter.HEADER_NUMBER ++;
//设置刷新和加载监听
xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
xRecyclerView.postDelayed(new Runnable() {
@Override
public void run() {
xRecyclerView.refreshComplete();
}
},1500);
}@Override
public void onLoadMore() {
xRecyclerView.postDelayed(new Runnable() {
@Override
public void run() {
List list2 = loadData();
if(list2 != null){
mAdapter.addAll(list2);
}
xRecyclerView.loadMoreComplete();
}
},1500);
}
});
}
private int insertNum = 0;
private List loadData(){
if(page> 10) return null;
List list1 = new ArrayList();
for (int i = page * 10;
i < 10 * (page + 1);
i++) {
list1.add(i + "");
}
page += 1;
return list1;
}
private List insertData(){
if(page > 10) return null;
List list1 = new ArrayList();
for (int i = page * 10;
i < 10 * (page + 1);
i++) {
list1.add("插入" + i);
}
page += 1;
return list1;
}@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
return true;
}@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.insert:
//插入一条数据
//list.add(4,"ddddd");
//mAdapter.notifyItemInserted(4);
//mAdapter.notifyItemRangeChanged(4,list.size()-4);
//插入一串数据
List list1 = insertData();
list.addAll(4,list1);
mAdapter.notifyItemRangeInserted(4,list1.size());
mAdapter.notifyItemRangeChanged(4,list.size()-4 + list1.size());
break;
case R.id.delete:
list.remove(4);
mAdapter.notifyItemRemoved(4);
mAdapter.notifyItemRangeChanged(4, list.size()-4);
break;
}
return super.onOptionsItemSelected(item);
}
}
5、xRecycler需要的适配器基类
package com.booyue.recycler.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
/**
* Created by Administrator on 2018/5/16.16:37
*/public abstract class BaseRecyclerAdapter extends RecyclerView.Adapter{
public List list;
public Context context;
public LayoutInflater layoutInflater;
public BaseRecyclerAdapter(Context context,List list) {
this.context = context;
this.list = list;
layoutInflater = LayoutInflater.from(context);
}@Override
public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return onCreateViewHolderImpl(parent,viewType);
}@Override
public void onBindViewHolder(BaseRecyclerViewHolder holder, int position) {
onBindViewHolderImpl(holder,position);
}
public abstract BaseRecyclerViewHolder onCreateViewHolderImpl(ViewGroup parent, int viewType);
public abstract void onBindViewHolderImpl(BaseRecyclerViewHolder holder, int position);
@Override
public int getItemCount() {
return list.size();
}/**
*
* @param list
*/
public void addAll(List list){
if(list != null && list.size() > 0){
this.list.addAll(list);
}
}public interface OnItemClickListener {
void onItemClickListener(View view, int pos, Object object);
}
public OnItemClickListener onItemClickListener;
public void setOnItemClickLinster(OnItemClickListener listener) {
this.onItemClickListener = listener;
}}
3、MainActivity的适配器MainAdapter:
import com.booyue.recycler.R;
import java.util.List;
/**
* Created by Administrator on 2018/5/16.16:45
*/public class MainAdapter extends BaseRecyclerAdapter {
//头布局的个数,默认只有一个下拉刷新,如果通过xRecyclerView.addHeaderView(header);
// 添加了头布局,此值需要相应的增加
public static int HEADER_NUMBER = 1;
public MainAdapter(Context context, List list) {
super(context, list);
}@Override
public BaseRecyclerViewHolder onCreateViewHolderImpl(ViewGroup parent, int viewType) {
View convertView = layoutInflater.inflate(R.layout.main_activity_list_item,parent,false);
MainItemViewHolder mainItemViewHolder = new MainItemViewHolder(convertView);
return mainItemViewHolder;
}@Override
public void onBindViewHolderImpl(final BaseRecyclerViewHolder holder, final int position) {
if (holder instanceof MainItemViewHolder) {
MainItemViewHolder mainItemViewHolder = (MainItemViewHolder) holder;
mainItemViewHolder.TVName.setText(list.get(position));
final View itemView = holder.itemView;
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onItemClickListener != null) {
int adapterPosition = holder.getAdapterPosition();
if((adapterPosition - HEADER_NUMBER) < list.size() && (adapterPosition - HEADER_NUMBER) >= 0){//一个刷新布局和两个头布局
onItemClickListener.onItemClickListener(itemView, adapterPosition,
list.get(adapterPosition - HEADER_NUMBER));
}
}
}
});
}}}
6、基类viewholder:
public class BaseRecyclerViewHolder extends RecyclerView.ViewHolder{
public BaseRecyclerViewHolder(View itemView) {
super(itemView);
}}
7、MainAdapter的viewholder:
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.booyue.recycler.R;
/**
* Created by Administrator on 2018/5/16.16:51
*/public class MainItemViewHolder extends BaseRecyclerViewHolder {
public TextView TVName;
public TextView tvDesc;
public ImageView ivIcon;
public MainItemViewHolder(View itemView) {
super(itemView);
TVName = (TextView) itemView.findViewById(R.id.tv_name);
tvDesc = (TextView) itemView.findViewById(R.id.tv_desc);
ivIcon = (ImageView) itemView.findViewById(R.id.iv_icon);
}
}
8、menu菜单
最后奉上完整资源: https://download.csdn.net/download/a1527238987/10432835
【Android 为Recyclerview添加头布局,下拉刷新,上拉加载】