Android:Listview,view-hilighting显示正确的行
我试图突出显示Android问答程序中选定的一行。然而,我有一些奇怪的行为。基本上,传递到onListItemClick的位置正确地反映了备份数据数组中的位置,但显然使用子索引查找的视图是错误的。子索引似乎与位置相反,就像一个堆栈。使用这个假设,我可以让代码工作,但这是违反直觉的 代码如下:Android:Listview,view-hilighting显示正确的行,android,highlight,listitem,Android,Highlight,Listitem,我试图突出显示Android问答程序中选定的一行。然而,我有一些奇怪的行为。基本上,传递到onListItemClick的位置正确地反映了备份数据数组中的位置,但显然使用子索引查找的视图是错误的。子索引似乎与位置相反,就像一个堆栈。使用这个假设,我可以让代码工作,但这是违反直觉的 代码如下: public class QuestionActivity extends ListActivity { 。。。 /> 。。。 下面是简单的列表项单选项自定义.xml <?xml versi
public class QuestionActivity extends ListActivity {
。。。
/>
。。。
下面是简单的列表项单选项自定义.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,无论是true还是false,但这些似乎都没有任何区别
更令人满意、更直观的方法是什么 您是否有指定的机会?我不知道这是否会改变getChildAt的行为(目前我没有任何东西可以测试),但这将是我的第一个猜测。它应该设置为从顶部开始,但它从1开始(与数组索引等不同)。伙计,我不敢相信我错过了这个。没有对实际问题给予适当的关注 您正在尝试将selView的值设置为单击的项目,对吗?这就是onListItemClicked的“View v”参数
View selView = v;
我会做你想做的事
getChildAt()的行为与您期望的不同,因为它是从ViewGroup继承的;在ListView中,它不一定只对应于列表项,因为ViewGroup中可能有其他内容(例如页眉和页脚)。简言之,它并不是你使用它的方式。它与本例中的位置相反,这可能只是巧合。不,这在任何地方都没有指定。我在代码中尝试过设置它,包括true和false,但似乎没有任何区别。我确信这与此有关,它在ListActivity代码中无处不在。不,它们都从零开始。子索引基本上与位置相反。我的“位置”变量的工作方式与我描述的相同。我不知道如果我们没有手动更改任何东西(如stackFromBottom或其他东西),为什么您的工作方式会有所不同……(我没有:p)位置变量对我来说也会像预期的那样工作-但是当我根据位置得到子对象时,它会出现反向!是否直接从ListView重写onListItemClick?或者中间是否有任何层(例如,您是否从ListView派生)?我只是想知道super.onListItemClick中是否发生了导致这种奇怪的事情。您是如何绑定ListView的?您是手动添加项目,还是使用适配器?这是样板文件。我在活动中扩展ListView,并使用适配器。我将添加上面的相关代码。我已经没有想法了:)你能发布“简单列表\项目\单一选择\自定义”的XML吗?也许有线索,E.Z是的,这就是我想做的。你的解决方案是我尝试的第一件事;不幸的是,那是我第一次注意到这种奇怪的行为。虽然数据的显示顺序正确,但节点的分配顺序却相反。
<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>
<?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"
/>
View selView = v;