ListView中的按钮不可点击 [英] Button inside ListView not clickable
问题描述
ListView在一个片段内。
$
< 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.0encoding =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>
$ b $ 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 Description
android:textSize =14sp/>
$ Button $ b $ 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
/ >
$ b< 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 =
android:textStyle =bold
android:gravity =center_vertical
android:textSize =14sp
/>
$ b b 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:textStyle = bold
android:textSize =14sp/>
< / LinearLayout>
< / LinearLayout>
< / LinearLayout>
< / LinearLayout>
我的列表适配器是:
TextView separator;
LinearLayout relativeLayout;
TextView eventHeader;
TextView eventDescription;
ImageView blueDot;
按钮button1;
按钮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);
}
返回convertView;
$ b $ p
$ b
问题在于两个按钮不可点击。我试过很多:
ListView listView =(ListView)rootView.findViewById(R.id.event_list);
listView.setItemsCanFocus(true);
我也尝试了按钮上的设置:
< pre $ android:focusable =true
android:clickable =true
我还试用了android:descendantFocusability。
我的任何一个尝试都没有使按钮可点击。
xml应该如下:
<?xml version =1.0encoding =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>
$ b $ 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 Description
android:textSize =14sp/>
$ Button $ b $ 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
/ >
$ b< 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 =
android:textStyle =bold
android:gravity =center_vertical
android:textSize =14sp
/>
$ b b 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:textStyle = bold
android:textSize =14sp/>
< / LinearLayout>
< / LinearLayout>
< / LinearLayout>
< / LinearLayout>
I want to be able to click on a button inside an item of a ListView. It should have a different effect from clicking the whole item. I realize there are several questions asked on stackoverflow, but none of the suggestions works for me.
The ListView is inside a Fragment.
Layout of the 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>
Layout of each list item:
<?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>
My list adapter is:
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;
}
}
The problem is that the two buttons are not clickable. What I tried to far:
ListView listView = (ListView) rootView.findViewById(R.id.event_list);
listView.setItemsCanFocus(true);
I also tried setting on the button:
android:focusable="true"
android:clickable="true"
I also experimented with android:descendantFocusability.
None of my tries made the buttons clickable.
Insert the attribute android:descendantFocusability="blocksDescendants"
in the Parent Layout declaration of each list item.
The xml should be as follows:
<?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>
这篇关于ListView中的按钮不可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!