Android的ImageButton的ListView中排clicklistener没有响应 [英] Android ImageButton in ListView row clicklistener not responding

查看:299
本文介绍了Android的ImageButton的ListView中排clicklistener没有响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含ImageButtons行(如下图所示的O),一个ListView:

  [< ------- ------- TextView的GT&; O O O]
并[d ------- TextView的-------> O O O]

我发现,第一行具有间歇行为。起初,它出现的图像按钮onclicklisteners没有被要求只排在前列。我发现是,对于按钮onclicklisteners 的被调用,但是点击似乎排队/缓冲,直到我点击该行本身。

我会很高兴发布一些code,如果没有人知道这样做的原因,但我的code是相当大,需要修剪张贴在这里了。我使用的是光标适配器和定制viewbinder但点击侦听我活动的onCreate内分配的()。

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

我没有用一个ViewHolder(ConvertView等等) - 这是我的问题吗?它看起来好像点击听众都被分配3次,每一行,即使我只有这样做,如果该参数:columnIndex等于1。

下面是该行的项目XML:

 <?XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / layout_header
    机器人:layout_width =match_parent
    机器人:layout_height =40dp
    机器人:背景=@绘制/ row_selector
    机器人:方向=垂直>    <的TextView
        机器人:ID =@ + ID / tv_header
        机器人:layout_width =match_parent
        机器人:layout_height =0dp
        机器人:layout_weight =0
        机器人:layout_gravity =center_vertical
        机器人:paddingLeft =4DP
        机器人:背景=@彩色/ listSection
        机器人:文字=
        机器人:文字颜色=@机器人:彩色/白
        机器人:文字样式=大胆/>    <的LinearLayout
        机器人:layout_width =match_parent
        机器人:layout_height =40dp
        机器人:方向=横向>    <! -
      〜使用Android:layout_weight因此,这种观点会占用所有的空间
      〜剩下的在没有其他看法已指定了它的重量。
      - >
    <的TextView
        机器人:ID =@ + ID / lv_tune
        机器人:layout_width =0dp
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_gravity =center_vertical
        机器人:layout_weight =1
        机器人:layout_marginLeft =10dp
        机器人:文字=调
        机器人:TEXTSIZE =20SP
        机器人:文字颜色=@色/ lvForeground/>    <! -
      〜安卓重力在视图本身的严重性,而
      〜安卓:layout_gravity是观的重心在它的
      〜的ViewGroup(容器),在这种情况下,它可能不是你意什么
      〜做的,所以我删除它。
      - >    < ImageView的
        机器人:ID =@ + ID / ibAddPlaylist
        机器人:layout_width =40dp
        机器人:layout_height =34dp
        机器人:layout_gravity =中心
        机器人:知名度=看得见
        机器人:SRC =@绘制/ clipboard30x30grey/>    < ImageView的
        机器人:ID =@ + ID / ibLikeHate
        机器人:layout_width =40dp
        机器人:layout_height =34dp
        机器人:layout_gravity =中心
        机器人:知名度=看得见
        机器人:SRC =@绘制/ heart1_30x30grey/>    < ImageView的
        机器人:ID =@ + ID / ivPlay
        机器人:layout_width =40dp
        机器人:layout_height =34dp
        机器人:layout_gravity =中心
        机器人:知名度=隐形
        机器人:SRC =@绘制/ play_30x30/>  < / LinearLayout中>
< / LinearLayout中>

这里是getView()方法:

  @覆盖
       公共查看getView(INT位置,查看convertView,父母的ViewGroup)
       {
         最后查看视图= super.getView(位置,convertView,父母);
         最后的TextView文本=(TextView的)view.findViewById(R.id.lv_tune);
         最后LinearLayout.LayoutParams PARAMS =(LinearLayout.LayoutParams)text.getLayoutParams();         如果(PARAMS!= NULL)
         {
           INT喜= params.height;           如果(IsStartOfAlphaSection(位置))
           {
             // PDS:纵观布局这里的TextView ..
             如果(HEI == LayoutParams.WRAP_CONTENT)
             {
               // PDS:但是修改父视图的布局..
               // view.setBackgroundColor(0xFFDDDDDD);               // PDS:家长不具备的LinearLayout - 用途
               // AbsListView.LayoutParams
// ***下面这一行是问题..
               view.setLayoutParams(新AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,g_NormalRowHeight + g_SectionHeaderHeight));
               view.requestLayout();
// ***
             }
           }
           其他
           {
             // PDS:普通线             // PDS:纵观布局这里的TextView ..
             如果(HEI == LinearLayout.LayoutParams.WRAP_CONTENT)
             {
               // PDS:但是修改父视图的布局..
               // view.setBackgroundColor(Color.WHITE);
               view.setLayoutParams(新AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,g_NormalRowHeight));
               view.requestLayout();
             }
           }
         }         返回视图。
       }

我已经强调,是造成我的问题线(S)。我要做的就是做出属于组/节头项行高度更大。 This changes the layout that is used and also seems to mess up the click handlers for the imageviews on the first row.它也使第一行的触摸连续突出的第一行。点击任何其他行和higlight消失。

更新:以mmlooloo的建议综合考虑,低于code似乎是为我工作。看起来pretty虽然丑(所以没有原code,我张贴过!;-))

 公共查看getView(INT位置,查看convertView,父母的ViewGroup)
{
     最后查看视图。     LayoutInflater李=(LayoutInflater)mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);     如果(IsStartOfAlphaSection(位置))
     {
       鉴于= li.inflate(R.layout.like_hate_row_header,父母,假);
     }
     其他
     {
       鉴于= li.inflate(R.layout.like_hate_row,父母,假);
     }     //没有要求super.getView()被调用停止ViewBinder :: setViewValue()
     super.getView(的立场,观点,父母);
}


解决方案

我觉得你的getView有一些问题,首先你不要夸大任何布局和正在使用的getView的构造函数多数民众赞成坏主意,你必须充气convertView或使用单传递给你,但你要使用不同的位置,不同的看法你的情况,我认为你应该总是夸大convertView。我建议你​​看到这个问题,并回答和我的回答如下,以获取有关convertView感更好的意见。

<一个href=\"https://stackoverflow.com/questions/25217780/android-list-array-adapter-notifydatasetchanged-not-updating-view-correctly/25218033#25218033\">android清单阵列adapter.notifydatasetchanged没有更新的观点正确

所以,如果我想解决您的问题,我做的是这样的:
首先创建两个XML文件,一个重presents我没有标题项目,另外再presents我的头项目。
在getView我检查的位置,如果它一定是我的头按头的xml它充气别的我没有通过XML头膨胀,因此它会变成这样的:

 公共查看getView(INT位置,查看convertView,父母的ViewGroup){        LayoutInflater气筒=
                (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        查看视图。
        如果(位置==头){
              鉴于= inflater.inflate(R.layout.header,父母,假);
              //在这里你必须调用特定对象或imageButtons的finViewById
              //这显示在页眉并为它们分配点击听众
        }其他{
             鉴于= inflater.inflate(R.layout.none_header,父母,假);
              //在这里你必须调用特定对象或imageButtons的finViewById
              //它出现在无头,并将其分配点击监听器
        }        返回视图。}

和还设置聚焦的假每个ImageButton的;

I have a ListView with rows that contain ImageButtons (shown as 'O' below):

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

I am finding that the first row has intermittent behaviour. At first it appeared that the image button onclicklisteners were not being called for just the top row. What I have found is that the onclicklisteners for the buttons do get called, but the clicks seem to be queued/buffered until I click on the row itself.

I will happily post some code if nobody knows the reason for this but my code is quite large and would need pruning before posting here. I am using a cursor adapter and custom viewbinder but the click listeners are assigned within the onCreate() of my activity.

The click listeners themselves I assign to the image button objects within the setViewValue() method of my CustomViewBinder class.

I haven't used a ViewHolder (ConvertView etc) - is this my problem? It does look as though the click listeners are being assigned 3 times for every row even though I only do so if the columnIndex is equal to 1.

Here is the row item 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>

And here is the getView() method:

       @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;
       }

I have highlighted the line(s) that are causing my problem. What I do is make the row height bigger for items that are group/section headers. This changes the layout that is used and also seems to mess up the click handlers for the imageviews on the first row. It also causes touching of the first row to continuously highlight the first row. Click on any other row and the higlight disappears.

UPDATE: Taking mmlooloo's suggestions into account, the below code seems to be working for me. Looks pretty ugly though (so did the original code that I posted too! ;-) )

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 );     
}   

解决方案

I think your getView has some issues, first you do not inflate any layout and you are using the constructor of the getView thats bad idea, you must inflate convertView or using the one passes to you, But in your case that you want to use different view for different position i think you should always inflate convertView. and I recommend you to see this question and answer and the comments of below of my answer to get better sense about convertView.

android list array adapter.notifydatasetchanged not updating view correctly

So if i want to solve your problem i do it in this way: first create two XML files, one represents my none header item and another represents my header items. in the getView i check the position and if it must be header i inflate it by header xml else i inflate it by none header xml, so it will become something like this:

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;

}

and also setting focusable to false for each imageButton;

这篇关于Android的ImageButton的ListView中排clicklistener没有响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆