尝试更改一个ListView元素Bug

时间:2016-03-19 20:30:02

标签: android listview click

我点击它时尝试从ListView更改ImageView。 我的方法如何尝试它看起来像这样:

<!DOCTYPE html>
<html>
  <head>
    <title>The Vincent Collection: Luxury in Haircare</title>
    <link rel="stylesheet" href="css/main.css">
  </head>
  <body>
    <div class="menucontainer">
      <img class="vcmenulogo" src="http://www.thevincentcollection.com/img/vc-logo-menu.png" />
      <ul class="menulinks">
        <li><a href="/">Home</a></li>
        <li><a href="/about/">About</a></li>
        <li><a href="/work/">Products</a></li>
        <li><a href="/contact/">Contact</a></li>
      </ul>
    </div>
    <div id="footer"> <p class="footer">Copyright &copy; 2016 Salon on Wheels, LLC. All rights reserved.</p> </div>
  </body>
</html>

这在我的ListView的private void updateEQAnimation(int index){ View v = LV_songView.getChildAt(index -LV_songView.getFirstVisiblePosition()); if(v == null) return; ImageView EQ_ANIM = (ImageView) v.findViewById(R.id.imageView_eq_animation); EQ_ANIM.setVisibility(View.VISIBLE); } 中调用,如下所示:

onItemClickListener

现在我的问题:

当我向上或向下滚动时,其他ListView元素的ImageView也会发生变化。但实际上我只想更改一个

1 个答案:

答案 0 :(得分:0)

最佳方法是在列表适配器中添加一个公共方法如何?我将描述。

  1. 首先创建您想要在列表视图中显示的对象。

    public class YourObject {
    
    private int id;
    private int avatar;
    private String name;
    
    public YourObject(int id, int avatar, String name) {
        this.id = id;
        this.avatar = avatar;
        this.name = name;
    }
    
    public int getAvatar() {
        return avatar;
    }
    
    public void setAvatar(int avatar) {
        this.avatar = avatar;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    @Override
    public String toString() {
        return new Gson().toJson(this);
    }}
    
  2. 制作如下的适配器:

    公共类YourAdapter扩展了BaseAdapter {

    private AppCompatActivity ctx;
    private LayoutInflater li;
    private LinkedHashMap<Integer, YourObject> items;
    
    public YourAdapter(AppCompatActivity ctx, LinkedHashMap<Integer, YourObject> items) {
        this.ctx = ctx;
        this.items = items;
        this.li = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    @Override
    public int getCount() {
        return items.size();
    }
    
    @Override
    public YourObject getItem(int position) {
        List<YourObject> lst = new ArrayList<>(items.values());
        return lst.get(position);
    }
    
    @Override
    public long getItemId(int position) {
        return 0;
    }
    
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        convertView = li.inflate(R.layout.lst_item_your_view, null);
        ImageView avatar = (ImageView) convertView.findViewById(R.id.avatar);
        CustomTextView name = (CustomTextView) convertView.findViewById(R.id.name);
    
        name.setText(getItem(position).getName());
    
        avatar.setImageResource(getItem(position).getAvatar());
    
        return convertView;
    }
    
    public void refresh(final YourObject yourObject) {
                items.remove(yourObject.getId());
                items.put(yourObject.getId(), yourObject);
                notifyDataSetChanged();
    }
    

    }

  3. 现在,在您的活动中,您可以在以下所有位置进行更改:

    YourAdapter adapter = new YourAdapter(ctx,items); listView.setAdapter(适配器); YourObject your object = new YourObject(id,R.drawable.other_icon,&#34; name&#34;); adapter.refresh(yourObject);