在设置recyclerview的时候我们常常会遇到类似如下图片显示的情况,最后一个item总是显示一半?那么怎么快速解决呢?
基于item本身的容器,我们其实可以在默认再添加一个容器然后不显示即可,修改代码如下:
修改处1 将本就不存在的item置为INVISION 顺便处理好你本身的item点击逻辑,避免干扰
@Override
public void onBindViewHolder(@NonNull SViewHolder holder, int position) {
if (position==getItemCount()-1){
holder.itemView.setVisibility(View.INVISIBLE);//修改处1 将本就不存在的item置为INVISION 顺便处理好你本身的item点击逻辑,避免干扰
}else {
//...原来的逻辑
}
}
修改处2 适配选项器底部显示不全问题
@Override
public int getItemCount() {
return selectorList.size()+1;//修改处2 增加1个底部item 适配选项器底部显示不全问题
}
adapter完整代码
package com.ui.design.main.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.alibaba.android.arouter.launcher.ARouter;
import com.ui.design.R;
import com.ui.design.main.bean.SelectorBean;
import java.util.List;
/**
* 选项适配器
* tangxianfeng
* 2021.7.21
*/
public class SelectorAdapter extends RecyclerView.Adapter<SelectorAdapter.SViewHolder> {
private final List<SelectorBean> selectorList;
private final Context mContext;
public SelectorAdapter(Context context, List<SelectorBean> selectors){
this.mContext=context;
this.selectorList=selectors;
}
@NonNull
@Override
public SViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new SViewHolder(LayoutInflater.from(mContext).inflate(R.layout.selector_item, parent,false));
}
@Override
public void onBindViewHolder(@NonNull SViewHolder holder, int position) {
if (position==getItemCount()-1){
holder.itemView.setVisibility(View.INVISIBLE);//修改处1 将本就不存在的item置为INVISION 顺便处理好你本身的item点击逻辑,避免干扰
}else {
holder.cardname.setText(selectorList.get(position).Selectorname);
holder.itemView.setOnClickListener(v -> {
ARouter.getInstance().build(selectorList.get(position).routeName).withString("title",selectorList.get(position).Selectorname).withString("blogurl",selectorList.get(position).blogurl).navigation();
});
}
}
@Override
public int getItemCount() {
return selectorList.size()+1;//修改处2 适配选项器底部显示不全问题
}
static class SViewHolder extends RecyclerView.ViewHolder{
private final TextView cardname;
public SViewHolder(@NonNull View itemView) {
super(itemView);
cardname=(TextView) itemView.findViewById(R.id.card_selector_name);
}
}
}
尾部增加1个item 修改效果
或许可能上面最后一个item可能还是欠点意思,没有完全显示,这时再加一个item就行
adapter完整代码
package com.ui.design.main.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.alibaba.android.arouter.launcher.ARouter;
import com.ui.design.R;
import com.ui.design.main.bean.SelectorBean;
import java.util.List;
/**
* 选项适配器
* tangxianfeng
* 2021.7.21
*/
public class SelectorAdapter extends RecyclerView.Adapter<SelectorAdapter.SViewHolder> {
private final List<SelectorBean> selectorList;
private final Context mContext;
public SelectorAdapter(Context context, List<SelectorBean> selectors){
this.mContext=context;
this.selectorList=selectors;
}
@NonNull
@Override
public SViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new SViewHolder(LayoutInflater.from(mContext).inflate(R.layout.selector_item, parent,false));
}
@Override
public void onBindViewHolder(@NonNull SViewHolder holder, int position) {
if (position==getItemCount()-1||position==getItemCount()-2){
holder.itemView.setVisibility(View.INVISIBLE);//修改处1 将本就不存在的item置为INVISION 顺便处理好你本身的item点击逻辑,避免干扰
}else {
holder.cardname.setText(selectorList.get(position).Selectorname);
holder.itemView.setOnClickListener(v -> {
ARouter.getInstance().build(selectorList.get(position).routeName).withString("title",selectorList.get(position).Selectorname).withString("blogurl",selectorList.get(position).blogurl).navigation();
});
}
}
@Override
public int getItemCount() {
return selectorList.size()+2;//修改处2 适配选项器底部显示不全问题
}
static class SViewHolder extends RecyclerView.ViewHolder{
private final TextView cardname;
public SViewHolder(@NonNull View itemView) {
super(itemView);
cardname=(TextView) itemView.findViewById(R.id.card_selector_name);
}
}
}
尾部增加2个item 修改效果
如果直接复制可能会出现代码缺陷,完整代码请去仓库下载
如果觉得还行,耽误您几秒钟的时间去我的仓库点点star,万一以后用到了呢?
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)