TextView 在列表中滚动时重复
TextView repeating on scrolling in List
实际上,当用户单击图像视图时,我一直在尝试在文本视图上显示用户名。单击图像视图时显示用户名,但当我向下滚动时,相同的名称出现在列表中的不同位置。
例如:
当我点击用户个人资料时,它会显示其名称:
但是当我向下滚动时,该名称在另一个用户身上重复出现。
这是我的代码:
private List<Checkins> allPersons;
Checkins checkin;
public CustomAdapter(FragmentActivity activity, List<Checkins> classModel)
{
context = activity;
allPersons = classModel;
mActivity = activity;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
if (convertView == null)
{
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.programlist, null);
holder = new mHolder();
holder.txt_name = (TextView) convertView.findViewById(R.id.fragment_browse_grid_footer_product_name);
holder.txt_userName = (TextView) convertView.findViewById(R.id.txt_userName);
holder.imgV_profile = (ImageView) convertView.findViewById(R.id.imgVProfilePic);
holder.imgV_chceckins = (ImageView) convertView.findViewById(R.id.imageV_CheckinPlace);
holder.imgV_placePic = (ImageView) convertView.findViewById(R.id.checkinPlace_image);
convertView.setTag(holder);
} else{
holder = (mHolder) convertView.getTag();
}
checkin = allPersons.get(position);
holder.txt_name.setText(checkin.getPlaceName());
holder.imgV_profile.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Checkins checkin2 = allPersons.get(position);
holder.txt_userName.setVisibility(View.VISIBLE);
holder.txt_userName.setText(checkin2.getUserName());
}
});
return convertView;
}
class mHolder {
TextView txt_name;
TextView txt_userName;
ImageView imgV_profile;
ImageView imgV_chceckins;
ImageView imgV_placePic;
int pos;
}
更新:如何只显示一个文本视图。
如图所示,如果我点击另一个用户,我不想一次显示两个用户名(两个文本视图),一次只显示一个用户名。
删除此 checkin = allPersons.get(position);
像下面这样使用你的所有人名单
holder.txt_userName.setVisibility(View.VISIBLE);
holder.txt_userName.setText(allPersons.get(position).getUserName());
只要您的适配器获得视图,您就应该隐藏它。这是由 ListView Recycling Mechanism.
引起的
因此,将以下行添加到您的 getView
方法中。
holder.txt_userName.setVisibility(View.GONE);
注意: 为了获得更好的性能,您不需要每次都注册 onClick
侦听器。注册一次就够了。因此,将 onClick
注册码移动到 if (convertView == null)
块。
更新 #1
隐藏和重新显示 TextView
的简单解决方案。将以下 if
块添加到您的 getView
方法中。
Checkins thisPerson = allPersons.get(position);
if(thisPerson.isUsernameDisplayed()){
holder.txt_userName.setVisibility(View.VISIBLE);
holder.txt_userName.setText(thisPerson.getUserName());
} else {
holder.txt_userName.setVisibility(View.GONE);
}
你可能会问我isUsernameDisplayed
是什么。答案是,您应该跟踪每个项目以查看其用户名是否可见。为此,您应该将它们的状态保存在某个地方。您可能希望将此信息保存在 allPersons
本身中。
使用列表存储显示昵称的位置!
并根据列表设置TextView'Visibility
。
因为重用 ViewHolder
!
private List<Checkins> allPersons;
private SparseArray<Integer> list = new SparseArray<>();
Checkins checkin;
public CustomAdapter(FragmentActivity activity, List<Checkins> classModel)
{
context = activity;
allPersons = classModel;
mActivity = activity;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
if (convertView == null)
{
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.programlist, null);
holder = new mHolder();
holder.txt_name = (TextView) convertView.findViewById(R.id.fragment_browse_grid_footer_product_name);
holder.txt_userName = (TextView) convertView.findViewById(R.id.txt_userName);
holder.imgV_profile = (ImageView) convertView.findViewById(R.id.imgVProfilePic);
holder.imgV_chceckins = (ImageView) convertView.findViewById(R.id.imageV_CheckinPlace);
holder.imgV_placePic = (ImageView) convertView.findViewById(R.id.checkinPlace_image);
convertView.setTag(holder);
} else{
holder = (mHolder) convertView.getTag();
}
if(list.get(position)!=null &&list.get(position) == true){
holder.txt_userName.setVisibility(View.VISIBLE);
}else{
holder.txt_userName.setVisibility(View.GONE);
}
checkin = allPersons.get(position);
holder.txt_name.setText(checkin.getPlaceName());
holder.imgV_profile.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
list.append(position,true);
Checkins checkin2 = allPersons.get(position);
holder.txt_userName.setVisibility(View.VISIBLE);
holder.txt_userName.setText(checkin2.getUserName());
}
});
return convertView;
}
class mHolder {
TextView txt_name;
TextView txt_userName;
ImageView imgV_profile;
ImageView imgV_chceckins;
ImageView imgV_placePic;
int pos;
}
如果你只需要一个,你甚至不需要list.Just使用int
来存储它。
实际上,当用户单击图像视图时,我一直在尝试在文本视图上显示用户名。单击图像视图时显示用户名,但当我向下滚动时,相同的名称出现在列表中的不同位置。
例如:
当我点击用户个人资料时,它会显示其名称:
但是当我向下滚动时,该名称在另一个用户身上重复出现。
这是我的代码:
private List<Checkins> allPersons;
Checkins checkin;
public CustomAdapter(FragmentActivity activity, List<Checkins> classModel)
{
context = activity;
allPersons = classModel;
mActivity = activity;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
if (convertView == null)
{
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.programlist, null);
holder = new mHolder();
holder.txt_name = (TextView) convertView.findViewById(R.id.fragment_browse_grid_footer_product_name);
holder.txt_userName = (TextView) convertView.findViewById(R.id.txt_userName);
holder.imgV_profile = (ImageView) convertView.findViewById(R.id.imgVProfilePic);
holder.imgV_chceckins = (ImageView) convertView.findViewById(R.id.imageV_CheckinPlace);
holder.imgV_placePic = (ImageView) convertView.findViewById(R.id.checkinPlace_image);
convertView.setTag(holder);
} else{
holder = (mHolder) convertView.getTag();
}
checkin = allPersons.get(position);
holder.txt_name.setText(checkin.getPlaceName());
holder.imgV_profile.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Checkins checkin2 = allPersons.get(position);
holder.txt_userName.setVisibility(View.VISIBLE);
holder.txt_userName.setText(checkin2.getUserName());
}
});
return convertView;
}
class mHolder {
TextView txt_name;
TextView txt_userName;
ImageView imgV_profile;
ImageView imgV_chceckins;
ImageView imgV_placePic;
int pos;
}
更新:如何只显示一个文本视图。 如图所示,如果我点击另一个用户,我不想一次显示两个用户名(两个文本视图),一次只显示一个用户名。
删除此 checkin = allPersons.get(position);
像下面这样使用你的所有人名单
holder.txt_userName.setVisibility(View.VISIBLE);
holder.txt_userName.setText(allPersons.get(position).getUserName());
只要您的适配器获得视图,您就应该隐藏它。这是由 ListView Recycling Mechanism.
引起的因此,将以下行添加到您的 getView
方法中。
holder.txt_userName.setVisibility(View.GONE);
注意: 为了获得更好的性能,您不需要每次都注册 onClick
侦听器。注册一次就够了。因此,将 onClick
注册码移动到 if (convertView == null)
块。
更新 #1
隐藏和重新显示 TextView
的简单解决方案。将以下 if
块添加到您的 getView
方法中。
Checkins thisPerson = allPersons.get(position);
if(thisPerson.isUsernameDisplayed()){
holder.txt_userName.setVisibility(View.VISIBLE);
holder.txt_userName.setText(thisPerson.getUserName());
} else {
holder.txt_userName.setVisibility(View.GONE);
}
你可能会问我isUsernameDisplayed
是什么。答案是,您应该跟踪每个项目以查看其用户名是否可见。为此,您应该将它们的状态保存在某个地方。您可能希望将此信息保存在 allPersons
本身中。
使用列表存储显示昵称的位置!
并根据列表设置TextView'Visibility
。
因为重用 ViewHolder
!
private List<Checkins> allPersons;
private SparseArray<Integer> list = new SparseArray<>();
Checkins checkin;
public CustomAdapter(FragmentActivity activity, List<Checkins> classModel)
{
context = activity;
allPersons = classModel;
mActivity = activity;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
if (convertView == null)
{
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.programlist, null);
holder = new mHolder();
holder.txt_name = (TextView) convertView.findViewById(R.id.fragment_browse_grid_footer_product_name);
holder.txt_userName = (TextView) convertView.findViewById(R.id.txt_userName);
holder.imgV_profile = (ImageView) convertView.findViewById(R.id.imgVProfilePic);
holder.imgV_chceckins = (ImageView) convertView.findViewById(R.id.imageV_CheckinPlace);
holder.imgV_placePic = (ImageView) convertView.findViewById(R.id.checkinPlace_image);
convertView.setTag(holder);
} else{
holder = (mHolder) convertView.getTag();
}
if(list.get(position)!=null &&list.get(position) == true){
holder.txt_userName.setVisibility(View.VISIBLE);
}else{
holder.txt_userName.setVisibility(View.GONE);
}
checkin = allPersons.get(position);
holder.txt_name.setText(checkin.getPlaceName());
holder.imgV_profile.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
list.append(position,true);
Checkins checkin2 = allPersons.get(position);
holder.txt_userName.setVisibility(View.VISIBLE);
holder.txt_userName.setText(checkin2.getUserName());
}
});
return convertView;
}
class mHolder {
TextView txt_name;
TextView txt_userName;
ImageView imgV_profile;
ImageView imgV_chceckins;
ImageView imgV_placePic;
int pos;
}
如果你只需要一个,你甚至不需要list.Just使用int
来存储它。