我有一个 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 );
}