ListView ViewHolder 项目子视图可见性改变
ListView ViewHolder item child View visibility changing
我有一个 ListView
和一个自定义 ArrayAdapter
。问题是我无法在某些选择元素中隐藏 (setVisibility(View.GONE)
) TextView
。这是我的代码的最小化可读摘要部分:
public class ListViewAdapter extends ArrayAdapter<ItemModel> {
private int listItemLayout;
static class ViewHolder {
private TextView description;
}
public ListViewAdapter(Context context, int layoutId, List<ItemModel> itemList) {
super(context, layoutId, itemList);
this.listItemLayout = layoutId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ItemModel item = getItem(position);
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(listItemLayout, parent, false);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.description = convertView.findViewById(R.id.text_desription);
viewHolder.description.setText(item.getDescription());
if (TextUtils.isEmpty(item.getDescription())) {
viewHolder.description.setVisibility(View.GONE);
}
return convertView;
}
}
我在这里做错了什么?如果内容 String
为空,如何在 ListView
中的项目中隐藏视图?
添加 if 条件的 else 部分以将可见性从 GONE
更改为 VISIBLE
,如下面的代码所示。
public class ListViewAdapter extends ArrayAdapter {
private int listItemLayout;
static class ViewHolder {
private TextView description;
}
public ListViewAdapter(Context context, int layoutId, List itemList) {
super(context, layoutId, itemList);
this.listItemLayout = layoutId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ItemModel item = getItem(position);
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(listItemLayout, parent, false);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.description = convertView.findViewById(R.id.text_desription);
viewHolder.description.setText(item.getDescription());
if (TextUtils.isEmpty(item.getDescription())) {
viewHolder.description.setVisibility(View.GONE);
}else{
viewHolder.description.setVisibility(View.VISIBLE);
}
return convertView;
}
}
或者
尝试if (item.getDescription()==null || item.getDescription().trim().length() == 0)
而不是 if (TextUtils.isEmpty(item.getDescription()))
检查任何白色 space 不应出现在您的文本中。试试这个-
if (TextUtils.isEmpty(item.getDescription().trim()) ||item.getDescription().trim().length()==0 ) {
viewHolder.description.setVisibility(View.GONE);
}
else{
viewHolder.description.setVisibility(View.VISIBLE);
}
我有一个 ListView
和一个自定义 ArrayAdapter
。问题是我无法在某些选择元素中隐藏 (setVisibility(View.GONE)
) TextView
。这是我的代码的最小化可读摘要部分:
public class ListViewAdapter extends ArrayAdapter<ItemModel> {
private int listItemLayout;
static class ViewHolder {
private TextView description;
}
public ListViewAdapter(Context context, int layoutId, List<ItemModel> itemList) {
super(context, layoutId, itemList);
this.listItemLayout = layoutId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ItemModel item = getItem(position);
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(listItemLayout, parent, false);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.description = convertView.findViewById(R.id.text_desription);
viewHolder.description.setText(item.getDescription());
if (TextUtils.isEmpty(item.getDescription())) {
viewHolder.description.setVisibility(View.GONE);
}
return convertView;
}
}
我在这里做错了什么?如果内容 String
为空,如何在 ListView
中的项目中隐藏视图?
添加 if 条件的 else 部分以将可见性从 GONE
更改为 VISIBLE
,如下面的代码所示。
public class ListViewAdapter extends ArrayAdapter { private int listItemLayout; static class ViewHolder { private TextView description; } public ListViewAdapter(Context context, int layoutId, List itemList) { super(context, layoutId, itemList); this.listItemLayout = layoutId; } @Override public View getView(int position, View convertView, ViewGroup parent) { ItemModel item = getItem(position); ViewHolder viewHolder; if (convertView == null) { viewHolder = new ViewHolder(); convertView = LayoutInflater.from(getContext()).inflate(listItemLayout, parent, false); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.description = convertView.findViewById(R.id.text_desription); viewHolder.description.setText(item.getDescription()); if (TextUtils.isEmpty(item.getDescription())) { viewHolder.description.setVisibility(View.GONE); }else{ viewHolder.description.setVisibility(View.VISIBLE); } return convertView; } }
或者
尝试if (item.getDescription()==null || item.getDescription().trim().length() == 0)
而不是 if (TextUtils.isEmpty(item.getDescription()))
检查任何白色 space 不应出现在您的文本中。试试这个-
if (TextUtils.isEmpty(item.getDescription().trim()) ||item.getDescription().trim().length()==0 ) {
viewHolder.description.setVisibility(View.GONE);
}
else{
viewHolder.description.setVisibility(View.VISIBLE);
}