當(dāng)我點(diǎn)擊點(diǎn)贊的時(shí)候,贊的圖標(biāo)變成藍(lán)色,然后該點(diǎn)贊數(shù)+1.但是加載更多會(huì)出現(xiàn)數(shù)據(jù)錯(cuò)亂.比如我現(xiàn)在點(diǎn)贊的是第一條,但是加載更多后會(huì)變到第三條去了.
下面是效果圖和代碼:
CommentsController類:
public class CommentsController extends TabController implements BaseQuickAdapter.RequestLoadMoreListener {
@BindView(R.id.rv_comments)
RecyclerView mRecyclerView;
@BindView(R.id.rl_comments_empty_view)
RelativeLayout mEmptyView;
@BindView(R.id.rl_comments_loading)
RelativeLayout mLoading;
private static final String TAG = "CommentsController";
private Context mContext;
private List<CommentsBean> mDatas;
private CommentsAdapter mAdapter;
private String mVid;
private int mListSize;
private int mSize = 1;
public CommentsController(Context context) {
super(context);
this.mContext = context;
}
@Override
protected View initContentView(Context context) {
View view = View.inflate(context, R.layout.controller_comments, null);
ButterKnife.bind(this, view);
return view;
}
@Override
public void initData() {
getInitData();
}
public void getInitData() {
mSize = 1;//切換視頻列表再切換評(píng)論需重置
mListSize = 0;
mLoading.setVisibility(View.VISIBLE);
mVid = (String) BaseApplication.getApplication().getMap().get("vid");
mDatas = new ArrayList<>();
mAdapter = new CommentsAdapter(R.layout.item_comments, mDatas, mContext);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setAdapter(mAdapter);
getCommentsData();
}
/**
* 獲取初始化評(píng)論列表數(shù)據(jù)
*/
private void getCommentsData() {
ApiManager.getService()
.commentsList(NetWorkRequestUtils.createRequestBody(new CommentsObject()))
.compose(RxUtils.<BaseResponse<List<CommentsBean>>>schedulers((Activity) mContext))
.subscribe(new HttpCallback<List<CommentsBean>>((Activity) mContext) {
@Override
public void onSuccess(List<CommentsBean> commentsBeans, String msg) {
mListSize += commentsBeans.size();
Log.d(TAG, "onSuccess: 1506= mListSize " + mListSize + ", commentsBeans=" + commentsBeans);
if (commentsBeans.size() >= 10)
mAdapter.setOnLoadMoreListener(CommentsController.this, mRecyclerView);//大于等于10條數(shù)據(jù)才去加載更多
if (mListSize == 0 && commentsBeans.isEmpty()) {
mEmptyView.setVisibility(View.VISIBLE);
} else if (commentsBeans != null && !ResponseUtils.isDataEnd(commentsBeans)) {
mSize++;
mDatas.addAll(commentsBeans);
mAdapter.notifyDataSetChanged();
mAdapter.loadMoreComplete();
} else {
mDatas.addAll(commentsBeans);
mAdapter.notifyDataSetChanged();
mAdapter.loadMoreEnd();
}
}
@Override
public void onComplete() {
super.onComplete();
mLoading.setVisibility(View.GONE);
if (mListSize > 0)
mEmptyView.setVisibility(View.GONE);
}
});
}
@Override
public void onLoadMoreRequested() {
mRecyclerView.postDelayed(new Runnable() {
@Override
public void run() {
getCommentsData();
}
}, 500);
}
private class CommentsObject {
String vid = mVid;
int p = mSize;
int type = 1;
}
}
CommentsAdapter類:
public class CommentsAdapter extends BaseQuickAdapter<CommentsBean, BaseViewHolder> {
private Context mContext;
private String mVid;
private final String mToken;
private boolean isLike;
public CommentsAdapter(int layoutResId, @Nullable List<CommentsBean> data, Context context) {
super(layoutResId, data);
this.mContext = context;
mVid = (String) BaseApplication.getApplication().getMap().get("vid");
mToken = PreferenceUtils.getString(mContext, "token");
Log.d(TAG, "CommentsAdapter: 1440= vid=" + mVid + ", token=" + mToken);
}
@Override
protected void convert(BaseViewHolder helper, CommentsBean item) {
helper.setText(R.id.tv_comments_username, item.getNickname())
.setText(R.id.tv_comments_date, item.getCtime())
.setText(R.id.tv_comments_content, item.getContent());
Glide.with(mContext).load(item.getHeadimg()).crossFade()
.transform(new GlideRoundTransformUtils(mContext))//將圖片轉(zhuǎn)為圓形
.into((ImageView) helper.getView(R.id.iv_comments_avatar));
int position = helper.getLayoutPosition();
LikeButton likeButton = helper.getView(R.id.comments_like_button);//點(diǎn)贊按鈕
TextView tvLikeCount = helper.getView(R.id.tv_video_comments_like_count);//點(diǎn)贊數(shù)
likeComments(position, likeButton, tvLikeCount);//點(diǎn)贊評(píng)論
// boolean like = item.isLike();
// if(like){
// likeButton.setLiked(true);
// }else{
// likeButton.setLiked(false);
// }
}
private void likeComments(final int position, LikeButton likeButton, final TextView tvLikeCount) {
// likeButton.setEnabled(true);//點(diǎn)贊開關(guān),false為禁止點(diǎn)贊,默認(rèn)true,用于無(wú)網(wǎng)絡(luò)時(shí)禁止點(diǎn)贊
boolean fastDoubleClick = FastClickUtils.isFastDoubleClick();//TODO:避免快速點(diǎn)擊發(fā)起多次請(qǐng)求
// likeButton.setLiked(true);//TODO:加載更多后數(shù)據(jù)錯(cuò)亂
likeButton.setOnLikeListener(new OnLikeListener() {
@Override
public void liked(LikeButton likeButton) {
//TODO:提交點(diǎn)贊結(jié)果到后臺(tái)
Toast.makeText(mContext, "點(diǎn)擊了" + position, Toast.LENGTH_SHORT).show();
int likeCount = Integer.parseInt(tvLikeCount.getText().toString().trim());
int liked = likeCount + 1;
tvLikeCount.setText(liked + "");
}
@Override
public void unLiked(LikeButton likeButton) {
//TODO:提交取消點(diǎn)贊結(jié)果到后臺(tái)
int likeCount = Integer.parseInt(tvLikeCount.getText().toString().trim());
int liked = likeCount - 1;
tvLikeCount.setText(liked + "");
}
});
}
}
這和listview的加載模式是一樣的,設(shè)置了變動(dòng)的一定記得給未設(shè)置的也設(shè)置數(shù)據(jù),比如:你設(shè)置第一條未藍(lán)色,那么其他要設(shè)置黑色,不要因?yàn)槟J(rèn)是黑色就不設(shè)置了。
listview和RecyclerView都是可見(jiàn)刷新的模式,比如這個(gè)文章的說(shuō)法:
https://www.2cto.com/kf/20160...
官方在早期listview的時(shí)代有做過(guò)相關(guān)分享,可能是2014年的時(shí)候,不可考,大致清楚為什么,然后解決的方式就比較簡(jiǎn)單了,總得來(lái)說(shuō)就是對(duì)所有的項(xiàng)目都要記錄數(shù)據(jù)用以對(duì)展示進(jìn)行判斷。如果不記錄就會(huì)出現(xiàn)錯(cuò)亂,因?yàn)槭遣季謴?fù)用的方式。
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過(guò)二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。