简体   繁体   English

Android ListView操作在不同项目中重复

[英]Android ListView actions repeat in different items

I have a custom layout for a Listview, each row item contains a button that when clicked it shows a small imageview in the item, however the actions i perform in one item, repeats for another item down the list, for example, if i click the button in item 1, the imageview will show up in item 1 and item 10, if i click the button in item 2, the Imageview will show up on item 2 and item 11 and while i scroll it will keep repeating in different items, heres the code for my custom adapter: 我有一个用于Listview的自定义布局,每个行项目都包含一个按钮,当单击该按钮时,该按钮在项目中显示一个小imageview,但是我在一个项目中执行的操作会重复执行列表中的另一个项目,例如,如果我单击项目1中的按钮,imageview将显示在项目1和项目10中,如果我单击项目2中的按钮,Imageview将显示在项目2和项目11中,而当我滚动时,它会在不同的项目中重复出现,这是我的自定义适配器的代码:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        mparent = parent;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.places_item, parent, false);

            holder = new ViewHolder();
            holder.placeimage = (CircularImageView) convertView.findViewById(R.id.locationimage_pilayout);
            holder.addbtn = (TextView) convertView.findViewById(R.id.addbtn_pilayout);
            holder.delbtn = (TextView) convertView.findViewById(R.id.delbtn_pilayout);
            holder.oribtn = (TextView) convertView.findViewById(R.id.oribtn_pilayout);
            holder.placename = (TextView) convertView.findViewById(R.id.locationname_pilayout);
            holder.selected = (ImageView) convertView.findViewById(R.id.selected_pilayout);
            holder.origin = (ImageView) convertView.findViewById(R.id.origin_pilayout);
            holder.swipeLayout = (SwipeRevealLayout) convertView.findViewById(R.id.swipe_pilayout);
            holder.mainLayout = (LinearLayout) convertView.findViewById(R.id.main_pilayout);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final Place item = getItem(position);
        /*
        my code assigning the button click listener and assigning the views
        */
        return convertView;
    }

Am i missing something? 我想念什么吗? im sure this could be a simple fix, but i havent found it yet. 我肯定这可能是一个简单的修复,但我还没有找到它。 any help would be kindly appreciated. 任何帮助将不胜感激。

In ListView the individual views for rows, get reused. ListView中,行的各个视图将被重用。 For example, let's say the window can show up to 10 rows inside the ListView . 例如,假设该窗口最多可以在ListView显示10行。 Now when you scroll down, the 1st view gets out of the window from the top, and a new 11th view comes into the window from the bottom. 现在,当您向下滚动时,第一个视图从顶部移出窗口,而新的第11个视图从底部移入窗口。 In order to save memory and CPU power, Android uses the same 1st view for the 11th view. 为了节省内存和CPU能力,Android将相同的1st视图用于11th视图。 That's the purpose of convertView and the if (convertView == null) {} else {} code. 这就是convertViewif (convertView == null) {} else {}代码的目的。

So the reason why the image is being shown in the 1st item and also in the 11th item, is that they are exactly one view object. 因此,在第1项和第11项中显示图像的原因是它们恰好是一个视图对象。 To tackle this issue, in the getView() method, you need to reset every attribute of every view and don't rely on the defaults. 要解决此问题,您需要在getView()方法中重置每个视图的每个属性,并且不要依赖默认值。

So adding a line like the one below, will get rid of the mentioned problem: 因此,添加如下所示的一行将摆脱上述问题:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    // all your code ...

    holder.placeImage.setImageResource(0);  //<-- This clears any previously set image.
    return convertView;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM