首先在适配器中进行判断
//判断是否收藏
if (resultBean.getWhetherCollection()==1){
viewHolder.mation_whetherCollection.setBackgroundResource((R.mipmap.common_icon_collect_s));
}else {
viewHolder.mation_whetherCollection.setBackgroundResource((R.mipmap.common_icon_collect_n));
}
##resultBean.getWhetherCollection()为请求网络得到的初始数据,返回值有两个,1和2,1代表初始为收藏,2代表初始为未收藏。
收藏的点击事件
//收藏/取消收藏的点击事件
viewHolder.mation_whetherCollection.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnClickedListener != null) {
mOnClickedListener.onClicked(i,resultBean.getId(), resultBean.getWhetherCollection());
}
}
});
## 点击事件进行接口回调。mOnClickedListener为自定义的接口。
## i ——————>点击的item的下标
## resultBean.getId()——————> item的id,此处通过得到的数据里获得
resultBean.getWhetherCollection()—————>和上面的一样
调取适配器的接口
//收藏
informationAdapter.setOnClickedListener(new InformationAdapter.onClickedListener() {
@Override
public void onClicked(int i,int position, int WhetherCollection) {
boolean flate=true;
if (WhetherCollection == 1) {
//初始为1,此时点击则取消收藏,调取取消收藏的接口
doDelete(getActivity().getApplication(), Apis.information_cancel_collection + "infoId=" + position, InformationCollectionBean.class);
flate=false;
} else {
//初始为2,此时点击则收藏成功,调取成功收藏的接口
HashMap<String, String> map = new HashMap<>();
map.put("infoId", position + "");
doPostUser(getActivity().getApplication(), Apis.information_add_collection, map, InformationCollectionBean.class);
flate=true;
}
informationAdapter.refresh(i,flate);
}
});
##informationAdapter————>适配器的名称
## informationAdapter.refresh(i,flate)————>调用适配器里自定义的方法refresh();
适配器里自定义的refresh(),根据点击事件的结果,对显示结果进行刷新
public void refresh(int i,boolean flate){
InformationBean.ResultBean resultBean = mMationjihe.get(i);
if (!flate){
//收藏数量改变
resultBean.setCollection(resultBean.getCollection()-1);
//改变收藏的初始值
resultBean.setWhetherCollection(2);
}else {
resultBean.setCollection(resultBean.getCollection()+1);
resultBean.setWhetherCollection(1);
}
notifyDataSetChanged();
}
结束