gpt4 book ai didi

Android setOnCheckedChangeListener 运行两次

转载 作者:行者123 更新时间:2023-11-29 15:42:32 25 4
gpt4 key购买 nike

这是我的代码。什么是原因?

holder.favorite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
addFavorite(famous);
} else {
deleteFavorite(famous);
}
}
});

isChecked=true时,首先运行addFavorite(),然后运行deleteFavorite()

isChecked=false时,首先运行deleteFavorite(),然后运行addFavorite()

不知道为什么...

编辑:当我向下/向上滚动我的 ListView 时,它也会调用这些方法......很奇怪......

    private void deleteFavorite(final FamousTop40Ranking famous) {
DeleteFavoriteData data = new DeleteFavoriteData(famous.getId());
FavoriteDeleteApi.Factory.getInstance().deleteFavorite(data.getData())
.enqueue(new Callback<StatusInfoModel>() {
@Override
public void onResponse(Call<StatusInfoModel> call, Response<StatusInfoModel> response) {
showToast(mActivity, "Famous deleted from your Favorites list.");
famous.setFollowersCountry(famous.getFollowersCountry() - 1);
famous.setFollowersWorld(famous.getFollowersWorld() - 1);
notifyDataSetChanged();
}

@Override
public void onFailure(Call<StatusInfoModel> call, Throwable t) {
Log.d("deleteFavorite", mActivity.getString(R.string.something_went_wrong) + t.getMessage());
}
});
}




private void addFavorite(final FamousTop40Ranking famous) {
FavoriteCountApi.Factory.getInstance().countFavorites()
.enqueue(new Callback<CountFavoriteModel>() {
@Override
public void onResponse(Call<CountFavoriteModel> call, Response<CountFavoriteModel> response) {
if (response.isSuccessful()) {
if (response.body().getCount() < 20) {
FavoriteAddApi.Factory.getInstance().addFavorite(String.valueOf(famous.getId()))
.enqueue(new Callback<StatusInfoModel>() {
@Override
public void onResponse(Call<StatusInfoModel> call, Response<StatusInfoModel> response) {
showToast(mActivity, "Famous added from your Favorites list.");
famous.setFollowersCountry(famous.getFollowersCountry() + 1);
famous.setFollowersWorld(famous.getFollowersWorld() + 1);
notifyDataSetChanged();
}

@Override
public void onFailure(Call<StatusInfoModel> call, Throwable t) {
Log.d("addFavorite", mActivity.getString(R.string.something_went_wrong) + t.getMessage());
}
});
} else {
showToast(mActivity, mActivity.getString(R.string.reached_max_favorites));
}
}
}

@Override
public void onFailure(Call<CountFavoriteModel> call, Throwable t) {
Log.d("countFavorite", mActivity.getString(R.string.something_went_wrong) + t.getMessage());
}
});
}

getView() 方法:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;

if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_view_ranking_famous_single_item, parent, false);
}

final FamousTop40Ranking famous = famousModelList.get(position);

holder = new ViewHolder(convertView);

holder.name.setText(famous.getName());
if (mTab == 0) { // RankingFamousMainFragment.TAB_FAMOUS
holder.followers.setText(String.valueOf(famous.getFollowersWorld()));
} else {
holder.followers.setText(String.valueOf(famous.getFollowersCountry()));
}

if (famous.getIsFavorite().get(0).getFavorite().equals("1")) {
holder.favorite.setChecked(true);
} else {
holder.favorite.setChecked(false);
}

Glide
.with(mActivity)
.load(famous.getPhoto())
.fallback(R.drawable.bg_gradient)
.error(R.drawable.bg_gradient)
.centerCrop()
.crossFade()
.into(holder.photo);

holder.favorite.setOnCheckedChangeListener(null);
holder.favorite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
addFavorite(famous);
} else {
deleteFavorite(famous);
}
}
});

convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showToast(mActivity, mActivity.getString(R.string.famous_clicked, position));
}
});

return convertView;
}

最佳答案

在获取 View 中。将 setonCHecked(null) 移到 if/else 之前。

之所以如此,是因为 getView 可能会被调用很多次,在这种情况下,我认为它被调用是因为当您更改状态(选中/取消选中)时,它会重绘按钮。在您的 getView 中,您正在调用 setChecked 来触发监听器。在调用 holder.favorite.setChecked(true/false) 之前将监听器设置为 null 不会双重触发它。

关于上/下问题- 一样的,当 View 是屏幕时,它被删除。当它再次出现时,它会调用 getView,由于 holder.favorite.setChecked(true) 激活了 onCheckChangedLIstener,它会触发所有内容

举个例子:

holder.favorite.setOnCheckedChangeListener(null);
holder.favorite.setChecked(true);
holder.favourite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// handle your logic
}
});

关于Android setOnCheckedChangeListener 运行两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38220795/

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