Android内部按钮ListView不响应点击

时间:2012-06-06 18:22:39

标签: android android-listview android-button

我有一个默认的ListView,我已经为列表项添加了我的自定义视图,但这些视图中的按钮大多数时间都不可点击。当按钮收到点击事件时,我输出一个Log.v,但是我必须点击按钮几十次才会注册点击。

与我有关的另一个问题是,当按下按钮时,我希望动画发生,显示从下面滑出的菜单。目前我已经尝试了几种不同的方法,比如为视图创建自定义类,而不是仅使用布局inflater来获取视图的relativeLayout对象,但没有任何工作正常。我甚至尝试过使用listview.getAdapter().notifyDataSetChanged();但是当我需要动画时,它只有一个弹出到位的扩展视图。

我到处搜索过,似乎唯一可行的解​​决方案是重写我自己的自定义列表视图或使用带有scrollview的线性布局。后者似乎更容易,但我不认为它几乎与列表视图一样优化。

如果您需要查看一些代码,我们将非常感谢您的建议,请告诉我们......

谢谢!

更新:

getView()包含:

    Holder hold;
    convertView = friends.get(position);
    hold = new Holder();
    hold.pos = position;
    convertView.setTag(hold);
    return convertView;

基本上我暂时将ArrayList<RelativeLayout>传递给适配器,这样我就不必每次都创建一个新视图,这样当我向下滚动时动画将保持动画...

在此活动的OnCreate()内我使用下一个代码设置ArrayList<RelativeLayout>,但这只是暂时的,因为我打算稍后使用其他方法,例如Async任务或其他什么以便此视图包含一些数据...

    RelativeLayout temp;
    for(int i=0; i<30; i++){
        temp = (RelativeLayout) inflater.inflate(R.layout.list_item, null);

        final LinearLayout extraListItemInfo = (LinearLayout) temp.findViewById(R.id.extraListItemInfo);

        Button infoBtn = (Button) temp.findViewById(R.id.infoBtn);
        infoBtn.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                Log.v("ListItem", "Button has been clicked... ");
                extraListItemInfo .setVisibility(View.VISIBLE);

                ExpandAnimation closeAnim = new ExpandAnimation(extraListItemInfo , animHeight);
                closeAnim.setDuration(750);
                closeAnim.setFillAfter(true);

                if(extraListItemInfo .getTag() == null || !extraListItemInfo .getTag().equals("expanded")){
                    extraListItemInfo .getLayoutParams().height = 0;
                    friendInfoList.startAnimation(closeAnim.expand());
                    extraListItemInfo .setTag("expanded");
                }else if(extraListItemInfo .getTag().equals("expanded")){
                    extraListItemInfo .startAnimation(closeAnim.collapse());
                    extraListItemInfo .setTag("closed");
                }
                //((BaseAdapter) listview.getAdapter()).notifyDataSetChanged(); i tried it here once but then left it
    //as the only action inside the listview's onitemclick()
            }
        });

        listItems.add(temp);
    }

这是我正在使用的列表项:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/darkgrey"
    android:paddingBottom="5dp" >

    <LinearLayout
        android:id="@+id/extraListItemInfo "
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/listItemInfo"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="-10dp"
        android:background="@color/grey"
        android:orientation="vertical"
        android:visibility="gone" >


        <RelativeLayout
            android:id="@+id/RelativeLayout04"
            android:layout_width="match_parent"
            android:layout_height="@dimen/activity_list_height"
            android:layout_marginTop="5dp" >

            <ImageView
                android:id="@+id/ImageView04"
                android:layout_width="wrap_content"
                android:layout_height="25dp"
                android:layout_margin="5dp"
                android:src="@drawable/logo_d" />

            <TextView
                android:id="@+id/TextView04"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@+id/ImageView04"
                android:text="TextView"
                android:textColor="@color/black"
                android:textSize="17dp" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/RelativeLayout03"
            android:layout_width="match_parent"
            android:layout_height="@dimen/activity_list_height" >

            <ImageView
                android:id="@+id/ImageView03"
                android:layout_width="wrap_content"
                android:layout_height="25dp"
                android:layout_margin="5dp"
                android:src="@drawable/logo_d" />

            <TextView
                android:id="@+id/TextView03"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@+id/ImageView03"
                android:text="TextView"
                android:textColor="@color/black"
                android:textSize="17dp" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/RelativeLayout02"
            android:layout_width="match_parent"
            android:layout_height="@dimen/activity_list_height" >

            <ImageView
                android:id="@+id/ImageView02"
                android:layout_width="wrap_content"
                android:layout_height="25dp"
                android:layout_margin="5dp"
                android:src="@drawable/logo_d" />

            <TextView
                android:id="@+id/TextView02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@+id/ImageView02"
                android:text="TextView"
                android:textColor="@color/black"
                android:textSize="17dp" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/relativeLayout1"
            android:layout_width="match_parent"
            android:layout_height="@dimen/activity_list_height" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="25dp"
                android:layout_margin="5dp"
                android:src="@drawable/logo_d" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@id/imageView1"
                android:text="TextView"
                android:textColor="@color/black"
                android:textSize="17dp" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/RelativeLayout01"
            android:layout_width="match_parent"
            android:layout_height="@dimen/activity_list_height">

            <ImageView
                android:id="@+id/ImageView01"
                android:layout_width="wrap_content"
                android:layout_height="25dp"
                android:layout_margin="5dp"
                android:src="@drawable/logo_d" />

            <TextView
                android:id="@+id/TextView01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@+id/ImageView01"
                android:text="TextView"
                android:textColor="@color/black"
                android:textSize="17dp" />
        </RelativeLayout>
    </LinearLayout>
        <RelativeLayout
        android:id="@+id/listItemInfo"
        android:layout_width="wrap_content"
        android:layout_height="95dp"
        android:background="@drawable/friend_cell_background2x"
        android:clickable="true" >


        <RelativeLayout
            android:id="@+id/leftLayout"
            android:layout_width="90dp"
            android:layout_height="match_parent" >
            <ImageView
                android:id="@+id/imgCompany"
                android:layout_width="60dp"
                android:layout_height="50dp"
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:scaleType="centerInside"
                android:src="@drawable/user2x" />
            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:scaleType="fitXY"
                android:src="@drawable/online_indicator2s" />

        </RelativeLayout>


        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_marginTop="5dp"
            android:layout_toRightOf="@+id/leftLayout"
            android:background="@android:color/transparent"
            android:gravity="left|center"
            android:orientation="vertical"
            android:paddingLeft="5dp" >


            <TextView
                android:id="@+id/lblCompanyName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Contact Name"
                android:textColor="@color/white"
                android:textSize="18dp"
                android:textStyle="bold" >

            </TextView>


            <TextView
                android:id="@+id/lblReawrdDesc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Last Played Offer"
                android:textColor="@color/white"
                android:textSize="17dp" >

            </TextView>
        </LinearLayout>

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="5dp"
            android:src="@drawable/facebook_btn2x" />
        <Button
            android:id="@+id/infoBtn"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:layout_alignRight="@+id/imageView4"
            android:background="@drawable/info_btn2x"
            android:clickable="true" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="13dp"
            android:layout_toLeftOf="@+id/infoBtn"
            android:text="Follows 30+"
            android:textColor="@color/white"
            android:textSize="11dp" />

        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="75dp"
            android:layout_height="20dp"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="10dp"
            android:layout_toLeftOf="@+id/textView2"
            android:background="@drawable/fan_btn2x"
            android:text="Fans 30+"
            android:textColor="@color/white"
            android:textSize="11dp" />


        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/imageView4"
            android:src="@drawable/google_btn2x" />

    </RelativeLayout>

</RelativeLayout>

对不起任何可能让人难以阅读的布局问题...但我希望这可以帮助你们理解我的问题...谢谢

更新2:

所有这些答案在某些方面都有所帮助,但我认为我必须首先解决的主要问题是为什么按钮在我第一次滚动离开listItem然后回到它之后才接收到点击事件,然后再次单击该按钮...如果有人可以帮助找到解决方案我思考其他一切将更容易解决... 感谢...

根据要求提供截图,但请记住这张照片是在三星Galaxy标签10.1上拍摄的,由于我使用相同的布局放大这个大屏幕,它看起来与我通常测试的手机上的内容大不相同(摩托罗拉droid x根本没有根,不能截图...)

enter image description here

另一个更新: 我设法通过Extending ArrayAdapter而不是基本适配器来使点击和动画很好地工作。可悲的是,我仍然遇到问题,因为只有列表的下半部分是可点击的。列表的上半部分仍然像以前一样表现出非常微不足道的点击事件......关于这次发生了什么的任何想法?感谢...

6 个答案:

答案 0 :(得分:3)

嗯,这不是一个真正的答案,但在重写了几次后,我设法修复它,使其完全按照我想要的方式运行。

这一次,我将所有数据与每个视图分开,让每个列表项都是继承RelativeLayout的自定义类,并为其特定的OnClickListener实现自己的infoBtn。< / p>

我的适配器现在只是扩展了ArrayAdapter&lt;&gt;并覆盖此getView()方法:

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

    if(convertView != null && (convertView instanceof FriendListItem2))
        ((FriendListItem2) convertView).setFriendInfo(friends.get(position));
    else
        convertView = new FriendListItem2(getContext(), friends.get(position));

    return convertView;
}

最后,在这个页面的主要活动中,我只需使用我传递数据的适配器设置ListView

这比以前更加清晰,我希望它没有对代码进行多次重写以使其正确。希望有人可以从中受益,尽管我仍然不知道为什么我之前遇到问题。

感谢以前的所有建议。

答案 1 :(得分:2)

这是为每一行创建的复杂布局......

无论如何,当您使用new关键字时,您正在创建不同的范围。简而言之,你的onClickListener 会看到你期望它看到的30个不同的extraListItemInfo引用。

尝试这样的事情:

@Override
public void onClick(View v) {
    LinearLayout extraListItemInfo = v.getParent();
    ...

答案 2 :(得分:2)

尝试在放置子视图的顶层布局中使用此属性。

android:descendantFocusability="blocksDescendants"

它有点棘手,但我建议如果上面的行不起作用,尝试从按钮中删除focusable = true和focusable =“false”之间切换。它应该工作。

干杯!

答案 3 :(得分:0)

android:onClick="onClick"添加到按钮xml,它会调用onClick()方法

答案 4 :(得分:0)

我遇到了同样的问题。尝试从相对布局中获取“android:clicable =”true“”。当你这样做时,活动期望你做setOnClickListener到那个RelativeLayout。它对我有用

答案 5 :(得分:0)

您可以从ListViewAdapter类的getView()方法触发按钮上的click事件。 看到这个问题

Android : How to set onClick event for Button in List item of ListView

相关问题