欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android开发中RecyclerView隐藏item的所在区域显示空白的方法

程序员文章站 2024-01-24 08:20:46
说明: recyclerview隐藏当前item时候,由于为了优化性能,只是把内容隐藏,但是高度不会改变,这样就会出现一片空白区域。 解决方法: 手动改变隐藏的item高度为0。 代码: 比如要隐藏...

说明:

recyclerview隐藏当前item时候,由于为了优化性能,只是把内容隐藏,但是高度不会改变,这样就会出现一片空白区域。

解决方法:

手动改变隐藏的item高度为0。

代码:

比如要隐藏下面xml代码item




 

 

这里给了一个方法把ll_item这个布局id传入即可



 //防止隐藏item出现空白
 public void setvisibility(boolean isvisible, view itemview) {
  recyclerview.layoutparams param = (recyclerview.layoutparams) itemview.getlayoutparams();
  if (isvisible) {
param.height = relativelayout.layoutparams.wrap_content;// 这里注意使用自己布局的根布局类型
param.width = relativelayout.layoutparams.match_parent;// 这里注意使用自己布局的根布局类型
itemview.setvisibility(view.visible);
  } else {
itemview.setvisibility(view.gone);
param.height = 0;
param.width = 0;
  }
  itemview.setlayoutparams(param);
 }

完整代码:


package com.mandaotech.ewallet.android.ui.adapter;

import android.app.activity;
import android.content.intent;
import android.os.bundle;
import android.support.v7.widget.recyclerview;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.linearlayout;
import android.widget.relativelayout;
import android.widget.textview;

import com.mandaotech.ewallet.r;
import com.mandaotech.ewallet.android.ui.activity.elecaccount.accountaddrlistqryactivity;
import com.mandaotech.ewallet.android.ui.activity.elecaccount.accountoccupationactivity;
import com.mandaotech.ewallet.android.ui.activity.elecaccount.bean.accountoccupationbean;
import com.mandaotech.ewallet.android.ui.activity.elecaccount.bean.addrlistqrybean;
import com.mandaotech.ewallet.android.ui.activity.elecaccount.bean.addrlistqrybean.addlistqry;

import java.io.serializable;
import java.util.list;

/**
 * 省级市县
 * created by zst on 2018/7/12.
 */
public abstract class accountaddrlistqryadapter extends recyclerview.adapter {

 private list list;
 private int selectid = 0;
 private activity activity;
 private string searchkeyname = "";//搜索关键字
 private string addrlevel;//跳出自动+1,如果进来的是4则关闭返回省级市县对象

 public accountaddrlistqryadapter(activity activity, list list, string addrlevel) {
  this.activity = activity;
  this.list = list;
  this.addrlevel = addrlevel;
 }

 @override
 public viewholder oncreateviewholder(viewgroup parent, int viewtype) {
  view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.item_account_addrlist_qry, parent, false);
  return new viewholder(view);
 }

 @override
 public void onbindviewholder(viewholder holder, final int position) {
  holder.tv_column.settext(list.get(position).getaddrname());
  holder.tv_column.setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view view) {
 //setcolumn(list.get(position).getoccupation());

 int addrlevelint = integer.parseint(addrlevel);

 if (addrlevelint == 4) {//如果是最后一级直接关闭返回
  intent intent = new intent();
  bundle bundle = new bundle();
  bundle.putserializable(accountaddrlistqryactivity.intent_result_addrlist_qry_bean, (serializable) list.get(position));
  intent.putextras(bundle);
  intent.putextra(accountaddrlistqryactivity.intent_result_check_name, list.get(position).getaddrname());
  activity.setresult(activity.result_ok, intent);
  activity.finish();
 } else {//如果不是最后一级,向下一级跳转
  setcheckname(list.get(position).getaddrname());

  addrlevelint = addrlevelint + 1;

  intent intent = new intent(activity, accountaddrlistqryactivity.class);
  intent.putextra(accountaddrlistqryactivity.intent_request_addr_code, list.get(position).getaddrcode());
  intent.putextra(accountaddrlistqryactivity.intent_request_addr_level, addrlevelint + "");
  activity.startactivityforresult(intent, accountaddrlistqryactivity.intent_request_code);
 }


}
  });

  if (list.get(position).getaddrname().contains(searchkeyname)) {
setvisibility(true, holder.ll_item);
  } else {
setvisibility(false, holder.ll_item);
  }
 }

 @override
 public int getitemcount() {
  return list.size();
 }

 public static class viewholder extends recyclerview.viewholder {
  textview tv_column;
  linearlayout ll_item;

  public viewholder(view view) {
super(view);
tv_column = (textview) view.findviewbyid(r.id.tv_column);
ll_item = (linearlayout) view.findviewbyid(r.id.ll_item);
  }
 }

 //防止隐藏item出现空白
 public void setvisibility(boolean isvisible, view itemview) {
  recyclerview.layoutparams param = (recyclerview.layoutparams) itemview.getlayoutparams();
  if (isvisible) {
param.height = relativelayout.layoutparams.wrap_content;// 这里注意使用自己布局的根布局类型
param.width = relativelayout.layoutparams.match_parent;// 这里注意使用自己布局的根布局类型
itemview.setvisibility(view.visible);
  } else {
itemview.setvisibility(view.gone);
param.height = 0;
param.width = 0;
  }
  itemview.setlayoutparams(param);
 }

 public string getsearchkeyname() {
  return searchkeyname;
 }

 public void setsearchkeyname(string searchkeyname) {
  this.searchkeyname = searchkeyname;
 }

 public abstract void setcheckname(string checkname);
}