I have the following getView in the BaseAdapter.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout itemLo;
if (convertView != null) {
itemLo = (LinearLayout) convertView;
} else {
itemLo = (LinearLayout) LayoutInflater.from(mContext.getApplicationContext()).
inflate(R.layout.item, parent, false);
}
View v1 = itemLo.findViewById(R.id.view1);
View v2 = itemLo.findViewById(R.id.view2);
if (position == 0) {
v1.setVisibility(View.GONE);
v2.setText("Start");
} else {
v1.setText("" + position);
v2.setText("" + position);
}
return convertView;
}
When it is the first row (row 0), I hide v1 in row 0. My worry is that, after row 0 is scrolled out of the window, the converView for row 0 will be reused by other rows. The issue is that, v1 in row 0 has been set to View.GONE in row 0. If other rows reuse the converView for row 0, do I have to set View.Visible to v1? My test shows that I don't have to reset visibility for v1 in the convertView. So I am confused开发者_高级运维. Doesn't converView conserves the visibility property for each view in it, when the convertView is reused?
Thanks.
You must use a condition based on your data instead of use current position.
i.e.
MyClass c = (MyClass) getItem(position);
....
if (c.isVisible()) {
v1.setText("" + position);
v2.setText("" + position);
} else {
v1.setVisibility(View.GONE);
v2.setText("Start");
}
...
(c.isVisible) or any other condition that you like.
converView is for reusing stuff, that mean you must set/reset every property that may be changed before in previous call to getView method.
do I have to set View.Visible to v1?
Yes
精彩评论