0

我有一个 ListView,其中的行包含 ImageButtons(如下所示的“O”):

[<-------TextView-------> O O O]
[<-------TextView-------> O O O]

我发现第一行有间歇性行为。起初,点击监听器上的图像按钮似乎并没有被调用只是顶行。我发现确实调用了按钮的 onclicklisteners,但是在我单击行本身之前,单击似乎是排队/缓冲的。

如果没有人知道原因,我会很乐意发布一些代码,但我的代码非常大,需要在发布之前进行修剪。我正在使用光标适配器和自定义视图绑定器,但单击侦听器是在我的活动的 onCreate() 中分配的。

我将点击监听器本身分配给我的 CustomViewBinder 类的 setViewValue() 方法中的图像按钮对象。

我没有使用 ViewHolder(ConvertView 等) - 这是我的问题吗?看起来好像每行都为点击侦听器分配了 3 次,尽管我只在 columnIndex 等于 1 时才这样做。

这是行项目 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_header"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="@drawable/row_selector"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_header"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0"
        android:layout_gravity="center_vertical"
        android:paddingLeft="4dp"
        android:background="@color/listSection"
        android:text=""
        android:textColor="@android:color/white"
        android:textStyle="bold"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal" >

    <!--
      ~ Use android:layout_weight so that this view will take up all the space
      ~ remaining when no other view has specifed its weight.
     -->
    <TextView
        android:id="@+id/lv_tune"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:layout_marginLeft="10dp"
        android:text="Tune"
        android:textSize="20sp"
        android:textColor="@color/lvForeground"/>

    <!-- 
      ~ android:gravity is for the gravity in the View itself, while
      ~ android:layout_gravity is for the gravity of the View in it's
      ~ ViewGroup(container), in this case it may not be what you intended to
      ~ do, so I removed it for you.
     -->

    <ImageView
        android:id="@+id/ibAddPlaylist"
        android:layout_width="40dp"
        android:layout_height="34dp"
        android:layout_gravity="center"
        android:visibility="visible"
        android:src="@drawable/clipboard30x30grey" />

    <ImageView
        android:id="@+id/ibLikeHate"
        android:layout_width="40dp"
        android:layout_height="34dp"
        android:layout_gravity="center"
        android:visibility="visible"
        android:src="@drawable/heart1_30x30grey" />

    <ImageView
        android:id="@+id/ivPlay"
        android:layout_width="40dp"
        android:layout_height="34dp"
        android:layout_gravity="center"
        android:visibility="invisible"
        android:src="@drawable/play_30x30" />

  </LinearLayout>
</LinearLayout>

这是 getView() 方法:

       @Override
       public View getView( int position, View convertView, ViewGroup parent )
       {
         final View view = super.getView( position, convertView, parent );
         final TextView text = (TextView) view.findViewById( R.id.lv_tune );
         final LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) text.getLayoutParams();

         if( params != null )
         {
           int hei = params.height;

           if( IsStartOfAlphaSection( position ) )
           {
             // PDS: Looking at the layout for the TextView here..
             if( hei == LayoutParams.WRAP_CONTENT )
             {
               // PDS: But modifying the layout of the parent view..
               // view.setBackgroundColor( 0xFFDDDDDD );

               // PDS: Parent does not have LinearLayout - uses
               // AbsListView.LayoutParams
//*** BELOW LINE IS THE PROBLEM ..
               view.setLayoutParams( new AbsListView.LayoutParams( AbsListView.LayoutParams.MATCH_PARENT, g_NormalRowHeight + g_SectionHeaderHeight ) );
               view.requestLayout();
//***
             }
           }
           else
           {
             // PDS: Normal line

             // PDS: Looking at the layout for the TextView here..
             if( hei == LinearLayout.LayoutParams.WRAP_CONTENT )
             {
               // PDS: But modifying the layout of the parent view..
               // view.setBackgroundColor( Color.WHITE );
               view.setLayoutParams( new AbsListView.LayoutParams( AbsListView.LayoutParams.MATCH_PARENT, g_NormalRowHeight ) );
               view.requestLayout();
             }
           }
         }

         return view;
       }

我已经突出显示了导致我的问题的行。我所做的是使作为组/节标题的项目的行高更大。这改变了使用的布局,并且似乎也弄乱了第一行图像视图的点击处理程序。它还导致触摸第一行以连续突出显示第一行。单击任何其他行,突出显示消失。

更新:考虑到 mmlooloo 的建议,下面的代码似乎对我有用。虽然看起来很丑(我发布的原始代码也是如此!;-))

public View getView( int position, View convertView, ViewGroup parent )
{
     final View view;

     LayoutInflater li = (LayoutInflater) mContext.getSystemService( Activity.LAYOUT_INFLATER_SERVICE );

     if( IsStartOfAlphaSection( position ) )
     {
       view = li.inflate( R.layout.like_hate_row_header,  parent, false );
     }
     else
     {
       view = li.inflate( R.layout.like_hate_row,  parent, false );
     }

     // NOT calling super.getView() stops ViewBinder::setViewValue() from being called
     super.getView( position, view, parent );     
}   
4

1 回答 1

0

我认为您的 getView 有一些问题,首先您不膨胀任何布局,并且您使用的是 getView 的构造函数,这是个坏主意,您必须膨胀 convertView 或使用传递给您的那个,但在您的情况下,您想使用不同的查看不同位置的视图我认为您应该始终为 convertView 充气。我建议您查看此问题和答案以及我的答案下面的评论,以更好地了解 convertView。

android列表数组adapter.notifydatasetchanged没有正确更新视图

因此,如果我想解决您的问题,我会这样做:首先创建两个 XML 文件,一个代表我的无标题项,另一个代表我的标题项。在getView中我检查位置,如果它必须是标题,我用标题xml膨胀它,否则我用无标题xml膨胀它,所以它会变成这样:

public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = 
                (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View view;
        if(position == header){
              view = inflater.inflate(R.layout.header, parent, false);
              // here you must call finViewById of specific objects or imageButtons 
              //which appear in the header and assign them click listeners  
        }else{
             view = inflater.inflate(R.layout.none_header, parent, false);
              // here you must call finViewById of specific objects or imageButtons 
              //which appear in the NONE header and assign them click listeners 
        }

        return view;

}

并将每个 imageButton 的可聚焦设置为 false;

于 2014-08-07T10:07:54.090 回答