[英]How to hide/display specific RecyclerView item?
我有一个联系人列表 RecyclerView,其中第一项(位置 0)保存用户的详细信息。 当按下工具栏中的搜索图标时,我希望该条目对用户隐藏。
我试过在它上面使用setVisibility(View.GONE)
,虽然条目被隐藏了,但它占用的空间仍然存在,就像设置setVisibility(View.INVISIBLE)
。 如何在我的 RecyclerView 中将此特定条目的可见性切换为 VISIBLE/GONE,或者如何仅将其高度设置为 0dp?
编辑:
我的 xml 很简单,它只包含一个 recyclerView 和一个 FAB。 我的适配器代码如下。
ContactsAdapter(
Context context,
List<LinphoneContact> contactsList,
ContactViewHolder.ClickListener clickListener,
SelectableHelper helper) {
super(helper);
mContext = context;
updateDataSet(contactsList);
mClickListener = clickListener;
}
@NonNull
@Override
public ContactViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// For the first position only, use the user's own contact card
// For all the rest, use the contact card
switch (viewType) {
case USER_CARD_VIEW:
View userCard =
LayoutInflater.from(parent.getContext())
.inflate(R.layout.contacts_user_card, parent, false);
return new ContactViewHolder(userCard, mClickListener);
case CONTACT_CARD_VIEW:
View contactCard =
LayoutInflater.from(parent.getContext())
.inflate(R.layout.contact_cell, parent, false);
return new ContactViewHolder(contactCard, mClickListener);
}
return null;
}
@Override
public void onBindViewHolder(@NonNull ContactViewHolder holder, int position) {
// Remove the user's card when searching contacts - DOES NOT WORK, MAKES ALL FIRST ITEMS
// INVISIBLE
// if (position == 0) {
// if (mIsSearchMode) holder.itemView.setVisibility(View.GONE);
// else holder.itemView.setVisibility(View.VISIBLE);
// }
if (position != 0) {
LinphoneContact contact = (LinphoneContact) getItem(position - 1);
holder.name.setText(contact.getFullName());
if (!mIsSearchMode) {
String fullName = contact.getFullName();
if (fullName != null && !fullName.isEmpty()) {
holder.separatorText.setText(String.valueOf(fullName.charAt(0)));
}
}
// Separator as in the big capital letter on the left to indicate sections
holder.separator.setVisibility(
mIsSearchMode
|| (getPositionForSection(getSectionForPosition(position))
!= position)
? View.GONE
: View.VISIBLE);
holder.linphoneFriend.setVisibility(
contact.isInFriendList() ? View.VISIBLE : View.GONE);
ContactAvatar.displayAvatar(contact, holder.avatarLayout);
boolean isOrgVisible = LinphonePreferences.instance().isDisplayContactOrganization();
String org = contact.getOrganization();
if (org != null && !org.isEmpty() && isOrgVisible) {
holder.organization.setText(org);
holder.organization.setVisibility(View.VISIBLE);
} else {
holder.organization.setVisibility(View.GONE);
}
holder.delete.setVisibility(isEditionEnabled() ? View.VISIBLE : View.INVISIBLE);
holder.delete.setChecked(isSelected(position));
// } else {
// // TODO - user's card should have whole and voicemail onClick listeners
}
// Log.d("contactsAdapter", "Position: " + position);
// Log.d("contactsAdapter", "Section for position: " +
// getSectionForPosition(position));
// Log.d(
// "contactsAdapter",
// "The other thingy: " +
// getPositionForSection(getSectionForPosition(position)));
}
// +1 item count to account for user's contact card
@Override
public int getItemCount() {
return mContacts.size() + 1;
}
public Object getItem(int position) {
if (position >= getItemCount()) return null;
return mContacts.get(position);
}
public void setIsSearchMode(boolean set) {
mIsSearchMode = set;
}
[...]
@Override
public int getItemViewType(int position) {
return position == 0 ? USER_CARD_VIEW : CONTACT_CARD_VIEW;
}
本质上,我不会将第一项添加到我的项目列表中,而是将其添加到 recyclerView,并将我的项目列表向上移动一个位置。
虽然入口被隐藏了,但它占据的空间还在
height
或padding
如何在我的 RecyclerView 中将此特定条目的可见性切换为 VISIBLE/GONE
在你的 onBindViewHolder
@Override
public void onBindViewHolder(@NonNull MyViewHolder viewHolder, int i) {
if (isButtonPressed) {
viewHolder.parentViewId.setVisibility(View.GONE);
}else {
viewHolder.parentViewId.setVisibility(View.VISIBLE);
}
}
创建一个public boolean isButtonPressed;
当您按下button
将其adapter.notifyDataSetChanged();
true
并调用adapter.notifyDataSetChanged();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.