Android Listview點(diǎn)贊問題關(guān)于圖片重復(fù)問題
《最近做一個(gè)小功能遇到這么一個(gè)問題,listview 與 baseadapter結(jié)合使用,關(guān)于點(diǎn)贊的的時(shí)候 圖片重復(fù)問題,比如:我在第1個(gè)item 點(diǎn)贊然后 心型換成了紅色,但是以后每隔幾個(gè)item就會(huì)出現(xiàn)一個(gè)紅色的心,響應(yīng)事件是對(duì)的,不知道哪出的問題,請(qǐng)大神解答”》
上面是一小哥在論壇中發(fā)的帖子遇到的問題,跟我遇到的問題一樣,下面有很多熱心的評(píng)論哥們給出了思路,我一想,原來這么簡(jiǎn)單啊。
先給出實(shí)現(xiàn)代碼,最后再來講思路好了。
這篇博客我重新編輯了一次,加上了動(dòng)畫和收藏的效果,評(píng)論的哥們說收藏和點(diǎn)贊不能同時(shí)進(jìn)行,圖片會(huì)錯(cuò)亂,我給了他思路,他還是沒實(shí)現(xiàn),哎,我還是再來一遍,修改修改吧,博主真是關(guān)心大家啊
效果圖
MainActivity.java
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //模擬的數(shù)據(jù)內(nèi)容集合 List<ContentBean> data = new ArrayList<ContentBean>(); for (int i = 0; i < 15; i++) { ContentBean bean = new ContentBean(); // 默認(rèn)都給他們賦值當(dāng)前都沒有點(diǎn)贊 bean.setZanFocus(false); bean.setZanNum(i); // 默認(rèn)都給他們賦值當(dāng)前都沒有收藏 bean.setShoucanFocus(false); bean.setShoucanNum(i); data.add(bean); } ListView listview = (ListView) findViewById(R.id.listview); listview.setAdapter(new MyAdapter(this,data)); } main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.zan.MainActivity" > <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </RelativeLayout> ContentBean.java public class ContentBean { private boolean zanFocus, shoucanFocus; private int zanNum, shoucanNum; public boolean isShoucanFocus() { return shoucanFocus; } public void setShoucanFocus(boolean shoucanFocus) { this.shoucanFocus = shoucanFocus; } public int getShoucanNum() { return shoucanNum; } public void setShoucanNum(int shoucanNum) { this.shoucanNum = shoucanNum; } public boolean isZanFocus() { return zanFocus; } public void setZanFocus(boolean zanFocus) { this.zanFocus = zanFocus; } public int getZanNum() { return zanNum; } public void setZanNum(int zanNum) { this.zanNum = zanNum; } } MyAdapter.java public class MyAdapter extends BaseAdapter { List<ContentBean> data = new ArrayList<ContentBean>(); Context context; public MyAdapter(Context context, List<ContentBean> data) { this.context = context; this.data = data; } @Override public int getCount() { return data.size();// 返回20條數(shù)據(jù) } @Override public Object getItem(int arg0) { return data.get(arg0); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { final ViewHolder holder; final ContentBean bean = data.get(position); if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.item, parent, false); holder = new ViewHolder(); holder.zan_img = (ImageView) convertView.findViewById(R.id.zan_img); holder.zan_num = (TextView) convertView.findViewById(R.id.zan_num); holder.shoucan_img = (ImageView) convertView.findViewById(R.id.shoucan_img); holder.shoucan_num = (TextView) convertView.findViewById(R.id.shoucan_num); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } // 取出bean中當(dāng)記錄狀態(tài)是否為true,是的話則給img設(shè)置focus點(diǎn)贊圖片 if (bean.isZanFocus()) { holder.zan_img.setImageResource(R.drawable.zan_focus); } else { holder.zan_img.setImageResource(R.drawable.zan_release); } // 取出bean中當(dāng)記錄狀態(tài)是否為true,是的話則給img設(shè)置release收藏圖片 if (bean.isShoucanFocus()) { holder.shoucan_img.setImageResource(R.drawable.shoucang_focus); } else { holder.shoucan_img.setImageResource(R.drawable.shoucang_release); } // 設(shè)置贊的數(shù)量 holder.zan_num.setText(bean.getZanNum() + ""); //設(shè)置收藏的數(shù)量 holder.shoucan_num.setText(bean.getShoucanNum()+""); holder.zan_img.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 獲取上次是否已經(jīng)被點(diǎn)擊 boolean flag = bean.isZanFocus(); // 判斷當(dāng)前flag是點(diǎn)贊還是取消贊,是的話就給bean值減1,否則就加1 if (flag) { bean.setZanNum(bean.getZanNum() - 1); } else { bean.setZanNum(bean.getZanNum() + 1); } // 反向存儲(chǔ)記錄,實(shí)現(xiàn)取消點(diǎn)贊功能 bean.setZanFocus(!flag); AnimationTools.scale(holder.zan_img); } }); holder.shoucan_img.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 獲取上次是否已經(jīng)被點(diǎn)擊 boolean flag = bean.isShoucanFocus(); // 判斷當(dāng)前flag是收藏還是取收藏,是的話就給bean值減1,否則就加1 if (flag) { bean.setShoucanNum(bean.getShoucanNum() - 1); } else { bean.setShoucanNum(bean.getShoucanNum() + 1); } // 反向存儲(chǔ)記錄,實(shí)現(xiàn)取消收藏功能 bean.setShoucanFocus(!flag); //動(dòng)畫 AnimationTools.scale(holder.shoucan_img); } }); return convertView; } private class ViewHolder { private ImageView zan_img,shoucan_img; private TextView zan_num,shoucan_num; } } item.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center" android:orientation="horizontal" > <ImageView android:id="@+id/zan_img" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/zan_num" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> AnimationTools.java public class AnimationTools { public static void scale(View v) { ScaleAnimation anim = new ScaleAnimation(1.0f, 1.5f, 1.0f, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); anim.setDuration(300); v.startAnimation(anim); } }
代碼其實(shí)很簡(jiǎn)單,稍作理解還是很容易弄懂的,我們?cè)趌istview更新item中的數(shù)據(jù)的時(shí)候,一定要明白一個(gè)道理,不要在item的view中直接更改數(shù)據(jù)(剛開始做的時(shí)候我就是直接holder.zan_img.setImageResource(資源圖片),發(fā)現(xiàn)往下滑動(dòng)的時(shí)候,上一次的觸摸記錄被下面的item給復(fù)用了,造成了數(shù)據(jù)的混亂),不然會(huì)造成數(shù)據(jù)的混亂,要是明白的listview的工作原理的話,可能會(huì)更清楚的明白,listview每次加載的數(shù)據(jù)是當(dāng)前屏幕的一屏數(shù)據(jù)(其實(shí)我了解的不多,但是在打印log的時(shí)候,發(fā)現(xiàn)log出來初始化的數(shù)據(jù)就是一屏的數(shù)據(jù)),當(dāng)你如果直接去改變view的樣式的話,你觸摸的當(dāng)前item會(huì)被下面還未出現(xiàn)的item給復(fù)用掉,我是這樣理解的=-=。
- 簡(jiǎn)單實(shí)用的Android UI微博動(dòng)態(tài)點(diǎn)贊效果
- Android 仿微信朋友圈點(diǎn)贊和評(píng)論彈出框功能
- Android項(xiàng)目開發(fā) 教你實(shí)現(xiàn)Periscope點(diǎn)贊效果
- Android中Listview點(diǎn)贊功能的實(shí)現(xiàn)
- Android實(shí)現(xiàn)點(diǎn)贊動(dòng)畫(27)
- Android中使用PopupWindow 仿微信點(diǎn)贊和評(píng)論彈出
- Android PraiseTextView實(shí)現(xiàn)朋友圈點(diǎn)贊功能
- Android實(shí)現(xiàn)朋友圈點(diǎn)贊列表
- Android高級(jí)UI特效仿直播點(diǎn)贊動(dòng)畫效果
- Android控件FlowLikeView實(shí)現(xiàn)點(diǎn)贊動(dòng)畫
相關(guān)文章
Flutter自動(dòng)路由插件auto_route使用詳解
這篇文章主要為大家介紹了Flutter自動(dòng)路由插件auto_route的基本使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Android DragImageView實(shí)現(xiàn)下拉拖動(dòng)圖片放大效果
這篇文章主要為大家詳細(xì)介紹了Android DragImageView實(shí)現(xiàn)下拉拖動(dòng)圖片放大效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android開發(fā)中ImageLoder加載網(wǎng)絡(luò)圖片時(shí)將圖片設(shè)置為ImageView背景的方法
這篇文章主要介紹了Android開發(fā)中ImageLoder加載網(wǎng)絡(luò)圖片時(shí)將圖片設(shè)置為ImageView背景的方法,涉及Android ImageView圖片加載及背景設(shè)置相關(guān)操作技巧,需要的朋友可以參考下2018-01-01Kotlin淺析延遲初始化與密封類的實(shí)現(xiàn)方法
Kotlin語(yǔ)言的許多特性,包括變量不可變,變量不可為空,等等。這些特性都是為了盡可能地保證程序安全而設(shè)計(jì)的,但是有些時(shí)候這些特性也會(huì)在編碼時(shí)給我們帶來不少的麻煩,下面我們來了解延遲初始化和密封類的特點(diǎn)2022-08-08Android 中 Tweened animation的實(shí)例詳解
這篇文章主要介紹了Android 中 Tweened animation的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09Android Studio打包APK文件具體實(shí)現(xiàn)步驟解析
這篇文章主要介紹了Android Studio打包APK文件具體實(shí)現(xiàn)步驟解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11Android模擬器安裝APP出現(xiàn)INSTALL_FAILED_NO_MATCHING_ABIS錯(cuò)誤解決方案
這篇文章主要介紹了 Android模擬器安裝APP出現(xiàn)INSTALL_FAILED_NO_MATCHING_ABIS錯(cuò)誤解決方案的相關(guān)資料,需要的朋友可以參考下2016-12-12