話不多說直接 在代碼里 注釋:
注意:1,在BaseAdapter中是代碼自動生成的無須手寫。
2,在這里This換成當(dāng)前上下文對象parent.getContext(),
@Override //看這里的標(biāo)志。
public View getView(final int position, View convertView, final ViewGroup parent) {
if (convertView == null) {//這里很簡單,就是判斷View是否為空
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pic, null);//以及View中所使用的布局文件。
holder = new viewHolder(convertView);
convertView.setTag(holder);
}else {
holder = (viewHolder) convertView.getTag();
}
Glide.with(parent.getContext()) //這里用到了網(wǎng)絡(luò)圖片緩存
.load(data.get(position).getPicUrl())
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher_round)
.into(holder.img_pic);
holder.tv_pic.setText(data.get(position).getTitle());//這里是對應(yīng)顯示的東西。
convertView.setOnLongClickListener(new View.OnLongClickListener() { //這里就是長按操作
@Override
public boolean onLongClick(View v) {
new AlertDialog.Builder(parent.getContext()) //這里我用到了Dialog,方便我們確定收藏與取消
.setTitle("收藏")
.setMessage("是否收藏?")
.setPositiveButton("收藏", new DialogInterface.OnClickListener() { //確認收藏操作
@Override
public void onClick(DialogInterface dialog, int which) {
Account account = BmobUser.getCurrentUser(BaseApplication.getInstance(),Account.class);//我們收藏肯定是必須登陸后才可以收藏內(nèi)容,否則不能收藏,跳轉(zhuǎn)頁面,直接送你去登陸或者注冊。
if(account != null) {//這里加了一個判斷
CollectionBean collectionBean = new CollectionBean();
collectionBean.setPicUrl(data.get(position).getPicUrl());
collectionBean.setuId(account.getObjectId());
collectionBean.setUrl(data.get(position).getUrl());
collectionBean.setTitle(data.get(position).getTitle());
collectionBean.setType(Constant.COLLECTION_TYPE_PIC);//這里根據(jù)自己的 Bean來設(shè)置
collectionBean.save(parent.getContext(), new SaveListener() {
@Override
public void onSuccess() {
Toast.makeText(parent.getContext(), "收藏成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(int i, String s) {
Toast.makeText(parent.getContext(), "收藏失敗", Toast.LENGTH_SHORT).show();
}
});
}
}
})
.setNegativeButton("取消",null)
.create()
.show();
return false;
}
});
return convertView;
}
代碼只供學(xué)習(xí),謝謝,有什么不足與錯誤,請大牛指點。