欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2016年11月15日 10:14:00   作者:富堨going  
最近在開發(fā)android方面的項(xiàng)目時(shí),遇到很多問題,下面小編以listview 與 baseadapter結(jié)合使用為例,給大家分享下關(guān)于點(diǎn)贊的的時(shí)候 圖片重復(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ù)用掉,我是這樣理解的=-=。

相關(guān)文章

最新評(píng)論