【问题标题】:How do I make this listview unselectable?如何使此列表视图无法选择?
【发布时间】:2011-11-28 01:53:36
【问题描述】:

我想让一个列表视图不可选择和不可点击。我说的是单击列表项时发生的颜色变化。代码如下。我是安卓新手,请多多关照。

来自:listitem.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8px">

<TextView
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#FFFFFF"/>

<TextView
    android:id="@+id/data"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="16px"/>

</LinearLayout>

来自:details.java

TestActionAdapter() {
        super(TestDetails.this, R.layout.action_list_item, actions);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TestAction action = actions.get(position);
        LayoutInflater inflater = getLayoutInflater();
        View view = inflater.inflate(R.layout.action_list_item, parent, false);
        TextView label = (TextView) view.findViewById(R.id.label);
        label.setText(action.getLabel());
        TextView data = (TextView) view.findViewById(R.id.data);
        data.setText(action.getData());
        return view;
    }

【问题讨论】:

    标签: android


    【解决方案1】:

    在这里查看答案:

    How to disable list items...

    基本扩展ArrayAdapter,并添加这2个功能:

     @Override
     public boolean areAllItemsEnabled() {
         return false;
     }
    
     @Override
     public boolean isEnabled(int position) {
         return false;
     }
    

    【讨论】:

    • 但是项目之间的分隔符现在已经消失了。有没有办法让他们回来?
    • 覆盖 areAllItemsEnabled() 方法似乎会删除列表视图分隔符,因此如果您想保留分隔符,请仅覆盖 isEnabled(int position)。
    • 如何使用BaseAdapter
    【解决方案2】:

    如果您只想阻止点击时突出显示所有行,只需使用ListView android:listSelector="@android:color/transparent"

    【讨论】:

      【解决方案3】:

      当你从getView(..) 方法返回视图时,只需在return view 之前输入view.setEnabled(false)

      【讨论】:

      • 我试过了,但是当我点击该项目时颜色仍然变为白色。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      相关资源
      最近更新 更多