【问题标题】:Android: Listview, view - hilighting the correct rowAndroid:Listview,视图 - 突出显示正确的行
【发布时间】:2011-08-30 00:33:36
【问题描述】:

我正在尝试突出显示 Android 测验程序中的选定行。但是,我得到了一些奇怪的行为。基本上,传递给 onListItemClick 的位置正确地反映了支持数据数组中的位置,但是显然使用子索引查找的视图是错误的。子索引似乎与位置相反,就像一个堆栈。使用这个假设,我可以使代码工作,但它是违反直觉的。

代码如下:

public class QuestionActivity extends ListActivity {

...

        /*
         * Create the adapter
         */
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.simple_list_item_single_choice_custom,
                mAppState.mAnswerArray) {
        };

        /* attach the adapter to the ListView */
        setListAdapter(adapter);

        /* set ListView's choice mode to single */
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    }

....

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);


    // always four rows; assume last row is zeroth child. Therefore,
    // if the first row is chosen (0 position) you need to get child 3.
    // 3 - 0 = 3.

    int childCount = l.getChildCount();
    int lastViewPos = childCount - 1;
    View selView = l.getChildAt(lastViewPos - position);
}

...

    <TextView android:id="@+id/display_question"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:textSize="40sp" android:typeface="normal" android:padding="10dp"
        android:layout_gravity="center_vertical|center_horizontal"
        android:textColor="@color/text_color" />
    />

    <ListView android:id="@+id/android:list"
        android:cacheColorHint="#00000000" android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:textColor="@color/text_color"
        android:divider="#00000000" android:dividerHeight="0sp"
        android:focusable="false">
    </ListView>
    <TextView android:id="@+id/display_english"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:paddingLeft="20dp" android:layout_gravity="center_vertical|center_horizontal"
        android:textSize="30sp" android:textColor="@color/text_color" />

    <Button android:id="@+id/next_button1" android:text="@string/next_question"
        android:textSize="15dp" android:layout_gravity="center_vertical|center_horizontal"
        android:textColor="@color/text_color" android:background="@drawable/custom_button"
        android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>

... 这是 simple_list_item_single_choice_custom.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="30sp" 
    android:gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorSingle"
    android:paddingLeft="20dip" 
    android:textColor="@color/text_color"
    android:background="@drawable/custom_list"
/>

我还尝试不在 onClick 事件上调用 super,并从代码中将 stackFromBottom 设置在列表视图上,无论是真还是假,但这些似乎都没有任何区别。

有什么更令人满意、更直观的方法来做到这一点?

【问题讨论】:

  • 你是直接从 ListView 覆盖 onListItemClick 吗?或者两者之间是否有任何层(例如,您是从 ListView 派生的)?我只是想知道 super.onListItemClick 中是否发生了导致这种奇怪的事情。
  • 你是如何绑定你的 ListView 的?您是手动添加项目,还是使用适配器?
  • 这是 AFAIK 的样板。我在活动中扩展 ListView,并且正在使用适配器。我会在上面添加相关代码。
  • 我的想法不多了 :) 你能发布“simple_list_item_single_choice_custom”的 XML 吗?也许那里有线索。

标签: android highlight listitem


【解决方案1】:

你有没有机会指定android:stackFromBottom?我不知道这是否会改变 getChildAt 的行为方式(我目前没有任何要测试的东西),但这是我的第一个猜测。

【讨论】:

  • 不,没有在任何地方指定。
  • 我确实尝试在代码中设置它,无论是真还是假,它似乎没有任何区别。我确信这与此有关,它在 ListActivity 代码中无处不在。
【解决方案2】:

它应该设置为从顶部开始,但它从 1 开始(与数组索引等不同)。

【讨论】:

  • 不,它们都从零开始。子索引基本上与位置相反。
  • 我的“位置”变量按照我的描述工作。我不知道如果我们没有手动改变任何东西(比如 stackFromBottom 或其他东西),为什么你的工作方式会有所不同......(我没有:p)
  • 位置变量对我来说也按预期工作 - 但是当我根据位置获取孩子时,它会反转!
【解决方案3】:

伙计,我不敢相信我错过了这个。没有适当注意实际问题。

您正在尝试将 selView 的值设置为被点击的项目,对吧?这就是 onListItemClicked 的“View v”参数。

View selView = v;

会做你正在寻找的。

getChildAt() 的行为与您期望的不同,因为它是从 ViewGroup 继承的;在 ListView 中,它不一定只对应于列表项,因为 ViewGroup 中可能还有其他内容(例如,页眉和页脚)。简而言之,它并不打算以您使用它的方式使用。在这种情况下它与位置相反可能只是一个巧合。

【讨论】:

  • E.Z. - 是的,这就是我想要做的。您的解决方案是我尝试的第一件事;不幸的是,那是我第一次注意到奇怪的行为。看起来虽然数据显示的顺序正确,但节点的分配顺序却相反。
猜你喜欢
  • 2012-01-21
  • 1970-01-01
  • 1970-01-01
  • 2016-06-09
  • 1970-01-01
  • 1970-01-01
  • 2015-03-19
  • 2013-09-07
  • 2014-02-27
相关资源
最近更新 更多