recycleview 网格布局setSpanSizeLooku
2020-12-01 本文已影响0人
jxtx
1.空白布局
GridLayoutManager layoutManager=new GridLayoutManager(getContext(),3);
binding.rlvGrade.setLayoutManager(layoutManager);
binding.rlvGrade.setAdapter(adapter);
//设置空白数据时,空白布局占据的spanCount
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
int viewType = adapter.getItemViewType(position);
if(viewType == -1) {
return 3;
}
return 1;
}
});
package com.mbzj.ykt_student.adapter;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import com.mbzj.ykt_student.R;
import com.mbzj.ykt_student.bean.GradeBean;
import java.util.List;
/**
* @author David
* @description:
* @date :2020/7/28 18:02
*/
public class UserMessageSelectClassAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private int TYPE_EMPTY=-1;
private int TYPE_DATA=1;
private Context context;
private List<GradeBean> list;
private onClickListener listener;
public UserMessageSelectClassAdapter(Context context, List<GradeBean> list,onClickListener listener) {
this.context = context;
this.list = list;
this.listener = listener;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType==TYPE_EMPTY){
return new EmptyViewHolder(LayoutInflater.from(context).inflate(R.layout.rlv_select_class_empty_item,parent,false));
}else {
return new SelectClassViewHolder(LayoutInflater.from(context).inflate(R.layout.rlv_user_msg_select_class_item,parent,false));
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder instanceof SelectClassViewHolder){
onBindSelectClassViewHolder((SelectClassViewHolder) holder,position);
}else if (holder instanceof EmptyViewHolder){
onBindEmptyViewHolder((EmptyViewHolder) holder,position);
}
// int itemViewType = getItemViewType(position);
// if (itemViewType==TYPE_EMPTY){
// onBindEmptyViewHolder((EmptyViewHolder) holder,position);
// }else {
// onBindSelectClassViewHolder((SelectClassViewHolder) holder,position);
// }
}
private void onBindEmptyViewHolder(EmptyViewHolder holder, int position) {
holder.tv_empty.setOnClickListener(v -> {
listener.refresh();
});
}
public void onBindSelectClassViewHolder(@NonNull SelectClassViewHolder holder, int position) {
GradeBean gradeBean = list.get(position);
holder.tv_class.setText(gradeBean.getGradeName());
if (selectPostion==position){
holder.tv_class.setSelected(true);
// holder.tv_class.setTextColor(ContextCompat.getColor(context,R.color.white));
}else {
// holder.tv_class.setTextColor(ContextCompat.getColor(context,R.color.color_666666));
holder.tv_class.setSelected(false);
}
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.tv_class.getLayoutParams();
if (position%3==0){
params.gravity= Gravity.LEFT;
}else if (position%3==1){
params.gravity= Gravity.CENTER_HORIZONTAL;
}else {
params.gravity= Gravity.RIGHT;
}
holder.tv_class.setOnClickListener(view -> {
listener.selected(position,list);
});
}
@Override
public int getItemViewType(int position) {
if (list==null || list.size()==0){
return TYPE_EMPTY;
}else {
return TYPE_DATA;
}
}
@Override
public int getItemCount() {
return list==null || list.size()==0 ?1:list.size();
}
private int selectPostion=-1;
public void update(int postion){
selectPostion=postion;
notifyDataSetChanged();
}
public void setNewData(List<GradeBean> gradeBeans) {
if (gradeBeans==null){
return;
}
list.clear();
list.addAll(gradeBeans);
notifyDataSetChanged();
}
public interface onClickListener{
void selected(int position, List<GradeBean> list);
void refresh();
}
class SelectClassViewHolder extends RecyclerView.ViewHolder{
TextView tv_class;
public SelectClassViewHolder(@NonNull View itemView) {
super(itemView);
tv_class=itemView.findViewById(R.id.tv_class);
}
}
class EmptyViewHolder extends RecyclerView.ViewHolder{
TextView tv_empty;
public EmptyViewHolder(@NonNull View itemView) {
super(itemView);
tv_empty=itemView.findViewById(R.id.tv_empty);
}
}
}
rlv_user_msg_select_class_item:
<?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:layout_marginBottom="19dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_class"
android:layout_width="55dp"
android:layout_height="22dp"
android:background="@drawable/select_class_bg"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:text="初二"
android:textColor="@color/select_class_text_color"
android:textSize="9sp" />
</LinearLayout>
rlv_select_class_empty_item:
<?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="match_parent">
<TextView
android:id="@+id/tv_empty"
android:textSize="12sp"
android:text="刷新"
android:gravity="center"
android:textColor="@color/color_333333"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
注意事项:
1.不能自定义的网格分割线binding.rlvGrade.addItemDecoration(gridSpacingItemDecoration);
原因: setSpanSizeLookup中getSpanSize返回3时还在计算间隔距离,可以再自定义的分割线GridDividerItemDecoration中getItemOffsets的方法中添加判断传入的viewType不去计算距离(个人观点)
解决方法:不使用自定义的分割线,使用代码控制距离
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.tv_class.getLayoutParams();
if (position%3==0){
params.gravity= Gravity.LEFT;
}else if (position%3==1){
params.gravity= Gravity.CENTER_HORIZONTAL;
}else {
params.gravity= Gravity.RIGHT;
}
注意:当作为父layout的LinearLayout的属性为android:orientation="vertical" 的时候,android:layout_gravity="?"这里设为横向的时候才能生效。比如:left,right,center_horizontal等
当作为父layout的LinearLayout的属性为android:orientation="horizental" 的时候,android:layout_gravity="?"这里设为纵向的时候才能生效。比如:top,bottom,center_vertical等;