RecyclerView学习之旅--只有让自己变得优秀,才能遇到更优秀的别人
(三种布局:LinearLayoutManager,GridLayoutManager,StaggeredGridLayoutManager)
(加header)
private SwipeRefreshLayout swipeRefresh; //下拉刷新
private RecyclerView recyclerView; //列表
private List<Integer> datas = new ArrayList<>();
private RecyclerAdapter adapter;
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch(msg.what){
case 1:
swipeRefresh.setRefreshing(false); //刷新结束
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_component);
swipeRefresh = (SwipeRefreshLayout)findViewById(R.id.swipeRefresh);
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
for(int i=0;i<30;i++){
datas.add(i);
}
//第三个参数为true代表布局倒过来,从下往上看,false代表一般正常的布局,从上往下
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.VERTICAL,false));
// recyclerView.setLayoutManager(new GridLayoutManager(getApplicationContext(),2));
// recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,LinearLayoutManager.VERTICAL));
//添加分割线
recyclerView.addItemDecoration(new ItemDivider(getApplicationContext(),R.drawable.divider,LinearLayoutManager.VERTICAL));
adapter = new RecyclerAdapter();
View headerView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.dialog_progress,null);
//设置RecyclerView的Header的布局
adapter.setmHeaderView(headerView);
recyclerView.setAdapter(adapter);
//刷新事件
swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mHandler.sendEmptyMessageDelayed(1,5000);
}
});
//设置刷新过程中的转圈圈的位置
swipeRefresh.setProgressViewOffset(true,0,50);
//设置转圈圈的大小
swipeRefresh.setSize(SwipeRefreshLayout.LARGE);
//设置转圈圈转的过程中颜色的变化
swipeRefresh.setColorSchemeResources(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
}
class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private static final int TYPE_HEAD = 0; //item为头部
private static final int TYPE_NORMAL = 1; //普通item
private View mHeaderView;
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
if(mHeaderView != null && viewType == TYPE_HEAD){
return new Holder(mHeaderView);
}else{
View view = LayoutInflater.from(context).inflate(R.layout.item_layout,parent,false);
return new Holder(view);
}
}
//添加头部item
public void setmHeaderView(View mHeaderView) {
this.mHeaderView = mHeaderView;
notifyItemInserted(0);
}
//GridLayoutManager.setSpanSizeLookup()方法可以单独写,但是为了以后封装方便,放在RecyclerView.Adapter的onAttachedToRecyclerView()方法里面
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
if(manager instanceof GridLayoutManager){
final GridLayoutManager gridLayoutManager = (GridLayoutManager) manager;
//问题:未设置setSpanSizeLookup时,Header占据一个单元格,与内容并排
//设置RecyclerView的Header占据一整行,也就是占据GridLayoutManager设置的每行单元格数
//只有GridLayoutManager的Header需要设置,LinearLayoutManager的Header不需要设置
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
//这个方法的返回值决定了我们每个position上的item占据的单元格个数
if(getItemViewType(position) == TYPE_HEAD){
return gridLayoutManager.getSpanCount();
}else{
return 1;
}
}
});
}
}
@Override
public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
super.onViewAttachedToWindow(holder);
ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();
if(params != null && params instanceof StaggeredGridLayoutManager.LayoutParams && holder.getLayoutPosition() == 0){
StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams)params;
//通过LayoutParams来控制StaggeredGridLayoutManager的header占据一整行
layoutParams.setFullSpan(true);
}
}
public View getmHeaderView() {
return mHeaderView;
}
//通过ItemType区分是头部还是普通item
@Override
public int getItemViewType(int position) {
if(mHeaderView == null) return TYPE_NORMAL;
if(position == 0){
return TYPE_HEAD;
}else{
return TYPE_NORMAL;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(getItemViewType(position) == TYPE_HEAD)return;
if(holder instanceof Holder){
switch (holder.getLayoutPosition() % 3){
case 0:
((Holder) holder).textView.setBackgroundColor(Color.MAGENTA);
break;
case 1:
((Holder) holder).textView.setBackgroundColor(Color.GREEN);
break;
case 2:
((Holder) holder).textView.setBackgroundColor(Color.YELLOW);
break;
}
((Holder) holder).textView.setText(datas.get(position-1)+"");
}
}
@Override
public int getItemCount() {
if(mHeaderView != null){
return datas.size() + 1; //有头部的时候
}else{
return datas.size(); //没有头部的时候
}
}
class Holder extends RecyclerView.ViewHolder{
private TextView textView;
public Holder(View view){
super(view);
if(view == mHeaderView)return; //头部item的视图
textView = (TextView)view.findViewById(R.id.textView); //普通item的视图
view.setTag(this);
}
}
class RecyclerHolder extends RecyclerView.ViewHolder{
private TextView textView;
public RecyclerHolder(View view){
super(view);
textView = (TextView)view.findViewById(R.id.textView);
view.setTag(this);
}
}
}
(三种布局:LinearLayoutManager,GridLayoutManager,StaggeredGridLayoutManager)
(加header)
private SwipeRefreshLayout swipeRefresh; //下拉刷新
private RecyclerView recyclerView; //列表
private List<Integer> datas = new ArrayList<>();
private RecyclerAdapter adapter;
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch(msg.what){
case 1:
swipeRefresh.setRefreshing(false); //刷新结束
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_component);
swipeRefresh = (SwipeRefreshLayout)findViewById(R.id.swipeRefresh);
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
for(int i=0;i<30;i++){
datas.add(i);
}
//第三个参数为true代表布局倒过来,从下往上看,false代表一般正常的布局,从上往下
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.VERTICAL,false));
// recyclerView.setLayoutManager(new GridLayoutManager(getApplicationContext(),2));
// recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,LinearLayoutManager.VERTICAL));
//添加分割线
recyclerView.addItemDecoration(new ItemDivider(getApplicationContext(),R.drawable.divider,LinearLayoutManager.VERTICAL));
adapter = new RecyclerAdapter();
View headerView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.dialog_progress,null);
//设置RecyclerView的Header的布局
adapter.setmHeaderView(headerView);
recyclerView.setAdapter(adapter);
//刷新事件
swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mHandler.sendEmptyMessageDelayed(1,5000);
}
});
//设置刷新过程中的转圈圈的位置
swipeRefresh.setProgressViewOffset(true,0,50);
//设置转圈圈的大小
swipeRefresh.setSize(SwipeRefreshLayout.LARGE);
//设置转圈圈转的过程中颜色的变化
swipeRefresh.setColorSchemeResources(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
}
class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private static final int TYPE_HEAD = 0; //item为头部
private static final int TYPE_NORMAL = 1; //普通item
private View mHeaderView;
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
if(mHeaderView != null && viewType == TYPE_HEAD){
return new Holder(mHeaderView);
}else{
View view = LayoutInflater.from(context).inflate(R.layout.item_layout,parent,false);
return new Holder(view);
}
}
//添加头部item
public void setmHeaderView(View mHeaderView) {
this.mHeaderView = mHeaderView;
notifyItemInserted(0);
}
//GridLayoutManager.setSpanSizeLookup()方法可以单独写,但是为了以后封装方便,放在RecyclerView.Adapter的onAttachedToRecyclerView()方法里面
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
if(manager instanceof GridLayoutManager){
final GridLayoutManager gridLayoutManager = (GridLayoutManager) manager;
//问题:未设置setSpanSizeLookup时,Header占据一个单元格,与内容并排
//设置RecyclerView的Header占据一整行,也就是占据GridLayoutManager设置的每行单元格数
//只有GridLayoutManager的Header需要设置,LinearLayoutManager的Header不需要设置
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
//这个方法的返回值决定了我们每个position上的item占据的单元格个数
if(getItemViewType(position) == TYPE_HEAD){
return gridLayoutManager.getSpanCount();
}else{
return 1;
}
}
});
}
}
@Override
public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
super.onViewAttachedToWindow(holder);
ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();
if(params != null && params instanceof StaggeredGridLayoutManager.LayoutParams && holder.getLayoutPosition() == 0){
StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams)params;
//通过LayoutParams来控制StaggeredGridLayoutManager的header占据一整行
layoutParams.setFullSpan(true);
}
}
public View getmHeaderView() {
return mHeaderView;
}
//通过ItemType区分是头部还是普通item
@Override
public int getItemViewType(int position) {
if(mHeaderView == null) return TYPE_NORMAL;
if(position == 0){
return TYPE_HEAD;
}else{
return TYPE_NORMAL;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(getItemViewType(position) == TYPE_HEAD)return;
if(holder instanceof Holder){
switch (holder.getLayoutPosition() % 3){
case 0:
((Holder) holder).textView.setBackgroundColor(Color.MAGENTA);
break;
case 1:
((Holder) holder).textView.setBackgroundColor(Color.GREEN);
break;
case 2:
((Holder) holder).textView.setBackgroundColor(Color.YELLOW);
break;
}
((Holder) holder).textView.setText(datas.get(position-1)+"");
}
}
@Override
public int getItemCount() {
if(mHeaderView != null){
return datas.size() + 1; //有头部的时候
}else{
return datas.size(); //没有头部的时候
}
}
class Holder extends RecyclerView.ViewHolder{
private TextView textView;
public Holder(View view){
super(view);
if(view == mHeaderView)return; //头部item的视图
textView = (TextView)view.findViewById(R.id.textView); //普通item的视图
view.setTag(this);
}
}
class RecyclerHolder extends RecyclerView.ViewHolder{
private TextView textView;
public RecyclerHolder(View view){
super(view);
textView = (TextView)view.findViewById(R.id.textView);
view.setTag(this);
}
}
}