国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android Listview點贊問題關(guān)于圖片重復問題

2019-12-12 04:39:32
字體:
供稿:網(wǎng)友

《最近做一個小功能遇到這么一個問題,listview 與 baseadapter結(jié)合使用,關(guān)于點贊的的時候 圖片重復問題,比如:我在第1個item 點贊然后 心型換成了紅色,但是以后每隔幾個item就會出現(xiàn)一個紅色的心,響應事件是對的,不知道哪出的問題,請大神解答”》

上面是一小哥在論壇中發(fā)的帖子遇到的問題,跟我遇到的問題一樣,下面有很多熱心的評論哥們給出了思路,我一想,原來這么簡單啊。

先給出實現(xiàn)代碼,最后再來講思路好了。

這篇博客我重新編輯了一次,加上了動畫和收藏的效果,評論的哥們說收藏和點贊不能同時進行,圖片會錯亂,我給了他思路,他還是沒實現(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();      // 默認都給他們賦值當前都沒有點贊      bean.setZanFocus(false);      bean.setZanNum(i);      // 默認都給他們賦值當前都沒有收藏      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.javapublic 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.javapublic 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中當記錄狀態(tài)是否為true,是的話則給img設(shè)置focus點贊圖片    if (bean.isZanFocus()) {      holder.zan_img.setImageResource(R.drawable.zan_focus);    } else {      holder.zan_img.setImageResource(R.drawable.zan_release);    }    // 取出bean中當記錄狀態(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)被點擊        boolean flag = bean.isZanFocus();        // 判斷當前flag是點贊還是取消贊,是的話就給bean值減1,否則就加1        if (flag) {          bean.setZanNum(bean.getZanNum() - 1);        } else {          bean.setZanNum(bean.getZanNum() + 1);        }        // 反向存儲記錄,實現(xiàn)取消點贊功能        bean.setZanFocus(!flag);        AnimationTools.scale(holder.zan_img);      }    });    holder.shoucan_img.setOnClickListener(new OnClickListener() {      @Override      public void onClick(View v) {        // 獲取上次是否已經(jīng)被點擊        boolean flag = bean.isShoucanFocus();        // 判斷當前flag是收藏還是取收藏,是的話就給bean值減1,否則就加1        if (flag) {          bean.setShoucanNum(bean.getShoucanNum() - 1);        } else {          bean.setShoucanNum(bean.getShoucanNum() + 1);        }        // 反向存儲記錄,實現(xiàn)取消收藏功能        bean.setShoucanFocus(!flag);        //動畫        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.javapublic 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);  }}

代碼其實很簡單,稍作理解還是很容易弄懂的,我們在listview更新item中的數(shù)據(jù)的時候,一定要明白一個道理,不要在item的view中直接更改數(shù)據(jù)(剛開始做的時候我就是直接holder.zan_img.setImageResource(資源圖片),發(fā)現(xiàn)往下滑動的時候,上一次的觸摸記錄被下面的item給復用了,造成了數(shù)據(jù)的混亂),不然會造成數(shù)據(jù)的混亂,要是明白的listview的工作原理的話,可能會更清楚的明白,listview每次加載的數(shù)據(jù)是當前屏幕的一屏數(shù)據(jù)(其實我了解的不多,但是在打印log的時候,發(fā)現(xiàn)log出來初始化的數(shù)據(jù)就是一屏的數(shù)據(jù)),當你如果直接去改變view的樣式的話,你觸摸的當前item會被下面還未出現(xiàn)的item給復用掉,我是這樣理解的=-=。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 酒泉市| 邵阳市| 南丰县| 涪陵区| 北碚区| 柞水县| 鄂州市| 准格尔旗| 江华| 秦皇岛市| 宜宾市| 九江市| 观塘区| 五大连池市| 壶关县| 常熟市| 侯马市| 大化| 客服| 同江市| 桐乡市| 澄江县| 分宜县| 若羌县| 洛南县| 衢州市| 民权县| 时尚| 揭阳市| 连平县| 临漳县| 扎赉特旗| 秀山| 华阴市| 库车县| 清流县| 广河县| 古交市| 林州市| 临潭县| 汾西县|