【问题标题】:ListView item setTextColor modified other elementsListView 项 setTextColor 修改其他元素
【发布时间】:2011-08-11 19:09:05
【问题描述】:

我有一个由 SimpleCursorAdapter 和自定义 ViewBinder 支持的 ListView。我想让这个列表视图中的项目在点击时改变它们的颜色。如果我在 OnClickListener 中执行此操作 - 它会部分工作,每 7 次更改单击的项目的颜色以及列表中项目的颜色(我猜,时间段取决于列表视图的可视区域)。

谁能建议如何处理这个问题?或者,也许指向一种更优雅的方式使 listView 中的项目可选?

谢谢。

UPD:(抱歉格式错误 - 这是我第一次发布问题):

以下是我如何尝试使 ListView 中的项目“选中”:

    private void setupListView(final ListView lv) {

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int position, final long id) {
                RelativeLayout layout = (RelativeLayout) view;
                int color;
                if (conditionMet) {
                      color = R.color.gray;
                 } else {
                      color = R.color.red;
                 }

                 for(int i = 0; i < layout.getChildCount(); i++) {
((TextView)layout.getChildAt(i)).setTextColor(getResources().getColor(color)); 
    }

                 return;
            }}

这就是我初始化适配器的方式:

        final SimpleCursorAdapter adapter =
                new SimpleCursorAdapter(
                        this,
                        itemId,
                        cursor,
                        from,
                        to
                );
        adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

            public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                final TextView textView = (TextView) view;
// do necessary conversions
                return true;
            }
        });
listView.setAdapter(adapter);

【问题讨论】:

  • 添加了一些代码,还有什么要添加的吗?实际上,正如我所说,这部分起作用,因为它确实“突出”了项目本身,但我想它的视图被重用了

标签: android listview text colors onclick


【解决方案1】:

您可以使用属性android:listSelector 设置列表中当前选定项目的主题或任何可绘制对象或颜色。

【讨论】:

    【解决方案2】:

    由于没有其他答案,而且我认为我对下面的建议有些麻烦,我发布了我是如何做到的:

    1. 我将点击的项目的 ID 存储在一个特殊的地图中

    2. 在 listview onclick 我检查刚刚点击的项目的 id 是否在地图中:如果是,我将其删除并使项目及其子项颜色为 A,否则我将 id 添加到地图并设置颜色到B

      public void onItemClick(AdapterView<?> adapterView, View view, int position, final long id) {
                  Context ctx = MainActivity.this;
                      RelativeLayout layout = (RelativeLayout) view;
                      try {
      
                              int color;
                              if (items.containsKey(id)) {
                                  items.remove(id);
                                  color = R.color.gray;
                                  tempIds.remove(id);
                              } else {
                                  items.put(id, sum);
                                  color = R.color.red;
                                  tempIds.add(id);
                              }
      
      
                          for (int i = 0; i < layout.getChildCount(); i++) {
                              final TextView textView = (TextView) layout.getChildAt(i);
                              textView.setTextColor(getResources().getColor(color));
                          }
                      } catch (ParseException e) {
                          Log.e(MainActivity.class.toString(), "Exception parsing", e);
                      }
                      return;
                  }
      

      }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-16
      • 2015-01-15
      • 1970-01-01
      • 1970-01-01
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多