【问题标题】:multiple views are effecting, ListView android多个视图正在影响,ListView android
【发布时间】:2012-07-19 10:33:27
【问题描述】:

我的 ListView 上有 8 个(可见 4 个)列表项。每个视图包含一个 TextView 和一个 ImageView(最初设置为透明)。现在正在尝试从 onItemClick 方法将 img 设置为 imageView。它对我来说工作正常,但是当我向下滚动其他一些视图时也会产生影响。例如,如果我选择第 0 个位置项目,则第 0 个和第 4 个位置视图都设置为相同的 img。我该如何解决这个问题。

java代码:

list.setAdapter(new SimpleAdapter(this,
            application.distanceList, R.layout.drop_down_view, from, to));
list.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View v, int arg2,
                long arg3) {
            try {
                ((ImageView) temp.findViewById(R.id.ddviv))
                        .setImageResource(android.R.color.transparent);
            } catch (NullPointerException e) {

            }
            ((ImageView) v.findViewById(R.id.ddviv))
                    .setImageResource(R.drawable.drop_sel);
            temp = v;}
    });

【问题讨论】:

  • 您是否使用 CustomAdapter 进行列表?如果是,则提供 getView() 代码。如果没有,则执行它。
  • 你说得对,这种问题在ListView出现很多次
  • 最好发布适配器类代码。
  • 实现 CustomAdapter 而不是 SimpleAdapter 并在适配器的 getView() 中放置应用图像的条件 if - else 用于特定位置。

标签: android android-listview


【解决方案1】:

您必须在getView() 方法中使用带有ViewHolders 的自定义适配器(扩展BaseAdapter 是一个不错的选择)。当你点击时,你改变了一些属性并调用notifyDataSetChanged()

如果你不知道如何实现自定义Adapter,Google 有很多吐槽=)

【讨论】:

  • 在我的应用程序中,我有很多这样的不同列表视图。我对为所有人创建自定义适配器不感兴趣。还有其他方法吗
  • @Babloo 为什么不对所有相同的 listViews 使用相同的自定义适配器类?我不明白你
  • 我的要求是一样的,但是列出的项目还是不同的。在这种情况下,我有 1 个 TextView 和 1 个 ImageView,但在其他情况下,我也有一些额外的项目。
  • @Babloo 然后您可以创建一个通用的自定义适配器,并将其扩展为您的其他列表。
【解决方案2】:

ListView 的行中设置这些图像的另一个选项是进行一些数据调整并使用SimpleAdapter.ViewBinder。我不知道你是如何为行视图设置数据的(我看不到你在 fromto 数组中有什么)或者你在 application.distanceList 中有什么所以这是一个小例子.首先,在来自application.distanceList 的每个Map(对应于ListView 行)中,您必须添加这样的条目:

map.put("position", "x"); // where x is the list row number(for the first Map in application.distanceList x will be 0, for the second Map in application.distanceList x will be 1 etc)

您将在from 数组中包含"position",并与此"position" 对应,您将从行布局中将ImageView 的ID 放入to 数组中。

接下来,您必须添加SimpleAdapter.ViewBinder 以将图像绑定到ListView 行(这将处理ListView 的回收机制):

mAdapter.setViewBinder(new SimpleAdapter.ViewBinder() {

            @Override
            public boolean setViewValue(View view, Object data,
                    String textRepresentation) {
                            // view is the ImageView from the row layout
                            // data is the x from the position column
                if (view.getId() == R.id.imageView1) {
                    Integer rowNumber = Integer.parseInt((String) data);
                                    // below is the explanation for mImageIds
                    ((ImageView) view).setImageResource(mImageIds[rowNumber]);
                    return true;
                }
                return false;
            }
        });

mImageIds 是一个ints 数组,它将保存ListView 行的drawable 的ID:

// a field in your class
private int[] mImageIds = new int[8]; // you said you had 8 rows

//.. in the onCreate method initialize the mImageIds array

    for (int j = 0; j < 8; j++) {
        mImageIds[j] = R.drawable.ic_launcher;// a default image(you could use android.R.color.transparent for empty)
    }

终于在onItemClick回调中:

public void onItemClick(AdapterView<?> arg0, View v, int arg2,
                long arg3) {
       mImageIds[position] = android.R.drawable.btn_minus; // the desired image
       mAdapter.notifyDataSetChanged();
}

这听起来比较复杂,希望你明白原理,另一种解决方案是像其他用户所说的那样构建一个自定义适配器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 2015-08-15
    • 2011-02-21
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多