某些ListViews行无法点击

时间:2013-07-22 14:11:57

标签: android listview

我有一个ListView,其中每一行都有三个TextView。出于某种原因,当我滚动ListView时,我无法点击某些行。似乎没有任何特定的模式。我最初认为这是一个焦点问题,所以我添加了一些语句来消除TextViews中的可点击性和焦点,但这还没有解决它。这是我的代码的相关部分:

questionsArrayAdapter = new ArrayAdapter<String>(this, R.id.number, questionsArrayList) {
            public View getView(int position, View convertView, ViewGroup parent) {
                View row = convertView;

                LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                if (row == null) {
                    // ROW INFLATION
                    row = inflater.inflate(R.layout.questionlistitem, parent, false);
                }
                TextView number = (TextView) row.findViewById(R.id.number);

                TextView answer = (TextView) row.findViewById(R.id.answer);
                TextView section = (TextView) row.findViewById(R.id.section);

                number.setFocusable(false);
                number.setClickable(false);

                answer.setFocusable(false);
                answer.setFocusable(false);

                section.setFocusable(false);
                section.setClickable(false);

这是questionlistitem.xml:

<?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:orientation="horizontal" >

    <TextView
        android:id="@+id/number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dip"
        android:gravity="center_vertical"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:visible="false" />

    <TextView
        android:id="@+id/answer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:layout_marginRight="5dip"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:paddingEnd="10dip"
        android:paddingStart="6dip"
        android:textAppearance="?android:attr/textAppearanceListItemSmall" /> 

    <TextView
        android:id="@+id/section"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:visible="true" />

</LinearLayout>

这是我的标准听众:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    System.out.println("got clicked");

}

有谁知道问题可能是什么?

1 个答案:

答案 0 :(得分:0)

我认为您认为这是一个问题,而TextViews正在将焦点从您的点击事件中移开,我认为您正走在正确的轨道上。 I found this discussion of what may be the same problem filed as a bug report for the source,然而这是多年前的事情,官方的话似乎是“按预期工作”。在链接中,两个建议的解决方案似乎在xml中使用android:focusable="false"上的TextViews属性(不确定这是否等同于以编程方式执行,如您所尝试的那样)或调用{ <{1}}在setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);返回之前。{/ p>

可能导致此问题的其他问题是您的convertView如何安排在xml文件中。水平TextViews的孩子通常不应该有LinearLayout match_parent或fill_parent。这会导致这些视图扩展以填充父视图的长度,可能与其兄弟姐妹重叠,并可能在视觉上和尝试与子项进行交互时单击子项时造成奇怪。您可能需要考虑使用layout_width属性,或将父级设为layout_weight,您可以更好地控制视图的位置。

相关问题