实际上,当用户点击图像视图时,我一直在尝试在文本视图上显示用户名。单击图像视图时显示用户名,但当我向下滚动时,同一名称出现在列表中的不同位置。
例如:
当我点击用户个人资料时,它会显示其名称:
这是我的代码:
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;
}
更新:如何只显示一个文字视图。 如图所示,如果我点击其他用户,一次只能输入一个用户名,我不想一次显示两个用户名(两个文本视图)。
答案 0 :(得分:2)
只要适配器获得视图,就应该隐藏它。这是由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
本身。
答案 1 :(得分:0)
删除此checkin = allPersons.get(position);
使用您的allPersons列表,如下所示
holder.txt_userName.setVisibility(View.VISIBLE);
holder.txt_userName.setText(allPersons.get(position).getUserName());
答案 2 :(得分:0)
使用列表存储显示昵称的位置!
根据列表设置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;
}
如果你只需要一个,你甚至不需要列表。只需使用int
来存储它。