简体   繁体   English

ANDROID:适配器位置返回零,并将视图重新添加到ListView

[英]ANDROID: Adapter position returns to zero and re-adds views to ListView

My app requires the ability to add several views on one line in a user selected order. 我的应用要求能够以用户选择的顺序在一行上添加多个视图。

I found this tutorial which seems to accomplish what I want with a bit of modification. 我发现本教程似乎通过一些修改即可完成我想要的工作。

http://www.androidpeople.com/android-custom-listview-tutorial-example/

Having followed the tutorial and made the required changes, the code works except for one strange issue. 在遵循了教程并进行了必要的更改之后,该代码可以正常工作,除了一个奇怪的问题。 The position increments but when it hits ~9 it returns to zero and then re-adds views that are already in the list and thus never reaches the >9 ones. 位置会增加,但是当它达到〜9时将返回零,然后重新添加列表中已经存在的视图,因此永远不会到达> 9的视图。

Also, if I scroll down to the bottom and then back up the very first entry has changed! 另外,如果我向下滚动到底部然后再备份,则第一个条目已更改! It may change more but I haven't checked that. 可能会有更多变化,但我尚未检查。

Through some tests I have discovered that the textSize has some effect. 通过一些测试,我发现textSize有一定作用。 If I set it small enough so that all 'rows' will show on screen at once then they appear fine. 如果我将其设置得足够小,以便所有“行”都可以立即显示在屏幕上,那么它们看起来就很好。

This is my listview layout that gets inflated into the main layout: 这是我的listview布局,被放大为主要布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:minHeight="60dip"
  style="@style/DefaultTheme">
    <TextView 
      android:id="@+id/Line01"
      android:layout_width="5dip"
      android:layout_height="fill_parent"
      android:background="#F00" />
    <TextView
      android:id="@+id/Line02"
      android:layout_width="5dip"
      android:layout_height="fill_parent"
      android:background="#0F0" />
    <TextView
      android:id="@+id/Line03"
      android:layout_width="5dip"
      android:layout_height="fill_parent"
      android:background="#00F"
      android:layout_marginRight="5dip"/>
    <LinearLayout
      android:id="@+id/LinearLayout"
      android:layout_weight="1"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:orientation="vertical"
      android:padding="2dip">
        <TextView android:id="@+id/Name"
          android:text="Name"
          style="@style/Name" />
        <TextView 
          android:id="@+id/Status"
          android:text="Status"
          style="@style/Status" />
    </LinearLayout>
    <ImageView 
      android:id="@+id/StatusImage"
      style="@style/StatusImage" />
</LinearLayout>

I change background colours and text but that is all. 我更改背景颜色和文本,仅此而已。 Any ideas what is the problem? 任何想法是什么问题?

Thanks! 谢谢!

That's because ListView recycles Views. 那是因为ListView回收视图。 It creates as many Views as required then the one which isn't available will be recycled as next. 它创建所需数量的视图,然后将不可用的视图作为下一个回收。

So you have to set the colors/attributes in the getView or bindView methods of the adapter. 因此,您必须在适配器的getView或bindView方法中设置颜色/属性。

And btw. 顺便说一句。 what you posted doesn't inflate the ListView, as there is no `' object in it. 您发布的内容不会使ListView膨胀,因为其中没有''对象。 At best it could be a single ListView item, but not the listview itself ^^ 充其量只能是一个ListView项,但不是listview本身^^

I feel dumb for asking this now and not going back over the tutorial completely. 我现在问这个问题而又没有完全回顾本教程,这真是愚蠢。

I was setting the values in my layout I place in the ListView on my main layout only when a new ViewHolder was created. 我正在设置布局中的值,仅当创建新的ViewHolder时,才将其放置在主布局中的ListView中。 Thus when I scrolled, the views were recycled, but never updated with the correct View settings. 因此,当我滚动时,视图被回收,但从未使用正确的“视图”设置进行更新。

All working fine now! 现在一切正常!

Cheers 干杯

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

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