【问题标题】:ListView - view is related with many rowsListView - 视图与许多行相关
【发布时间】:2015-07-11 16:01:19
【问题描述】:

我希望 ListView 中的项目在我触摸时消失。它正在工作,但是 问题是当我滚动列表时。随机项目正在消失或显示。 这是我的代码:

private ListView list;
(...)
list = (ListView) findViewById(R.id.units_list);
adapter = new UnitAdapter(this, R.layout.row_list, units);
list.setAdapter(adapter);

list.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            v.setVisibility(View.INVISIBLE);
        }

    });

列表视图 XML:

<ListView
    android:id="@+id/units_list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@id/strut"
    android:layout_marginTop="5dp" >
</ListView>

row_list.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="${relativePackage}.${activityClass}" 
android:descendantFocusability="blocksDescendants"    >

<ImageView
    android:id="@+id/img"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginBottom="15dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="15dp"
    android:gravity="center_vertical" />

<TextView
    android:id="@+id/description"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="5dp"
    android:layout_toRightOf="@id/img"
    android:gravity="center_vertical"
    android:textSize="14sp" />

<ImageView
    android:id="@+id/upgrade"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="15dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="15dp"
    android:gravity="center_vertical" />

我也试过了:

public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            list.getChildAt(position).setVisibility(View.INVISIBLE);
        }

甚至:

public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            list.getChildAt((int) id).setVisibility(View.INVISIBLE);
        }

但它的工作原理相同。 问题是什么?为什么 View 与多行相关?我该如何解决这个问题?

【问题讨论】:

  • listview 重用可见区域外的项目

标签: java android listview


【解决方案1】:

这可能与Android中ListView中的视图被回收有关,所以当你编辑一行时,你可能会看到滚动多行时的不同。

一种解决方案可能是更新底层适配器,然后调用 notifyDataSetChanged。但可能会有更顺畅的解决方案。

【讨论】:

    【解决方案2】:
    1. 使用 View Holder 设计模式。
    2. 当您从列表视图中删除项目时,也从列表中删除相同的项目(在您的案例单元中)。

    【讨论】:

      猜你喜欢
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多