简体   繁体   English

滚动后才能选择列表中的项目

[英]Cannot Select Items In List Until After Scrolling

I'm having a problem with an android activity that extends ListActivity . 我在扩展ListActivity的android活动中ListActivity It seems like the items in the list cannot be "clicked" until after I've scrolled the list. 直到我滚动列表之后,列表中的项目似乎才能被“单击”。 After you scroll the list a little, the "clicking" works fine. 稍微滚动列表后,“单击”效果很好。 If the list simply is not scrollable (not enough items, etc) then everything works as it. 如果列表根本无法滚动(没有足够的项目等),则一切正常。

Since the activity extends ListActivity I just override onListItemClick : 由于活动扩展了ListActivity我仅重写onListItemClick

@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
   Log.e(TAG, "Click fired");
}

The rows in the list are defined by a custom XML, and after reading through virtually all of the posts on here I'm thinking it has to do with focusable elements, but I can't seem to get it to work. 列表中的行是由自定义XML定义的,在通读了此处几乎所有文章之后,我认为它与可聚焦元素有关,但是我似乎无法使其正常工作。 As you can see from the list XML below, all the items have focusable="false" , but that didn't help. 从下面的XML列表中可以看到,所有项目都具有focusable="false" ,但这无济于事。

I've also tried all the options for android:descentFocusability -- but none work properly. 我也尝试了android:descentFocusability所有选项-但没有一个能正常工作。

This problem is apparent on all versions of Android from Froyo --> ICS 在Froyo-> ICS的所有Android版本上,此问题均显而易见

Edit 编辑

I have determined that this problem goes away if I remove the onScroll listener that is attached to the same list. 我已确定,如果删除附加到同一列表的onScroll侦听器,此问题将消失。 However, I really need that and I've put logging info inside both onScroll and onScrollStateChanged and nothing informative shows up (eg, scrolling isn't being fired either.) Is this a known conflict? 但是,我确实需onScroll ,并且已经将日志记录信息放在onScrollonScrollStateChanged并且没有显示任何信息(例如,也不触发滚动。)这是否是已知冲突?

Layout XML for activity in question: 有关活动的布局XML:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:focusable="false"
    android:focusableInTouchMode="false">
   <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:focusableInTouchMode="false">
     <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Search Results: "
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_gravity="left" 
            android:focusable="false"
            android:focusableInTouchMode="false" />
         <TextView android:id="@+id/query_type"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="context sensitive"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_gravity="right" 
            android:textStyle="italic"
            android:focusable="false"
        android:focusableInTouchMode="false" />
       </FrameLayout>
       <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minHeight="1dip"
        android:background='@color/grey'
        android:focusable="false"
        android:focusableInTouchMode="false" />
<ListView
    android:id="@+id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:cacheColorHint="#00000000"
    android:descendantFocusability="blocksDescendants" />
<TextView
    android:id="@+id/prodcutdb_results_empty"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="5dip"
    android:paddingTop='4dip'
    android:text="No results"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:visibility="gone"/>
</LinearLayout>

Custom list item XML: (adding focusable tags to the LinearLayout in this XML has no effect) 自定义列表项XML :(在此XML中将可聚焦标签添加到LinearLayout中无效)

<?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="?android:attr/listPreferredItemHeight"
    android:orientation="horizontal"
    android:paddingRight="4dip"
    android:cacheColorHint="#00000000">
<TextView 
    android:id="@+id/list_item_label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:paddingLeft="20dip"
    android:textSize="16dp" 
    android:layout_weight="1"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false" />
    <view  
        android:layout_width="wrap_content"
        android:id="@+id/list_item_image"
        android:layout_height="fill_parent"
        android:src="@drawable/right"
        android:maxHeight="50dp"
        android:maxWidth="50dp"
        android:layout_gravity="right"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false" />

</LinearLayout>

In your custom list item XML, did you intend for the 3rd tag to be ImageView? 在您的自定义列表项XML中,您是否打算将第3个标签设置为ImageView? I'm not sure if this is what is causing your problem, but if the sdk sees that your XML is not well-formed it may cause issues. 我不确定这是否是导致您出现问题的原因,但是如果SDK认为您的XML格式不正确,则可能会导致问题。

You should put your list in a normal LayoutView (don't use a scrollView) that can appear and make the rest of your layouts disappear and vice versa when needed so the list can have the complete screen for it. 您应该将列表放在可以显示的普通LayoutView(不要使用scrollView)中,并在需要时使其余布局消失,反之亦然,以便列表可以具有完整的屏幕。 that's how it works for me. 这就是对我有效的方式。

EDIT I use it like this 编辑我像这样使用它

  <RelativeLayout
    android:id="@+id/NewsListLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" android:visibility="gone">

    <ListView
        android:id="@+id/mylistView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</RelativeLayout>

To keep things short -- nothing worked. 为了简短起见-没有任何效果。 I added an onClick listener to each one of the custom views. 我向每个自定义视图添加了一个onClick侦听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM