gpt4 book ai didi

Android:只有我的 ListView 的最后一行对 OnClickListener 有反应

转载 作者:行者123 更新时间:2023-11-29 00:05:18 26 4
gpt4 key购买 nike

我有一个带有 View Holder 的 ListView。当我按下 ListView 的其中一行中的按钮时,它应该会在该位置展开该行并在我再次按下时折叠。我在布局上使用显示/隐藏来执行此操作。但是,点击并没有展开正确的行。

示例:

I was clicked
I am a row that was not clicked
I am a row that was not clicked
I am a row that was not clicked // yet my layout was shown and hidden

每当我滚动时都会发生这种情况,只有 ListView 中可见的最后一行会对我按下的任何行使用react。

代码

public class PetAdapter extends BaseAdapter {
...
...

@Override
public int getCount() {
return mPetList.size();
}

@Override
public int getViewTypeCount() {
return 2;
}

@Override
public int getItemViewType(int position) {
if (mPetList.get(position).getPetType().equals(ConstantValues.CAT)) {
return 0;
} else {
return 1;
}
}

@Override
public Object getItem(int position) {
return mPetList.get(position);
}

@Override
public long getItemId(int position) {
return Integer.parseInt(mPetList.get(position).getId());
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
Pet pet;
View view = convertView;

switch (getItemViewType(position)) {
case 0:
pet = mPetList.get(position);
mRowType = ConstantValues.CAT;
view = createCatRow(pet, convertView, parent);
break;
case 1:
pet = mPetList.get(position);
mRowType = ConstantValues.DOG;
view = createDogRow(pet, convertView, parent);
break;
}

return view;
}

膨胀行

    public View createCatRow(Pet pet, View convertView, ViewGroup parent) {
if (convertView != null) {
mCatHolder = (CatViewHolder) convertView.getTag();
} else {
convertView = LayoutInflater.from(mContext).inflate(R.layout.cat_row, parent, false);
mCatHolder= new CatViewHolder(convertView);
convertView.setTag(mCatHolder);
}

setCatText(pet);
setCatListeners(pet);

return convertView;
}

设置文本

    public void setCatText(Pet pet) {
mCatHolder.catName.setText(pet.getName());
}

设置监听器

public void setCatListeners(Pet pet) {      
mCatHolder.catName.setOnClickListener(getCatNameListener(pet));
}

public View.OnClickListener getCatNameListener(final Pet pet) {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
expandCard(pet);
}
};
}

设置可见性

public void expandCard(Pet pet) {
if (mCatHolder.expanded.getVisibility() == View.GONE) {
mCatHolder.collapsed.setVisibility(View.GONE);
mCatHolder.expanded.setVisibility(View.VISIBLE);
} else {
mCatHolder.collapsed.setVisibility(View.VISIBLE);
mCatHolder.expanded.setVisibility(View.GONE);
}
}

View 持有者

public static class CatViewHolder {            
@Bind(R.id.expanded) RelativeLayout expanded;
@Bind(R.id.collapsed ) RelativeLayout collapsed;
@Bind(R.id.cat_name) TextView catName;

public CatViewHolder(View view) {
ButterKnife.bind(this, view);
}
}

请帮助我理解为什么会发生这种行为。为什么最后一行对第一行的 OnClick 事件使用react。谢谢

最佳答案

RecyclerView中, View 被回收。因此,第一行的 View 被重新用于后面的行,并且它仍然是 OnClickListener。您必须在每个 onBindViewHolder() 中设置监听器。

关于Android:只有我的 ListView 的最后一行对 OnClickListener 有反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34282067/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com