我在 ListView 的 getView() 中将 textview 的 VISIBILITY 设置为 VISIBLE - 它对下一行也保持可见。如何解决这个问题?
I set VISIBILITY of textview to VISIBLE in getView() of ListView - it remains visible for the next rows as well. How to solve this?
在我的 ListView
的每一行中,我希望 TextView
(最初在 XML 中给出 android:visibility="gone"
)只有在满足条件时才可见.
所以我做了类似下面的伪代码。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
view = layoutInflater.inflate(R.layout.row_item, parent, false);
} else {
view = convertView;
}
...
if (ape.getAlpha() != null && ape.getAlpha().equals("RON") ) {
TextView textView = (TextView) view.findViewById(R.id.Item_textView5);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setBackgroundColor(Color.CYAN);
}
});
textView.setVisibility(View.VISIBLE);
}
...
}
现在,由于每一行都被回收,再次调用getView()
,一旦连续满足(ape.getAlpha() != null && ape.getAlpha().equals("RON"))
条件,因此textview
' s visibility
设置为 View.VISIBLE
,它对下一行也保持可见。
问题是,在将 textview
的可见性设置为 View.VISIBLE
后,我如何才能重置该行的布局将被回收的下一行 ,以便 gone
的重置可见性不会反映在当前行中,但对于将使用同一行布局的下一行不可见?
您可以将 textView.setVisibility(View.GONE);
放在函数的顶部。这将强制所有 textView 始终消失,直到它们满足条件。
只需按照评论中的建议添加一个 else 部分,并将您的 textView
初始化移到 if 块之外,如-
TextView textView = (TextView) view.findViewById(R.id.Item_textView5);
if (ape.getAlpha() != null && ape.getAlpha().equals("RON") ) {
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setBackgroundColor(Color.CYAN);
}
});
textView.setVisibility(View.VISIBLE);
}else{
textView.setVisibility(View.GONE);
}
在我的 ListView
的每一行中,我希望 TextView
(最初在 XML 中给出 android:visibility="gone"
)只有在满足条件时才可见.
所以我做了类似下面的伪代码。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
view = layoutInflater.inflate(R.layout.row_item, parent, false);
} else {
view = convertView;
}
...
if (ape.getAlpha() != null && ape.getAlpha().equals("RON") ) {
TextView textView = (TextView) view.findViewById(R.id.Item_textView5);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setBackgroundColor(Color.CYAN);
}
});
textView.setVisibility(View.VISIBLE);
}
...
}
现在,由于每一行都被回收,再次调用getView()
,一旦连续满足(ape.getAlpha() != null && ape.getAlpha().equals("RON"))
条件,因此textview
' s visibility
设置为 View.VISIBLE
,它对下一行也保持可见。
问题是,在将 textview
的可见性设置为 View.VISIBLE
后,我如何才能重置该行的布局将被回收的下一行 ,以便 gone
的重置可见性不会反映在当前行中,但对于将使用同一行布局的下一行不可见?
您可以将 textView.setVisibility(View.GONE);
放在函数的顶部。这将强制所有 textView 始终消失,直到它们满足条件。
只需按照评论中的建议添加一个 else 部分,并将您的 textView
初始化移到 if 块之外,如-
TextView textView = (TextView) view.findViewById(R.id.Item_textView5);
if (ape.getAlpha() != null && ape.getAlpha().equals("RON") ) {
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setBackgroundColor(Color.CYAN);
}
});
textView.setVisibility(View.VISIBLE);
}else{
textView.setVisibility(View.GONE);
}