【问题标题】:Button inside ListView not clickableListView 内的按钮不可点击
【发布时间】:2013-12-20 18:24:01
【问题描述】:

我希望能够单击 ListView 项目内的按钮。它应该与单击整个项目具有不同的效果。我知道有人在 stackoverflow 上提出了几个问题,但没有一个建议对我有用。

ListView 在 Fragment 中。

片段的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".EventFragment" >

    <ListView
        android:id="@+id/event_list"
        android:background="#C0FFFFFF" 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="5dp" />

</RelativeLayout>

每个列表项的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" 
android:background="#C0101010">

    <TextView
    android:id="@+id/event_list_separator"
    style="?android:attr/listSeparatorTextViewStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="separator"
    android:textColor="@android:color/white" />

    <LinearLayout
    android:id="@+id/event_list_element"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFFFF"
    android:padding="6dip" >

        <ImageView
        android:id="@+id/event_list_element_icon"
        android:layout_width="26dip"
        android:layout_height="60dip"
        android:layout_marginRight="6dip"
        android:contentDescription="TODO" />

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

            <TextView
            android:id="@+id/event_list_element_firstLine"
            android:layout_width="match_parent"
            android:layout_height="25dip"
            android:text="item_header"
            android:textSize="18sp" />

            <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="35dip"
            android:orientation="horizontal" >

                <TextView
                android:id="@+id/event_list_element_secondLine"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:ellipsize="marquee"
                android:singleLine="true"
                android:text="Description"
                android:textSize="14sp" />

                <Button
                android:id="@+id/event_list_element_button_1"
                android:layout_width="132dip"
                android:layout_height="match_parent"
                android:drawableLeft="@drawable/ic_button1"
                android:text="Participate"
                android:textStyle="bold"
                android:textSize="14sp"
                />


                <Button
                android:id="@+id/event_list_element_button_2"
                android:layout_width="110dip"
                android:layout_height="match_parent"
                android:ellipsize="marquee"
                android:drawableLeft="@drawable/ic_button2"
                android:singleLine="true"
                android:text="No thanks"
                android:textStyle="bold"
                android:gravity="center_vertical"
                android:textSize="14sp" 
                />

                <TextView
                android:id="@+id/event_list_element_additional_text"
                android:layout_width="100dip"
                android:layout_height="match_parent"
                android:ellipsize="marquee"
                android:singleLine="true"
                android:gravity="center_vertical"
                android:text="sample"
                android:textStyle="bold"
                android:textSize="14sp" /> 
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

我的列表适配器是:

public class EventAdapter extends ArrayAdapter<Event> {
    static class ViewHolder {
        TextView separator;
        LinearLayout relativeLayout;
        TextView eventHeader;
        TextView eventDescription;
        ImageView blueDot;
        Button button1;
        Button button2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater)     
            _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.event_list_item, parent, false);

            viewHolder = new ViewHolder();
            viewHolder.relativeLayout = (LinearLayout) convertView.findViewById(R.id.event_list_element);
            viewHolder.blueDot = (ImageView) convertView.findViewById(R.id.event_list_element_icon);
            viewHolder.eventHeader = (TextView) convertView.findViewById(R.id.event_list_element_firstLine);
            viewHolder.eventDescription = (TextView) convertView.findViewById(R.id.event_list_element_secondLine);
            viewHolder.button1 = (Button) convertView.findViewById(R.id.event_list_element_button1);
            viewHolder.button2 = (Button) convertView.findViewById(R.id.event_list_element_button2);
            viewHolder.separator = (TextView) convertView.findViewById(R.id.event_list_separator);
            convertView.setTag(viewHolder);
        } else{
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final Event item = getItem(position);
        if (item != null) {     
            OnClickListener listener = new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    Toast.makeText(_context, "boo!", Toast.LENGTH_SHORT).show();
                }
            };

            viewHolder.button1.setOnClickListener(listener);            
        }
        return convertView;
    }
}

问题是这两个按钮不可点击。我尝试过的:

ListView listView = (ListView) rootView.findViewById(R.id.event_list);
listView.setItemsCanFocus(true);

我也尝试在按钮上设置:

android:focusable="true"
android:clickable="true"

我还尝试了 android:descendantFocusability。

我的尝试都没有使按钮可点击。

【问题讨论】:

  • 尝试将点击侦听器移动到将 ui 元素与变量绑定的位置。看看这是否解决了它,如果没有,那么我认为您的列表视图正在吞噬事件。
  • 在谷歌上输入你的问题标题给了我这个:stackoverflow.com/questions/3045872/…
  • 谢谢,Ardo K。我在 Google 上看到了。它对我不起作用。我能够添加项目点击侦听器,但按钮没有反应。

标签: android listview button focus clickable


【解决方案1】:

在每个列表项的父布局声明中插入属性android:descendantFocusability="blocksDescendants"。 xml应该如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" 
android:descendantFocusability="blocksDescendants"
android:background="#C0101010">

    <TextView
    android:id="@+id/event_list_separator"
    style="?android:attr/listSeparatorTextViewStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="separator"
    android:textColor="@android:color/white" />

    <LinearLayout
    android:id="@+id/event_list_element"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFFFF"
    android:padding="6dip" >

        <ImageView
        android:id="@+id/event_list_element_icon"
        android:layout_width="26dip"
        android:layout_height="60dip"
        android:layout_marginRight="6dip"
        android:contentDescription="TODO" />

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

            <TextView
            android:id="@+id/event_list_element_firstLine"
            android:layout_width="match_parent"
            android:layout_height="25dip"
            android:text="item_header"
            android:textSize="18sp" />

            <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="35dip"
            android:orientation="horizontal" >

                <TextView
                android:id="@+id/event_list_element_secondLine"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:ellipsize="marquee"
                android:singleLine="true"
                android:text="Description"
                android:textSize="14sp" />

                <Button
                android:id="@+id/event_list_element_button_1"
                android:layout_width="132dip"
                android:layout_height="match_parent"
                android:drawableLeft="@drawable/ic_button1"
                android:text="Participate"
                android:textStyle="bold"
                android:textSize="14sp"
                />


                <Button
                android:id="@+id/event_list_element_button_2"
                android:layout_width="110dip"
                android:layout_height="match_parent"
                android:ellipsize="marquee"
                android:drawableLeft="@drawable/ic_button2"
                android:singleLine="true"
                android:text="No thanks"
                android:textStyle="bold"
                android:gravity="center_vertical"
                android:textSize="14sp" 
                />

                <TextView
                android:id="@+id/event_list_element_additional_text"
                android:layout_width="100dip"
                android:layout_height="match_parent"
                android:ellipsize="marquee"
                android:singleLine="true"
                android:gravity="center_vertical"
                android:text="sample"
                android:textStyle="bold"
                android:textSize="14sp" /> 
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

【讨论】:

    【解决方案2】:

    我找到了解决方案!我想定期更新事件列表(现在它是每 x 毫秒运行一次的线程,稍后我想将其切换为仅在发生更改时更新事件列表)。无论如何,代码是(在我的主要活动中):

    private BroadcastReceiver _bReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(RECEIVE_EVENT)) {
                Bundle bundle = intent.getExtras();
                Event event = (Event) bundle.get("event");
                showEvent(event);
            }
        }
    };
    
    private void showEvent(final Event event){
        final Context context = this;
    
        runOnUiThread(new Runnable() {
            public void run() {
                final ListView listview = (ListView) findViewById(R.id.event_list);
    
                EventAdapter adapter = new EventAdapter(context, id.event_list, getEventList());
                listview.setAdapter(adapter);     
            }
        });
    }
    

    每次都设置适配器肯定不是正确的做法。一旦我将其更改为只设置一次适配器,它就像建议使用 android:descendantFocusability="blocksDescendants"

    【讨论】:

    • 很高兴知道它符合我的建议 android:descendantFocusability="blocksDescendants" :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多